smartstring/ts/smartstring.indent.ts

93 lines
2.6 KiB
TypeScript
Raw Normal View History

import * as plugins from './smartstring.plugins';
2016-07-06 08:04:25 +00:00
2017-10-05 13:55:59 +00:00
/**
* splits a string into an array
* @param stringArg
*/
2017-10-26 13:24:10 +00:00
const splitStringAtLineBreak = (stringArg: string): string[] => {
let resultArray = stringArg.split('\n');
return cleanStringArray(resultArray);
};
2016-07-06 08:04:25 +00:00
2017-10-05 13:55:59 +00:00
/**
* joins a string together again
* @param stringArrayArg
*/
2017-10-26 13:24:10 +00:00
const joinStringWithLineBreaks = (stringArrayArg: string[]): string => {
let resultString: string = '';
2017-10-05 13:55:59 +00:00
for (let line of stringArrayArg) {
resultString = resultString + line + '\n'; // add new line at end
2017-10-05 13:55:59 +00:00
}
return resultString;
};
2016-07-07 21:35:15 +00:00
2017-10-05 13:55:59 +00:00
/**
* cleans first and last line in case they are empty
* @param stringArrayArg
*/
2017-10-26 13:24:10 +00:00
const cleanStringArray = (stringArrayArg: string[]): string[] => {
let testRegex = /^[\s]*$/;
if (testRegex.test(stringArrayArg[0])) {
stringArrayArg.shift();
2017-10-05 13:55:59 +00:00
}
if (testRegex.test(stringArrayArg[stringArrayArg.length - 1])) {
stringArrayArg.pop();
2017-10-05 13:55:59 +00:00
}
return stringArrayArg;
};
2016-07-06 08:04:25 +00:00
2017-10-05 13:55:59 +00:00
/**
* indent an array
* @param stringArg
* @param spaceAmount
*/
2017-10-26 13:24:10 +00:00
export const indent = (stringArg: string, spaceAmount: number): string => {
let localStringArray = splitStringAtLineBreak(stringArg);
2017-10-05 13:55:59 +00:00
for (let stringArg of localStringArray) {
stringArg = ' '.repeat(spaceAmount) + stringArg;
2017-10-05 13:55:59 +00:00
}
let resultString = joinStringWithLineBreaks(localStringArray);
return resultString;
};
2016-07-06 08:04:25 +00:00
2017-10-05 13:55:59 +00:00
/**
* indents a string with prefix
* @param stringArg
* @param prefixArg
*/
2017-10-26 13:24:10 +00:00
export const indentWithPrefix = (stringArg: string, prefixArg: string): string => {
let resultString: string;
let stringArray = splitStringAtLineBreak(stringArg);
let resultArray: string[] = [];
2017-10-05 13:55:59 +00:00
for (let stringItem of stringArray) {
resultArray.push(prefixArg + stringItem);
2017-10-05 13:55:59 +00:00
}
resultString = joinStringWithLineBreaks(resultArray);
return resultString;
};
2016-07-06 08:04:25 +00:00
2017-10-26 13:24:10 +00:00
export const normalize = (stringArg: string): string => {
let resultString: string;
let splitStringArray: string[] = splitStringAtLineBreak(stringArg);
let minCommonLeftOffset: number;
2017-10-26 13:24:10 +00:00
const deIndentRegex = /^(\s*)/;
const emptyLineRegex = /^(\s*)$/;
2017-10-26 13:24:10 +00:00
2017-10-05 13:55:59 +00:00
for (let stringItem of splitStringArray) {
let offsetString = deIndentRegex.exec(stringItem)[1];
2017-10-05 13:55:59 +00:00
if (
(typeof minCommonLeftOffset === 'undefined' || offsetString.length < minCommonLeftOffset) &&
!emptyLineRegex.test(stringItem)
2017-10-05 13:55:59 +00:00
) {
minCommonLeftOffset = offsetString.length;
2017-10-05 13:55:59 +00:00
}
}
let resultSplitStringArray = [];
2017-10-05 13:55:59 +00:00
for (let stringItem of splitStringArray) {
resultSplitStringArray.push(stringItem.substr(minCommonLeftOffset));
2017-10-05 13:55:59 +00:00
}
resultString = joinStringWithLineBreaks(resultSplitStringArray);
return resultString;
};