Skip to content

Commit f9721e3

Browse files
committed
feat: ChainableStringBuilder
1 parent 4453dd1 commit f9721e3

3 files changed

Lines changed: 448 additions & 312 deletions

File tree

src/ChainableStringBuilder.ts

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import { StringBuilder } from '.'
2+
import type { Replacer } from '.'
3+
4+
class ChainableStringBuilder {
5+
private readonly sb: StringBuilder
6+
readonly UNSET_VALUE = -1
7+
8+
static fromStringBuilder(sb: StringBuilder): ChainableStringBuilder {
9+
return new ChainableStringBuilder(sb.getString(), sb.capacity)
10+
}
11+
12+
constructor(str?: string, capacity?: number) {
13+
this.sb = new StringBuilder(str ?? '', capacity ?? this.UNSET_VALUE)
14+
}
15+
16+
insert(index: number, value: number | string | boolean, repeatCount = 1): this {
17+
this.sb.insert(index, value, repeatCount)
18+
return this
19+
}
20+
21+
append(value: number | string | boolean, repeatCount = 1): this {
22+
this.sb.append(value, repeatCount)
23+
return this
24+
}
25+
26+
prepend(value: number | string | boolean, repeatCount = 1): this {
27+
this.sb.prepend(value, repeatCount)
28+
return this
29+
}
30+
31+
appendNewLine(num = 1): this {
32+
this.sb.appendNewLine(num)
33+
return this
34+
}
35+
36+
prependNewLine(num = 1): this {
37+
this.sb.prependNewLine(num)
38+
return this
39+
}
40+
41+
appendSpace(num = 1): this {
42+
this.sb.appendSpace(num)
43+
return this
44+
}
45+
46+
prependSpace(num = 1): this {
47+
this.sb.prependSpace(num)
48+
return this
49+
}
50+
51+
appendArray(arr: Array<number | string | boolean>, separator = ''): this {
52+
this.sb.appendArray(arr, separator)
53+
return this
54+
}
55+
56+
prependArray(arr: Array<number | string | boolean>, separator = ''): this {
57+
this.sb.prependArray(arr, separator)
58+
return this
59+
}
60+
61+
appendJSON(
62+
json: Record<string | number | symbol, unknown> | Array<Record<string | number | symbol, unknown>>,
63+
space?: string | number
64+
): this {
65+
this.sb.appendJSON(json, space)
66+
return this
67+
}
68+
69+
prependJSON(
70+
json: Record<string | number | symbol, unknown> | Array<Record<string | number | symbol, unknown>>,
71+
space?: string | number
72+
): this {
73+
this.sb.prependJSON(json, space)
74+
return this
75+
}
76+
77+
appendCodePoint(...codePoints: number[]): this {
78+
this.sb.appendCodePoint(...codePoints)
79+
return this
80+
}
81+
82+
prependCodePoint(...codePoints: number[]): this {
83+
this.sb.prependCodePoint(...codePoints)
84+
return this
85+
}
86+
87+
replaceSubstring(str: string | Replacer, start?: number, end?: number): this {
88+
this.sb.replaceSubstring(str, start, end)
89+
return this
90+
}
91+
92+
setString(value: string): this {
93+
this.sb.setString(value)
94+
return this
95+
}
96+
97+
trim(): this {
98+
this.sb.trim()
99+
return this
100+
}
101+
102+
clear(): this {
103+
this.sb.clear()
104+
return this
105+
}
106+
107+
reset(): this {
108+
this.sb.reset()
109+
return this
110+
}
111+
112+
clone(): ChainableStringBuilder {
113+
return new ChainableStringBuilder(this.sb.getString(), this.sb.capacity)
114+
}
115+
116+
build(): StringBuilder {
117+
return this.sb
118+
}
119+
120+
getString(): string {
121+
return this.sb.getString()
122+
}
123+
124+
get capacity(): number {
125+
return this.sb.capacity
126+
}
127+
128+
set capacity(value: number) {
129+
this.sb.capacity = value
130+
}
131+
132+
get length(): number {
133+
return this.sb.length
134+
}
135+
set length(value: number) {
136+
this.sb.length = value
137+
}
138+
}
139+
140+
export { ChainableStringBuilder }

0 commit comments

Comments
 (0)