smartstring/ts/smartstring.indent.ts

65 lines
2.0 KiB
TypeScript
Raw Normal View History

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