AK-21/Graphite-Industrial-Intelligence
0
1const tab = 9 /* `\t` */2const space = 32 /* ` ` */3 4/**5 * Remove initial and final spaces and tabs at the line breaks in `value`.6 * Does not trim initial and final spaces and tabs of the value itself.7 *8 * @param {string} value9 * Value to trim.10 * @returns {string}11 * Trimmed value.12 */13export function trimLines(value) {14 const source = String(value)15 const search = /\r?\n|\r/g16 let match = search.exec(source)17 let last = 018 /** @type {Array<string>} */19 const lines = []20 21 while (match) {22 lines.push(23 trimLine(source.slice(last, match.index), last > 0, true),24 match[0]25 )26 27 last = match.index + match[0].length28 match = search.exec(source)29 }30 31 lines.push(trimLine(source.slice(last), last > 0, false))32 33 return lines.join('')34}35 36/**37 * @param {string} value38 * Line to trim.39 * @param {boolean} start40 * Whether to trim the start of the line.41 * @param {boolean} end42 * Whether to trim the end of the line.43 * @returns {string}44 * Trimmed line.45 */46function trimLine(value, start, end) {47 let startIndex = 048 let endIndex = value.length49 50 if (start) {51 let code = value.codePointAt(startIndex)52 53 while (code === tab || code === space) {54 startIndex++55 code = value.codePointAt(startIndex)56 }57 }58 59 if (end) {60 let code = value.codePointAt(endIndex - 1)61 62 while (code === tab || code === space) {63 endIndex--64 code = value.codePointAt(endIndex - 1)65 }66 }67 68 return endIndex > startIndex ? value.slice(startIndex, endIndex) : ''69}70 