@@ -8,30 +8,30 @@ An easy and fast in-memory string builder for Node.js.
8
8
## Code Example
9
9
10
10
``` javascript
11
- const StringBuilder = require (' node-stringbuilder' );
12
- var sb = new StringBuilder (' Hi ' );
13
- sb .appendLine (' , ' ).append (' This is a simple example demonstrating how to use this module.' );
11
+ const StringBuilder = require (" node-stringbuilder" );
12
+ const sb = new StringBuilder (" Hi " );
13
+ sb .appendLine (" , " ).append (" This is a simple example demonstrating how to use this module." );
14
14
console .log (sb .toString ()); // Hi,
15
15
// This is a simple example demonstrating how to use this module.
16
- sb .insert (' Text can be added into any position of this builder.' );
17
- sb .replace (53 , 118 , ' Or replace the existing text.' );
16
+ sb .insert (" Text can be added into any position of this builder." );
17
+ sb .replace (53 , 118 , " Or replace the existing text." );
18
18
console .log (sb .toString ()); // Text can be added into any position of this builder.HOr replace the existing text.
19
- sb .deleteCharAt (52 ).insert (52 , ' ' );
19
+ sb .deleteCharAt (52 ).insert (52 , " " );
20
20
console .log (sb .toString ()); // Text can be added into any position of this builder. Or replace the existing text.
21
- sb .toLowerCase ().replaceAll (' text' , ' string' );
21
+ sb .toLowerCase ().replaceAll (" text" , " string" );
22
22
console .log (sb .toString ()); // string can be added into any position of this builder. or replace the existing string.
23
23
console .log (sb .clone ().reverse ().toString ()); // .gnirts gnitsixe eht ecalper ro .redliub siht fo noitisop yna otni dedda eb nac gnirts
24
24
console .log (sb .toString (0 , 19 )); // string can be added
25
25
console .log (sb .length ()); // 86
26
26
console .log (sb .count ()); // 15
27
- console .log (sb .indexOf (' is ' )); // Uint32Array [ 43, 72 ]
28
- console .log (sb .indexOfSkip (' is ' )); // Uint32Array [ 43, 72 ]
29
- console .log (sb .lastIndexOf (' is ' )); // Uint32Array [ 72, 43 ]
27
+ console .log (sb .indexOf (" is " )); // Uint32Array [ 43, 72 ]
28
+ console .log (sb .indexOfSkip (" is " )); // Uint32Array [ 43, 72 ]
29
+ console .log (sb .lastIndexOf (" is " )); // Uint32Array [ 72, 43 ]
30
30
console .log (sb .indexOfRegExp (/ is/ g )); // { index: [ 43, 72 ], lastIndex: [ 45, 74 ] }
31
- console .log (sb .repeat ().indexOf (' is ' )); // Uint32Array [ 43, 72, 129, 158 ]
31
+ console .log (sb .repeat ().indexOf (" is " )); // Uint32Array [ 43, 72, 129, 158 ]
32
32
sb .substring (11 , 37 );
33
33
console .log (sb .toString ()); // be added into any position
34
- console .log (sb .equalsIgnoreCase (' be Added into Any position' )); // true
34
+ console .log (sb .equalsIgnoreCase (" be Added into Any position" )); // true
35
35
console .log (sb .toBuffer ()); // UTF-8 encoded
36
36
```
37
37
@@ -42,7 +42,7 @@ console.log(sb.toBuffer()); // UTF-8 encoded
42
42
* Multiple types of data are allowed to input.
43
43
* Strings
44
44
* Buffers(UTF-8 encoded)
45
- * Instances of this StringBuilder module
45
+ * Instances of this ` StringBuilder ` module
46
46
* ReadStream(to read file)
47
47
* Numbers, booleans, other objects
48
48
* Fast string search algorithm([ Boyer-Moore-MagicLen] ( https://magiclen.org/boyer-moore-magiclen/ ) )
@@ -58,50 +58,50 @@ Import this module by using `require` function.
58
58
const StringBuilder = require (' node-stringbuilder' );
59
59
```
60
60
61
- Use ` new ` operator or ` from ` function to create a StringBuilder instance.
61
+ Use ` new ` operator or ` from ` function to create a ` StringBuilder ` instance.
62
62
63
63
``` javascript
64
- var sb1 = new StringBuilder ();
65
- // or
66
- var sb2 = StringBuilder .from ();
64
+ const sb1 = new StringBuilder ();
65
+ // or
66
+ const sb2 = StringBuilder .from ();
67
67
```
68
68
69
- When creating an instance of StringBuilder, you can initialize the text and capacity.
69
+ When creating an instance of ` StringBuilder ` , you can initialize the text and capacity.
70
70
71
71
``` javascript
72
- var sb = StringBuilder .from (' First' , 4096 );
72
+ const sb = StringBuilder .from (" First" , 4096 );
73
73
```
74
74
75
- By default, a block of buffer space used by StringBuilder is 128 characters. The space of the buffer can be expanded or shrinked by blocks.
75
+ By default, a block of buffer space used by ` StringBuilder ` is 128 characters. The space of the buffer can be expanded or shrinked by blocks.
76
76
77
77
``` javascript
78
78
// To expand
79
- var newCapacity = 65536 ;
79
+ const newCapacity = 65536 ;
80
80
sb .expandCapacity (newCapacity);
81
81
// To shrink
82
82
sb .shrinkCapacity ();
83
83
```
84
84
85
- If some text are added into StringBuilder, StringBuilder will check its space. And if the space is too small, it will re-alloc a bigger one automatically. This re-allocation has overheads, if it does this frequently, your program may be slowed down. Therefore, if you can predict the length of your text, please set the capacity when creating a StringBuilder instance.
85
+ If some text are added into ` StringBuilder ` , ` StringBuilder ` will check its space. And if the space is too small, it will re-alloc a bigger one automatically. This re-allocation has overheads, if it does this frequently, your program may be slowed down. Therefore, if you can predict the length of your text, please set the capacity when creating a ` StringBuilder ` instance.
86
86
87
87
### Append
88
88
89
89
Concat text.
90
90
91
91
``` javascript
92
- sb .append (' string' ).append (123 ).append (false ).append (fs .createReadStream (path));
92
+ sb .append (" string" ).append (123 ).append (false ).append (fs .createReadStream (path));
93
93
```
94
94
95
95
Add a new line after append.
96
96
97
97
``` javascript
98
- sb .appendLine (' string' );
98
+ sb .appendLine (" string" );
99
99
```
100
100
101
101
Append text repeatedly.
102
102
103
103
``` javascript
104
- sb .appendRepeat (' string' , 3 );
104
+ sb .appendRepeat (" string" , 3 );
105
105
```
106
106
107
107
Append a file asynchronizely.
@@ -115,29 +115,34 @@ await sb.appendReadStream(fs.createReadStream(path));
115
115
Insert text to any position.
116
116
117
117
``` javascript
118
- sb .insert (' string' ); // To the head.
119
- sb .insert (5 , ' string' );
118
+ sb .insert (" string" ); // To the head.
119
+ sb .insert (5 , " string" );
120
120
```
121
121
122
122
### Replace
123
123
124
124
Replace text to the position in a range of index.
125
125
126
126
``` javascript
127
- sb .replace (4 , 15 , ' string' );
127
+ sb .replace (4 , 15 , " string" );
128
128
```
129
129
130
130
Replace existing substrings to another.
131
131
132
132
``` javascript
133
- sb .replacePattern (' old' , ' new' );
134
- sb .replacePattern (' old' , ' new' , offset, limit);
133
+ sb .replacePattern (" old" , " new" );
134
+ sb .replacePattern (
135
+ " old" ,
136
+ " new" ,
137
+ offset,
138
+ limit
139
+ );
135
140
```
136
141
137
142
Replace all existing substrings to another.
138
143
139
144
``` javascript
140
- sb .replaceAll (' old' , ' new' );
145
+ sb .replaceAll (" old" , " new" );
141
146
```
142
147
143
148
### Delete
@@ -207,90 +212,90 @@ sb.repeat(1);
207
212
208
213
### Expand Capacity
209
214
210
- Expand the capacity of this StringBuilder.
215
+ Expand the capacity of this ` StringBuilder ` .
211
216
212
217
``` javascript
213
- sb .expandCapacity (4096 ).append (' string' );
218
+ sb .expandCapacity (4096 ).append (" string" );
214
219
```
215
220
216
221
Expand and get the updated capacity,
217
222
218
223
``` javascript
219
- var capacity = sb .expandCapacity (4096 , true );
224
+ const capacity = sb .expandCapacity (4096 , true );
220
225
```
221
226
222
227
### Shrink Capacity
223
228
224
- Shrink the capacity of this StringBuilder.
229
+ Shrink the capacity of this ` StringBuilder ` .
225
230
226
231
``` javascript
227
- sb .shrinkCapacity ().clone ().append (' string' );
232
+ sb .shrinkCapacity ().clone ().append (" string" );
228
233
```
229
234
230
235
Shrink and get the updated capacity,
231
236
232
237
``` javascript
233
- var capacity = sb .shrinkCapacity (true );
238
+ const capacity = sb .shrinkCapacity (true );
234
239
```
235
240
236
241
### Get Current Text Length
237
242
238
- To get the length of this StringBuilder,
243
+ To get the length of this ` StringBuilder ` ,
239
244
240
245
``` javascript
241
- var length = sb .length ();
246
+ const length = sb .length ();
242
247
```
243
248
244
249
### Get Current Capacity
245
250
246
- To get the length of this StringBuilder,
251
+ To get the length of this ` StringBuilder ` ,
247
252
248
253
``` javascript
249
- var capacity = sb .capacity ();
254
+ const capacity = sb .capacity ();
250
255
```
251
256
252
257
### Count the words
253
258
254
259
To count the words,
255
260
256
261
``` javascript
257
- var words = sb .count ();
262
+ const words = sb .count ();
258
263
```
259
264
260
265
### Build String
261
266
262
267
Build a string of a specific range of index.
263
268
264
269
``` javascript
265
- var str = sb .toString (4 , 10 );
270
+ const str = sb .toString (4 , 10 );
266
271
```
267
272
268
273
Build a UTF-8 buffer of a specific range of index.
269
274
270
275
``` javascript
271
- var buffer = sb .toBuffer (4 , 10 );
276
+ const buffer = sb .toBuffer (4 , 10 );
272
277
```
273
278
274
279
To get the full text,
275
280
276
281
``` javascript
277
- var text = sb .toString ();
278
- var buffer = sb .toBuffer ();
282
+ const text = sb .toString ();
283
+ const buffer = sb .toBuffer ();
279
284
```
280
285
281
286
To get one character at a specific index,
282
287
283
288
``` javascript
284
- var c = sb .charAt (4 );
289
+ const c = sb .charAt (4 );
285
290
```
286
291
287
292
### Search String
288
293
289
294
Search substrings from the head,
290
295
291
296
``` javascript
292
- var indexArray = sb .indexOf (' string' );
293
- var indexArray2 = sb .indexOf (' string' , offset, limit);
297
+ const indexArray = sb .indexOf (" string" );
298
+ const indexArray2 = sb .indexOf (" string" , offset, limit);
294
299
```
295
300
296
301
Search substrings from the head by using RegExp,
@@ -302,38 +307,38 @@ var indexArray = sb.indexOf(/string/g);
302
307
Search substrings from the end,
303
308
304
309
``` javascript
305
- var indexArray = sb .lastIndexOf (' string' );
310
+ const indexArray = sb .lastIndexOf (" string" );
306
311
```
307
312
308
313
### Equals
309
314
310
315
Determine whether the two strings are the same.
311
316
312
317
``` javascript
313
- var equal = sb .equals (' string' );
318
+ const equal = sb .equals (" string" );
314
319
```
315
320
316
321
To ignore the case of letters,
317
322
318
323
``` javascript
319
- var equal = sb .equalsIgnoreCase (' string' );
324
+ const equal = sb .equalsIgnoreCase (" string" );
320
325
```
321
326
322
327
Determine whether it starts or ends with a specific pattern.
323
328
324
329
``` javascript
325
- var start = sb .startsWith (' string' );
326
- var end = sb .endsWith (' string' );
330
+ const start = sb .startsWith (" string" );
331
+ const end = sb .endsWith (" string" );
327
332
```
328
333
329
334
RegExp is not supported in ` startsWith ` and ` endsWith ` methods.
330
335
331
336
### Clone
332
337
333
- Clone this StringBuilder.
338
+ Clone this ` StringBuilder ` .
334
339
335
340
``` javascript
336
- var newSB = sb .clone ();
341
+ const newSB = sb .clone ();
337
342
```
338
343
339
344
## Tests
@@ -424,10 +429,4 @@ According to the result of benchmark, if you just want to append a few different
424
429
425
430
## License
426
431
427
- [ MIT] ( LICENSE )
428
-
429
- ## To Do
430
-
431
- * More test cases
432
-
433
- If you can help me do this as collaborators, I will be grateful.
432
+ [ MIT] ( LICENSE )
0 commit comments