-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringbuffer.js
57 lines (47 loc) · 992 Bytes
/
stringbuffer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* String buffer.
*/
class StringBuffer
{
/**
* Constructor.
*
* @constructor
*/
constructor()
{
// store components
this.buffer = [];
}
/**
* Inserts a string construction into buffer.
*
* @param {string} construction - construction to be added to buffer
*/
insert(construction)
{
this.buffer.push(...construction.split(''));
}
/**
* Clear buffer and returns previously bufferized string.
*
* @returns {string[]} - previously bufferized string
*/
flush()
{
// get current buffer content
const content = [...this.buffer];
// restart buffer
this.buffer = [];
return content;
}
/**
* Exposes the buffer current state.
*
* @returns {string[]} - current buffer state
*/
get state()
{
return this.buffer;
}
}