@@ -9,8 +9,7 @@ class StringBuilder {
99 constructor ( str : string , capacity : number )
1010
1111 constructor ( str ?: string , capacity ?: number ) {
12- if ( typeof str === 'string' || typeof str === 'boolean' || typeof str === 'number' ) this . _str = String ( str )
13- else this . _str = ''
12+ this . _str = String ( str ?? '' )
1413 if ( typeof capacity === 'number' && isFinite ( capacity ) && capacity > - 1 ) {
1514 this . _cap = capacity
1615 this . _str = this . _str . slice ( undefined , capacity )
@@ -20,11 +19,12 @@ class StringBuilder {
2019 insert ( index : number , value : number | string | boolean , repeatCount : number = 1 ) : boolean {
2120 if ( index > - 1 && index < this . _str . length ) {
2221 value = value . toString ( )
23- let repeated = ''
24- for ( ; repeatCount > 0 ; repeatCount -- ) repeated = repeated . concat ( value )
25- const inserted = this . _str . slice ( undefined , index ) + repeated + this . _str . slice ( index )
26- if ( this . checkCapacity ( inserted ) ) {
27- this . _str = inserted
22+ const operating = [ this . _str . slice ( undefined , index ) ]
23+ for ( ; repeatCount > 0 ; repeatCount -- ) operating . push ( value )
24+ operating . push ( this . _str . slice ( index ) )
25+ const insertion = operating . join ( '' )
26+ if ( this . checkCapacity ( insertion . length ) ) {
27+ this . _str = insertion
2828 return true
2929 }
3030 return false
@@ -33,10 +33,10 @@ class StringBuilder {
3333
3434 append ( value : number | string | boolean , repeatCount : number = 1 ) : boolean {
3535 value = value . toString ( )
36- let repeated = ''
37- for ( ; repeatCount > 0 ; repeatCount -- ) repeated = repeated . concat ( value )
38- const appended = this . _str . concat ( repeated )
39- if ( this . checkCapacity ( appended ) ) {
36+ const operating = [ this . _str ]
37+ for ( ; repeatCount > 0 ; repeatCount -- ) operating . push ( value )
38+ const appended = operating . join ( '' )
39+ if ( this . checkCapacity ( appended . length ) ) {
4040 this . _str = appended
4141 return true
4242 }
@@ -45,121 +45,124 @@ class StringBuilder {
4545
4646 prepend ( value : number | string | boolean , repeatCount : number = 1 ) : boolean {
4747 value = value . toString ( )
48- let repeated = ''
49- for ( ; repeatCount > 0 ; repeatCount -- ) repeated = repeated . concat ( value )
50- const prepended = repeated . toString ( ) . concat ( this . _str )
51- if ( this . checkCapacity ( prepended ) ) {
48+ const operating = [ ]
49+ for ( ; repeatCount > 0 ; repeatCount -- ) operating . push ( value )
50+ operating . push ( this . _str )
51+ const prepended = operating . join ( '' )
52+ if ( this . checkCapacity ( prepended . length ) ) {
5253 this . _str = prepended
5354 return true
5455 }
5556 return false
5657 }
5758
5859 appendNewLine ( num : number = 1 ) : boolean {
59- let lineBreaks = ''
60- for ( ; num > 0 ; num -- ) lineBreaks = lineBreaks . concat ( '\u000a' )
61- const appended = this . _str . concat ( lineBreaks )
62- if ( this . checkCapacity ( appended ) ) {
60+ const operating = [ this . _str ]
61+ for ( ; num > 0 ; num -- ) operating . push ( '\u000a' )
62+ const appended = operating . join ( '' )
63+ if ( this . checkCapacity ( appended . length ) ) {
6364 this . _str = appended
6465 return true
6566 }
6667 return false
6768 }
6869
6970 prependNewLine ( num : number = 1 ) : boolean {
70- let lineBreaks = ''
71- for ( ; num > 0 ; num -- ) lineBreaks = lineBreaks . concat ( '\u000a' )
72- const prepended = lineBreaks . concat ( this . _str )
73- if ( this . checkCapacity ( prepended ) ) {
71+ const operating = [ ]
72+ for ( ; num > 0 ; num -- ) operating . push ( '\u000a' )
73+ operating . push ( this . _str )
74+ const prepended = operating . join ( '' )
75+ if ( this . checkCapacity ( prepended . length ) ) {
7476 this . _str = prepended
7577 return true
7678 }
7779 return false
7880 }
7981
8082 appendSpace ( num : number = 1 ) : boolean {
81- let spaces = ''
82- for ( ; num > 0 ; num -- ) spaces = spaces . concat ( '\u0020' )
83- const appended = this . _str . concat ( spaces )
84- if ( this . checkCapacity ( appended ) ) {
83+ const operating = [ this . _str ]
84+ for ( ; num > 0 ; num -- ) operating . push ( '\u0020' )
85+ const appended = operating . join ( '' )
86+ if ( this . checkCapacity ( appended . length ) ) {
8587 this . _str = appended
8688 return true
8789 }
8890 return false
8991 }
9092
9193 prependSpace ( num : number = 1 ) : boolean {
92- let spaces = ''
93- for ( ; num > 0 ; num -- ) spaces = spaces . concat ( '\u0020' )
94- const prepended = spaces . concat ( this . _str )
95- if ( this . checkCapacity ( prepended ) ) {
94+ const operating = [ ]
95+ for ( ; num > 0 ; num -- ) operating . push ( '\u0020' )
96+ operating . push ( this . _str )
97+ const prepended = operating . join ( '' )
98+ if ( this . checkCapacity ( prepended . length ) ) {
9699 this . _str = prepended
97100 return true
98101 }
99102 return false
100103 }
101104
102105 appendArray ( arr : Array < number | string | boolean > , separator : string = '' ) : boolean {
103- const merged = arr . join ( separator )
104- const appended = this . _str . concat ( merged )
105- if ( this . checkCapacity ( appended ) ) {
106+ const appended = this . _str . concat ( arr . join ( separator ) )
107+ if ( this . checkCapacity ( appended . length ) ) {
106108 this . _str = appended
107109 return true
108110 }
109111 return false
110112 }
111113
112114 prependArray ( arr : Array < number | string | boolean > , separator : string = '' ) : boolean {
113- const merged = arr . join ( separator )
114- const prepended = merged . concat ( this . _str )
115- if ( this . checkCapacity ( prepended ) ) {
115+ const prepended = arr . join ( separator ) . concat ( this . _str )
116+ if ( this . checkCapacity ( prepended . length ) ) {
116117 this . _str = prepended
117118 return true
118119 }
119120 return false
120121 }
121122
122123 appendJSON ( json : Record < any , any > | Array < Record < any , any > > , space ?: string | number ) : boolean {
123- let appended = ''
124+ const operating = [ this . _str ]
124125 if ( Array . isArray ( json ) ) {
125- json . forEach ( ( value ) => ( appended = appended . concat ( JSON . stringify ( value , undefined , space ) ) ) )
126+ json . forEach ( ( value ) => operating . push ( JSON . stringify ( value , undefined , space ) ) )
126127 } else {
127- appended = appended . concat ( JSON . stringify ( json , undefined , space ) )
128+ operating . push ( JSON . stringify ( json , undefined , space ) )
128129 }
129- if ( this . checkCapacity ( this . _str + appended ) ) {
130- this . _str = this . _str . concat ( appended )
130+ const appended = operating . join ( '' )
131+ if ( this . checkCapacity ( appended . length ) ) {
132+ this . _str = appended
131133 return true
132134 }
133135 return false
134136 }
135137
136138 prependJSON ( json : Record < any , any > | Array < Record < any , any > > , space ?: string | number ) : boolean {
137- let prepended = ''
139+ const operating = [ ]
138140 if ( Array . isArray ( json ) ) {
139- json . forEach ( ( value ) => ( prepended = JSON . stringify ( value , undefined , space ) . concat ( prepended ) ) )
141+ json . forEach ( ( value ) => operating . push ( JSON . stringify ( value , undefined , space ) ) )
140142 } else {
141- prepended = prepended . concat ( JSON . stringify ( json , undefined , space ) )
143+ operating . push ( JSON . stringify ( json , undefined , space ) )
142144 }
143- if ( this . checkCapacity ( prepended + this . _str ) ) {
144- this . _str = prepended . concat ( this . _str )
145+ const prepended = operating . join ( '' ) + this . _str
146+ if ( this . checkCapacity ( prepended . length ) ) {
147+ this . _str = prepended
145148 return true
146149 }
147150 return false
148151 }
149152
150153 appendCodePoint ( ...codePoints : number [ ] ) : boolean {
151- const appended = String . fromCodePoint ( ...codePoints )
152- if ( this . checkCapacity ( this . _str + appended ) ) {
153- this . _str = this . _str . concat ( appended )
154+ const appended = this . _str + String . fromCodePoint ( ...codePoints )
155+ if ( this . checkCapacity ( appended . length ) ) {
156+ this . _str = appended
154157 return true
155158 }
156159 return false
157160 }
158161
159162 prependCodePoint ( ...codePoints : number [ ] ) : boolean {
160- const prepended = String . fromCodePoint ( ...codePoints )
161- if ( this . checkCapacity ( prepended + this . _str ) ) {
162- this . _str = prepended . concat ( this . _str )
163+ const prepended = String . fromCodePoint ( ...codePoints ) + this . _str
164+ if ( this . checkCapacity ( prepended . length ) ) {
165+ this . _str = prepended
163166 return true
164167 }
165168 return false
@@ -171,17 +174,18 @@ class StringBuilder {
171174 // If str is string else it is replacer
172175 if ( typeof str === 'string' ) {
173176 // If capacity is -1 OR capacity is more than equal to given str
174- if ( this . checkCapacity ( str ) ) {
177+ if ( this . checkCapacity ( str . length ) ) {
175178 this . _str = str
176179 return true
177180 }
178181 } else {
179- let replacementString : string = ''
182+ const operating : string [ ] = [ ]
180183 for ( let i = 0 ; i < this . _str . length ; i ++ ) {
181- replacementString += str ( this . _str [ i ] , i , this . _str )
184+ operating . push ( str ( this . _str [ i ] , i , this . _str ) )
182185 }
186+ const replacementString = operating . join ( '' )
183187 // If capacity is -1 OR capacity is more than equal to replacementString
184- if ( this . checkCapacity ( replacementString ) ) {
188+ if ( this . checkCapacity ( replacementString . length ) ) {
185189 this . _str = replacementString
186190 return true
187191 }
@@ -199,7 +203,7 @@ class StringBuilder {
199203 if ( typeof str === 'string' ) {
200204 replacementString = this . _str . slice ( undefined , start ) + str + this . _str . slice ( end )
201205 // If capacity is -1 OR capacity is more than equal to replacementString
202- if ( this . checkCapacity ( replacementString ) ) {
206+ if ( this . checkCapacity ( replacementString . length ) ) {
203207 this . _str = replacementString
204208 return true
205209 }
@@ -209,7 +213,7 @@ class StringBuilder {
209213 }
210214 replacementString = this . _str . slice ( undefined , start ) + replacementString + this . _str . slice ( end )
211215 // If capacity is -1 OR capacity is more than equal to replacementString
212- if ( this . checkCapacity ( replacementString ) ) {
216+ if ( this . checkCapacity ( replacementString . length ) ) {
213217 this . _str = replacementString
214218 return true
215219 }
@@ -218,15 +222,15 @@ class StringBuilder {
218222 }
219223
220224 setString ( value : string ) : boolean {
221- if ( this . checkCapacity ( value ) ) {
225+ if ( this . checkCapacity ( value . length ) ) {
222226 this . _str = value
223227 return true
224228 }
225229 return false
226230 }
227231
228- private checkCapacity ( optString : string ) : boolean {
229- return this . _cap === - 1 || this . _cap >= optString . length
232+ private checkCapacity ( newStrLen : number ) : boolean {
233+ return this . _cap === - 1 || this . _cap >= newStrLen
230234 }
231235
232236 getString ( ) : string {
@@ -277,12 +281,16 @@ class StringBuilder {
277281 if ( typeof value === 'number' && isFinite ( value ) && value > - 1 ) this . _str = this . _str . slice ( undefined , value )
278282 }
279283
280- // removes string
284+ /**
285+ * Only removes the string.
286+ */
281287 clear ( ) : void {
282288 this . _str = ''
283289 }
284290
285- // removes string & capacity
291+ /**
292+ * Removes string & capacity.
293+ */
286294 reset ( ) : void {
287295 this . _str = ''
288296 this . _cap = - 1
0 commit comments