-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstringbuilder.js
71 lines (54 loc) · 1.5 KB
/
stringbuilder.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"use strict";
const Stream = require('stream').Stream;
const StringBuilder = function StringBuilder(v) {
this.s = [];
this.append(v);
Stream.call(this);
if (this.newline === undefined) {
const isWindows = process.platform === 'win32';
this.newline = isWindows ? '\r\n' : '\n';
}
};
StringBuilder.prototype.append = function (v) {
if (v != null) {
this.s.push(v);
}
return this;
};
StringBuilder.prototype.appendLine = function (v) {
this.s.push(this.newline);
if (v != null) {
this.s.push(v);
}
return this;
};
StringBuilder.prototype.appendFormat = function () {
const p = /({?){([^}]+)}(}?)/g;
let a = arguments, v = a[0], o = false;
if (a.length === 2) {
if (typeof a[1] == 'object' && a[1].constructor !== String) {
a = a[1];
o = true;
}
}
const s = v.split(p);
const r = [];
for (let i = 0; i < s.length; i += 4) {
r.push(s[i]);
if (s.length > i + 3) {
if (s[i + 1] === '{' && s[i + 3] === '}') {
r.push(s[i + 1], s[i + 2], s[i + 3]);
} else {
r.push(s[i + 1], a[o ? s[i + 2] : parseInt(s[i + 2], 10) + 1], s[i + 3]);
}
}
}
this.s.push(r.join(''));
};
StringBuilder.prototype.clear = function () {
this.s.length = 0;
};
StringBuilder.prototype.toString = function () {
return this.s.length === 0 ? '' : this.s.join('');
};
module.exports = StringBuilder;