2022-03-18 21:50:24 +00:00
|
|
|
import * as plugins from './smartstring.plugins.js';
|
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[] => {
|
2018-07-21 12:37:39 +00:00
|
|
|
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 => {
|
2018-07-21 12:37:39 +00:00
|
|
|
let resultString: string = '';
|
2017-10-05 13:55:59 +00:00
|
|
|
for (let line of stringArrayArg) {
|
2018-07-21 12:37:39 +00:00
|
|
|
resultString = resultString + line + '\n'; // add new line at end
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2018-07-21 12:37:39 +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[] => {
|
2018-07-21 12:37:39 +00:00
|
|
|
let testRegex = /^[\s]*$/;
|
|
|
|
if (testRegex.test(stringArrayArg[0])) {
|
|
|
|
stringArrayArg.shift();
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2018-07-21 12:37:39 +00:00
|
|
|
if (testRegex.test(stringArrayArg[stringArrayArg.length - 1])) {
|
|
|
|
stringArrayArg.pop();
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2018-07-21 12:37:39 +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 => {
|
2018-07-21 12:37:39 +00:00
|
|
|
let localStringArray = splitStringAtLineBreak(stringArg);
|
2017-10-05 13:55:59 +00:00
|
|
|
for (let stringArg of localStringArray) {
|
2018-07-21 12:37:39 +00:00
|
|
|
stringArg = ' '.repeat(spaceAmount) + stringArg;
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2018-07-21 12:37:39 +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 => {
|
2018-07-21 12:37:39 +00:00
|
|
|
let resultString: string;
|
|
|
|
let stringArray = splitStringAtLineBreak(stringArg);
|
|
|
|
let resultArray: string[] = [];
|
2017-10-05 13:55:59 +00:00
|
|
|
for (let stringItem of stringArray) {
|
2018-07-21 12:37:39 +00:00
|
|
|
resultArray.push(prefixArg + stringItem);
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2018-07-21 12:37:39 +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 => {
|
2018-07-21 12:37:39 +00:00
|
|
|
let resultString: string;
|
|
|
|
let splitStringArray: string[] = splitStringAtLineBreak(stringArg);
|
|
|
|
let minCommonLeftOffset: number;
|
2017-10-26 13:24:10 +00:00
|
|
|
|
2018-07-21 12:37:39 +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) {
|
2018-07-21 12:37:39 +00:00
|
|
|
let offsetString = deIndentRegex.exec(stringItem)[1];
|
2017-10-05 13:55:59 +00:00
|
|
|
if (
|
2018-07-21 12:37:39 +00:00
|
|
|
(typeof minCommonLeftOffset === 'undefined' || offsetString.length < minCommonLeftOffset) &&
|
|
|
|
!emptyLineRegex.test(stringItem)
|
2017-10-05 13:55:59 +00:00
|
|
|
) {
|
2018-07-21 12:37:39 +00:00
|
|
|
minCommonLeftOffset = offsetString.length;
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2018-07-21 12:37:39 +00:00
|
|
|
}
|
|
|
|
let resultSplitStringArray = [];
|
2017-10-05 13:55:59 +00:00
|
|
|
for (let stringItem of splitStringArray) {
|
2018-07-21 12:37:39 +00:00
|
|
|
resultSplitStringArray.push(stringItem.substr(minCommonLeftOffset));
|
2017-10-05 13:55:59 +00:00
|
|
|
}
|
2018-07-21 12:37:39 +00:00
|
|
|
resultString = joinStringWithLineBreaks(resultSplitStringArray);
|
|
|
|
return resultString;
|
|
|
|
};
|