|
| 1 | +/** |
| 2 | + * Defines a builder for constructing a string from values. |
| 3 | + */ |
| 4 | +export class StringBuilder { |
| 5 | + private _strings: string[]; |
| 6 | + |
| 7 | + /** |
| 8 | + * Initializes a new instance of the StringBuilder class with an optional starting string value. |
| 9 | + * @param value The optional starting string value. |
| 10 | + */ |
| 11 | + constructor(value: string | StringBuilder | null = null) { |
| 12 | + this._strings = []; |
| 13 | + this.append(value); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Appends the value to this instance. |
| 18 | + * @param value The value to append. |
| 19 | + * @returns {StringBuilder} A reference to this instance after the append operation has completed. |
| 20 | + */ |
| 21 | + public append(value: string | StringBuilder | null): StringBuilder { |
| 22 | + if (value === undefined || value === null) { |
| 23 | + return this; |
| 24 | + } |
| 25 | + |
| 26 | + if (value instanceof StringBuilder) { |
| 27 | + this._strings.push(...value._strings); |
| 28 | + } else { |
| 29 | + if (/^\s*$/.test(value)) { |
| 30 | + return this; |
| 31 | + } |
| 32 | + |
| 33 | + this._strings.push(value); |
| 34 | + } |
| 35 | + |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Inserts the value to this instance at the specified index. |
| 41 | + * @param index The position in this instance where the value should be inserted. |
| 42 | + * @param value The value to insert. |
| 43 | + * @returns {StringBuilder} A reference to this instance after the insert operation has completed. |
| 44 | + */ |
| 45 | + public insert(index: number, value: string | StringBuilder | null): StringBuilder { |
| 46 | + if (value === undefined || value === null) { |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + if (value instanceof StringBuilder) { |
| 51 | + this._strings.splice(index, 0, ...value._strings); |
| 52 | + } else { |
| 53 | + if (/^\s*$/.test(value)) { |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + this._strings.splice(index, 0, value); |
| 58 | + } |
| 59 | + |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Replaces all occurrences of a specified string or value based on a regular expression in this instance with another specified string. |
| 65 | + * @param search The string or regular expression to find values to replace. |
| 66 | + * @param replace The string to replace values with. |
| 67 | + * @returns {StringBuilder} A reference to this instance after the replace operation has completed. |
| 68 | + */ |
| 69 | + public replace(search: string | RegExp, replace: string): StringBuilder { |
| 70 | + for (let i = 0; i < this._strings.length; i++) { |
| 71 | + this._strings[i] = this._strings[i].replace(search, replace); |
| 72 | + } |
| 73 | + |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Removes the specified range of characters from this instance. |
| 79 | + * @param startIndex The zero-based position in this instance where removal begins. |
| 80 | + * @param length The number of characters to remove. |
| 81 | + * @returns {StringBuilder} A reference to this instance after the remove operation has completed. |
| 82 | + */ |
| 83 | + public remove(startIndex: number, length: number): void { |
| 84 | + this._strings.splice(startIndex, length); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Checks whether this instance is empty. |
| 89 | + * @returns {boolean} True if this instance is empty; otherwise, false. |
| 90 | + */ |
| 91 | + public isEmpty(): boolean { |
| 92 | + return this._strings.length === 0; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Clears all strings from this instance. |
| 97 | + */ |
| 98 | + public clear(): void { |
| 99 | + this._strings = []; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Returns a string representation of the value of this instance with a join. |
| 104 | + * @returns {string} The strings with a join. |
| 105 | + */ |
| 106 | + public toString(join: string): string { |
| 107 | + return this._strings.join(join); |
| 108 | + } |
| 109 | +} |
0 commit comments