smartstring/ts/smartstring.indent.ts

61 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2016-07-06 08:04:25 +00:00
import * as plugins from "./smartstring.plugins";
let splitString = (stringArg:string):string[] => {
2016-07-07 22:57:11 +00:00
let resultArray = stringArg.split("\n");
return cleanStringArray(resultArray);
2016-07-06 08:04:25 +00:00
};
let joinString = (stringArrayArg:string[]):string => {
2016-07-07 17:53:14 +00:00
let resultString:string = "";
2016-07-06 08:04:25 +00:00
for(let line of stringArrayArg){
2016-07-07 17:53:14 +00:00
resultString = resultString + line + "\n";
2016-07-06 08:04:25 +00:00
};
2016-07-07 17:53:14 +00:00
return resultString;
2016-07-07 21:35:15 +00:00
};
let cleanStringArray = (stringArrayArg:string[]):string[] => {
let testRegex = /^[\s]*$/;
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
}
export let indent = (stringArg:string,spaceAmount:number):string => {
2016-07-07 17:53:14 +00:00
let resultString:string;
return resultString;
2016-07-06 08:04:25 +00:00
};
export let indentWithPrefix = (stringArg:string,prefixArg:string):string => {
2016-07-07 17:53:14 +00:00
let resultString:string;
2016-07-07 22:57:11 +00:00
let stringArray = splitString(stringArg);
let resultArray:string[] = [];
for(let stringItem of stringArray){
resultArray.push(prefixArg + stringItem);
};
resultString = joinString(resultArray);
2016-07-07 17:53:14 +00:00
return resultString;
2016-07-06 08:04:25 +00:00
};
2016-07-07 22:57:11 +00:00
export let normalizeIndent = (stringArg:string):string => {
2016-07-07 17:53:14 +00:00
let resultString:string;
let splitStringArray:string[] = splitString(stringArg);
let minCommonLeftOffset:number;
let deIndentRegex = /^(\s*)/;
for(let stringItem of splitStringArray){
let offsetString = deIndentRegex.exec(stringItem)[1];
if (typeof minCommonLeftOffset == "undefined" || offsetString.length < minCommonLeftOffset){
minCommonLeftOffset = offsetString.length;
};
};
let resultSplitStringArray = [];
for(let stringItem of splitStringArray){
resultSplitStringArray.push(stringItem.substr(minCommonLeftOffset));
};
resultString = joinString(resultSplitStringArray);
return resultString;
2016-07-06 08:04:25 +00:00
}