smartstring/dist/smartstring.indent.js

85 lines
5.7 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* splits a string into an array
* @param stringArg
*/
let splitStringAtLineBreak = (stringArg) => {
let resultArray = stringArg.split('\n');
return cleanStringArray(resultArray);
};
/**
* joins a string together again
* @param stringArrayArg
*/
let joinStringWithLineBreaks = (stringArrayArg) => {
let resultString = '';
for (let line of stringArrayArg) {
resultString = resultString + line + '\n'; // add new line at end
}
return resultString;
};
/**
* cleans first and last line in case they are empty
* @param stringArrayArg
*/
let cleanStringArray = (stringArrayArg) => {
let testRegex = /^[\s]*$/;
if (testRegex.test(stringArrayArg[0])) {
stringArrayArg.shift();
}
if (testRegex.test(stringArrayArg[stringArrayArg.length - 1])) {
stringArrayArg.pop();
}
return stringArrayArg;
};
/**
* indent an array
* @param stringArg
* @param spaceAmount
*/
exports.indent = (stringArg, spaceAmount) => {
let localStringArray = splitStringAtLineBreak(stringArg);
for (let stringArg of localStringArray) {
stringArg = ' '.repeat(spaceAmount) + stringArg;
}
let resultString = joinStringWithLineBreaks(localStringArray);
return resultString;
};
/**
* indents a string with prefix
* @param stringArg
* @param prefixArg
*/
exports.indentWithPrefix = (stringArg, prefixArg) => {
let resultString;
let stringArray = splitStringAtLineBreak(stringArg);
let resultArray = [];
for (let stringItem of stringArray) {
resultArray.push(prefixArg + stringItem);
}
resultString = joinStringWithLineBreaks(resultArray);
return resultString;
};
exports.normalize = (stringArg) => {
let resultString;
let splitStringArray = splitStringAtLineBreak(stringArg);
let minCommonLeftOffset;
let deIndentRegex = /^(\s*)/;
let emptyLineRegex = /^(\s*)$/;
for (let stringItem of splitStringArray) {
let offsetString = deIndentRegex.exec(stringItem)[1];
if ((typeof minCommonLeftOffset === 'undefined' || offsetString.length < minCommonLeftOffset)
&& !emptyLineRegex.test(stringItem)) {
minCommonLeftOffset = offsetString.length;
}
}
;
let resultSplitStringArray = [];
for (let stringItem of splitStringArray) {
resultSplitStringArray.push(stringItem.substr(minCommonLeftOffset));
}
resultString = joinStringWithLineBreaks(resultSplitStringArray);
return resultString;
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzdHJpbmcuaW5kZW50LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzdHJpbmcuaW5kZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBRUE7OztHQUdHO0FBQ0gsSUFBSSxzQkFBc0IsR0FBRyxDQUFDLFNBQWlCO0lBQzdDLElBQUksV0FBVyxHQUFHLFNBQVMsQ0FBQyxLQUFLLENBQUMsSUFBSSxDQUFDLENBQUE7SUFDdkMsTUFBTSxDQUFDLGdCQUFnQixDQUFDLFdBQVcsQ0FBQyxDQUFBO0FBQ3RDLENBQUMsQ0FBQTtBQUVEOzs7R0FHRztBQUNILElBQUksd0JBQXdCLEdBQUcsQ0FBQyxjQUF3QjtJQUN0RCxJQUFJLFlBQVksR0FBVyxFQUFFLENBQUE7SUFDN0IsR0FBRyxDQUFDLENBQUMsSUFBSSxJQUFJLElBQUksY0FBYyxDQUFDLENBQUMsQ0FBQztRQUNoQyxZQUFZLEdBQUcsWUFBWSxHQUFHLElBQUksR0FBRyxJQUFJLENBQUEsQ0FBQyxzQkFBc0I7SUFDbEUsQ0FBQztJQUNELE1BQU0sQ0FBQyxZQUFZLENBQUE7QUFDckIsQ0FBQyxDQUFBO0FBRUQ7OztHQUdHO0FBQ0gsSUFBSSxnQkFBZ0IsR0FBRyxDQUFDLGNBQXdCO0lBQzlDLElBQUksU0FBUyxHQUFHLFNBQVMsQ0FBQTtJQUN6QixFQUFFLENBQUMsQ0FBQyxTQUFTLENBQUMsSUFBSSxDQUFDLGNBQWMsQ0FBRSxDQUFDLENBQUUsQ0FBQyxDQUFDLENBQUMsQ0FBQztRQUN4QyxjQUFjLENBQUMsS0FBSyxFQUFFLENBQUE7SUFDeEIsQ0FBQztJQUNELEVBQU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