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