-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (165 loc) · 4.86 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict';
const READ = 1,
CHECK_ARR = 2,
NORMAL_END = 3,
PUSH_ANY = 4,
PUSH_ARR = 5,
PUSH_PAD = 6,
READ_AFTER_ANCHOR = 7,
PUSH_HASH = 8,
NEXT_ANCHOR = 9,
ABNORMAL_END = 10;
const isNotString = (arg) => {
return typeof(arg) !== 'string';
};
const isNotStringLengthOne = (arg) => {
return isNotString(arg) && arg.length !== 1;
};
const getFormatReader = (string) => {
let str = string, i = 0;
return () => {
return str.charAt(i++);
}
}
const EMPTY_STRING = "";
const DIGIT_REGEXP = /[\d]/g;
class IntFormat {
setData (num = "") {
if (num === null) {
num = "";
}
let tmp;
if (typeof num === 'string') {
tmp = num.match(DIGIT_REGEXP);
} else {
tmp = num.toString().match(DIGIT_REGEXP);
}
if (tmp === null) {
this.data = [];
} else {
this.data = tmp;
}
this.len = this.data.length;
}
constructor ({ format, padWith = EMPTY_STRING, sigil = '#', anchor = '\\' } = {}) {
this.sig = sigil;
this.pad = padWith;
this.anc = anchor;
if (typeof(arguments[0]) === 'string') {
this.f = arguments[0];
} else {
this.f = format;
}
}
setFormat (format) {
if (isNotString(format)) {
throw new TypeError('IntFormat.setFormat error: bad argument. Argument must be a string');
}
this.f = format;
return this;
}
setPad (padWith) {
this.pad = padWith;
return this;
}
setSigil (sigil) {
if (isNotStringLengthOne(sigil)) {
throw new TypeError('IntFormat.setSigil error: bad argument. Argument must be a string, length 1');
}
this.sig = sigil;
return this;
}
setAnchor (anchor) {
if (isNotStringLengthOne(anchor)) {
throw new TypeError('IntFormat.setAnchor error: bad argument. Argument must be a string, length 1');
}
this.anc = anchor;
return this;
}
format (num) {
if (!this.f) {
throw new TypeError(`format undefined`);
}
this.setData(num);
const read = getFormatReader(this.f);
let state = READ,
notFinished = true,
c,
out = [],
argCounter = 0;
do {
switch (state) {
case READ:
c = read();
switch (c) {
case this.sig:
state = CHECK_ARR;
break;
case this.anc:
state = READ_AFTER_ANCHOR;
break;
case EMPTY_STRING:
state = NORMAL_END;
break;
default:
state = PUSH_ANY;
break;
}
break;
case PUSH_ANY:
out.push(c);
state = READ;
break;
case CHECK_ARR:
if (argCounter < this.len) {
state = PUSH_ARR;
} else {
state = PUSH_PAD;
}
break;
case PUSH_ARR:
out.push(this.data[argCounter++]);
state = READ;
break;
case PUSH_PAD:
out.push(this.pad);
state = READ;
break;
case READ_AFTER_ANCHOR:
c = read();
switch (c) {
case this.sig:
state = PUSH_HASH;
break;
case this.anc:
state = NEXT_ANCHOR;
break;
case EMPTY_STRING:
state = ABNORMAL_END;
break;
default:
state = PUSH_ANY;
break;
}
break;
case PUSH_HASH:
out.push(this.sig);
state = READ;
break;
case NEXT_ANCHOR:
out.push(this.anc);
state = READ;
break;
case ABNORMAL_END:
out.push(this.anc);
state = NORMAL_END;
break;
case NORMAL_END:
notFinished = false;
break;
}
} while (notFinished);
return out.join('');
}
}
module.exports.IntFormat = IntFormat;