smartstring/dist/smartstring.indent.js

61 lines
4.6 KiB
JavaScript
Raw Normal View History

2016-07-06 08:04:25 +00:00
"use strict";
2016-07-17 15:30:26 +00:00
let splitString = (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
};
2016-07-17 15:30:26 +00:00
let joinString = (stringArrayArg) => {
2016-10-31 23:22:38 +00:00
let resultString = '';
2016-07-17 15:30:26 +00:00
for (let line of stringArrayArg) {
2016-10-31 23:22:38 +00:00
resultString = resultString + line + '\n';
2016-07-06 08:04:25 +00:00
}
2016-07-07 21:35:15 +00:00
return resultString;
};
2016-07-17 15:30:26 +00:00
let cleanStringArray = (stringArrayArg) => {
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
};
2016-07-17 15:30:26 +00:00
exports.indent = (stringArg, spaceAmount) => {
let resultString;
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.indentWithPrefix = (stringArg, prefixArg) => {
let resultString;
let stringArray = splitString(stringArg);
let resultArray = [];
for (let stringItem of stringArray) {
2016-07-07 22:57:11 +00:00
resultArray.push(prefixArg + stringItem);
}
;
resultString = joinString(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;
let splitStringArray = splitString(stringArg);
let minCommonLeftOffset;
let deIndentRegex = /^(\s*)/;
let emptyLineRegex = /^(\s*)$/;
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));
}
;
resultString = joinString(resultSplitStringArray);
return resultString;
2016-07-06 08:04:25 +00:00
};
2016-10-31 23:22:38 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzdHJpbmcuaW5kZW50LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzdHJpbmcuaW5kZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFFQSxJQUFJLFdBQVcsR0FBRyxDQUFDLFNBQWlCO0lBQ2hDLElBQUksV0FBVyxHQUFHLFNBQVMsQ0FBQyxLQUFLLENBQUMsSUFBSSxDQUFDLENBQUE7SUFDdkMsTUFBTSxDQUFDLGdCQUFnQixDQUFDLFdBQVcsQ0FBQyxDQUFBO0FBQ3hDLENBQUMsQ0FBQztBQUVGLElBQUksVUFBVSxHQUFHLENBQUMsY0FBd0I7SUFDdEMsSUFBSSxZQUFZLEdBQVcsRUFBRSxDQUFBO0lBQzdCLEdBQUcsQ0FBQyxDQUFDLElBQUksSUFBSSxJQUFJLGNBQWMsQ0FBQyxDQUFBLENBQUM7UUFDN0IsWUFBWSxHQUFHLFlBQVksR0FBRyxJQUFJLEdBQUcsSUFBSSxDQUFBO0lBQzdDLENBQUM7SUFDRCxNQUFNLENBQUMsWUFBWSxDQUFBO0FBQ3ZCLENBQUMsQ0FBQTtBQUVELElBQUksZ0JBQWdCLEdBQUcsQ0FBQyxjQUF3QjtJQUM1QyxJQUFJLFNBQVMsR0FBRyxTQUFTLENBQUE7SUFDekIsRUFBRSxDQUFDLENBQUMsU0FBUy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