1 | import { EOL } from 'os';
|
2 |
|
3 | const DEFAULT_MAX_SIZE = 2048;
|
4 |
|
5 | export class StringBuilder {
|
6 | private currentLength = 0;
|
7 | private readonly strings: string[] = [];
|
8 | private readonly maxSize = DEFAULT_MAX_SIZE;
|
9 |
|
10 | public append(str: string): void {
|
11 | this.strings.push(str);
|
12 | this.currentLength += str.length;
|
13 | while (this.currentLength > this.maxSize && this.strings.length > 1) {
|
14 | const shifted = this.strings.shift()!;
|
15 | this.currentLength -= shifted.length;
|
16 | }
|
17 | }
|
18 |
|
19 | public toString(): string {
|
20 | return this.strings.join('');
|
21 | }
|
22 |
|
23 | public static concat(...builders: StringBuilder[]): string {
|
24 | return builders
|
25 | .map((b) => b.toString())
|
26 | .filter(Boolean)
|
27 | .join(EOL);
|
28 | }
|
29 | }
|