smartstring/dist/smartstring.indent.js

85 lines
5.8 KiB
JavaScript
Raw Normal View History

2016-07-06 08:04:25 +00:00
"use strict";
2017-10-05 13:55:59 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
/**
* splits a string into an array
* @param stringArg
*/
2017-10-26 13:24:10 +00:00
const splitStringAtLineBreak = (stringArg) => {
2016-10-31 23:22:38 +00:00
let resultArray = stringArg.split('\n');
2016-07-07 22:57:11 +00:00
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) => {
2016-10-31 23:22:38 +00:00
let resultString = '';
2016-07-17 15:30:26 +00:00
for (let line of stringArrayArg) {
2017-10-05 13:55:59 +00:00
resultString = resultString + line + '\n'; // add new line at end
2016-07-06 08:04:25 +00:00
}
2016-07-07 21:35:15 +00:00
return resultString;
};
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) => {
2016-07-17 15:30:26 +00:00
let testRegex = /^[\s]*$/;
2016-07-07 21:35:15 +00:00
if (testRegex.test(stringArrayArg[0])) {
stringArrayArg.shift();
}
if (testRegex.test(stringArrayArg[stringArrayArg.length - 1])) {
stringArrayArg.pop();
}
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
*/
2016-07-17 15:30:26 +00:00
exports.indent = (stringArg, spaceAmount) => {
2017-10-05 13:55:59 +00:00
let localStringArray = splitStringAtLineBreak(stringArg);
for (let stringArg of localStringArray) {
stringArg = ' '.repeat(spaceAmount) + stringArg;
}
let resultString = joinStringWithLineBreaks(localStringArray);
2016-07-07 21:35:15 +00:00
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
*/
2016-07-17 15:30:26 +00:00
exports.indentWithPrefix = (stringArg, prefixArg) => {
let resultString;
2017-10-05 13:55:59 +00:00
let stringArray = splitStringAtLineBreak(stringArg);
2016-07-17 15:30:26 +00:00
let resultArray = [];
for (let stringItem of stringArray) {
2016-07-07 22:57:11 +00:00
resultArray.push(prefixArg + stringItem);
}
2017-10-05 13:55:59 +00:00
resultString = joinStringWithLineBreaks(resultArray);
2016-07-07 21:35:15 +00:00
return resultString;
2016-07-06 08:04:25 +00:00
};
2016-07-17 15:30:26 +00:00
exports.normalize = (stringArg) => {
let resultString;
2017-10-05 13:55:59 +00:00
let splitStringArray = splitStringAtLineBreak(stringArg);
2016-07-17 15:30:26 +00:00
let minCommonLeftOffset;
2017-10-26 13:24:10 +00:00
const deIndentRegex = /^(\s*)/;
const emptyLineRegex = /^(\s*)$/;
2016-07-17 15:30:26 +00:00
for (let stringItem of splitStringArray) {
let offsetString = deIndentRegex.exec(stringItem)[1];
2016-10-31 23:22:38 +00:00
if ((typeof minCommonLeftOffset === 'undefined' || offsetString.length < minCommonLeftOffset)
&& !emptyLineRegex.test(stringItem)) {
2016-07-07 21:35:15 +00:00
minCommonLeftOffset = offsetString.length;
}
}
;
2016-07-17 15:30:26 +00:00
let resultSplitStringArray = [];
for (let stringItem of splitStringArray) {
2016-07-07 21:35:15 +00:00
resultSplitStringArray.push(stringItem.substr(minCommonLeftOffset));
}
2017-10-05 13:55:59 +00:00
resultString = joinStringWithLineBreaks(resultSplitStringArray);
2016-07-07 21:35:15 +00:00
return resultString;
2016-07-06 08:04:25 +00:00
};
2017-10-26 13:24:10 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzdHJpbmcuaW5kZW50LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzdHJpbmcuaW5kZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3M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