-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlib.ts
More file actions
30 lines (27 loc) · 1.13 KB
/
lib.ts
File metadata and controls
30 lines (27 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* Prepends a given count of whitespaces to every single line in a text.
* Lines with yaml seperators (---) will not be indented
* @param {string} text Text which should get some leading whitespaces on each line.
* @param {number} count The number of whitesspaces to prepend on each line of the returned string.
* @returns {string} Input Text, which has the given count of whitespaces prepended on each single line.
*/
export function prependWhitespacesOnEachLine(text: string, count: number): string {
if (count < 0) {
throw new Error("The count parameter is not a positive number")
}
const spaces = " ".repeat(count)
return text.replace(/^(?!---)/mg, spaces)
}
/**
* Removes the leading line break of the first element of an array.
* @param {RegExpMatchArray} delimiters Array for processing.
* @returns {RegExpMatchArray}
*/
export function removeLeadingLineBreakOfFirstElement(delimiters: RegExpMatchArray): RegExpMatchArray {
let firstDelimiter = delimiters.shift()
if (firstDelimiter) {
firstDelimiter = firstDelimiter.replace(/^\n/, "")
delimiters.unshift(firstDelimiter)
}
return delimiters
}