-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsb.ts
More file actions
84 lines (75 loc) · 1.87 KB
/
Copy pathsb.ts
File metadata and controls
84 lines (75 loc) · 1.87 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* A lightweight StringBuilder
*/
export class StringBuilder {
private parts: string[];
/**
* Creates a new StringBuilder instance
* @param initialValue Optional initial string value
*/
constructor(initialValue = "") {
this.parts = initialValue ? [initialValue] : [];
}
/**
* Appends a string to the builder
* @param str The string to append
* @returns The StringBuilder instance for chaining
*/
append(str: string): StringBuilder {
this.parts.push(str);
return this;
}
/**
* Clears all content from the builder
* @returns The StringBuilder instance for chaining
*/
clear(): StringBuilder {
this.parts = [];
return this;
}
/**
* Appends a string followed by a newline character
* @param str The string to append
* @returns The StringBuilder instance for chaining
*/
appendLine(str = ""): StringBuilder {
this.parts.push(`${str}\n`);
return this;
}
/**
* Returns the current length of the string
*/
get length(): number {
return this.toString().length;
}
/**
* Converts the StringBuilder to a string
* @returns The built string
*/
toString(): string {
return this.parts.join("");
}
/**
* Checks if a string contains or ends with line breaks
* @param str The string to check
* @returns True if the string contains any line breaks
*/
static isMultiline(str: string): boolean {
// Count all line breaks in the string
let lineBreakCount = 0;
for (let i = 0; i < str.length; i++) {
// Check for \n (Line Feed)
if (str[i] === "\n") {
lineBreakCount++;
}
// Check for \r (Carriage Return) not followed by \n (to avoid double counting \r\n)
else if (
str[i] === "\r" &&
(i === str.length - 1 || str[i + 1] !== "\n")
) {
lineBreakCount++;
}
}
return lineBreakCount > 0;
}
}