-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf2.js
More file actions
359 lines (301 loc) · 8.11 KB
/
f2.js
File metadata and controls
359 lines (301 loc) · 8.11 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
'use strict';
var util = require('util');
var LRUDict = require('lru-dict');
var obusGet = require('obus/_get');
var obusParse = require('obus/parse');
var R_PATH = /(?:\(((?:"(?:\\[\s\S]|[^"])*"|'(?:\\[\s\S]|[^'])*'|[^()]+)+)\))/.source;
var R_SWAP = /([^0]\d*)\$/.source;
var R_SIGN = /([+-])?/.source;
var R_FILL = /(?:([\s\S]):)/.source;
var R_LENG = /(\d+)/.source;
var R_PREC = /(?:\.(\d+))/.source;
var R_TYPE = /([a-z])/.source;
var R_TEXT = /([^%]+)/.source;
var R_PERC = /(%)%?/.source;
var R_LEX = util.format('%(?:%s|%s|)(?:(?:%s)?%s?%s)?%s?%s|%s|%s',
R_PATH, R_SWAP, R_SIGN, R_FILL, R_LENG, R_PREC, R_TYPE, R_TEXT, R_PERC);
var LEX = new RegExp(R_LEX, 'g');
function TmplItem(type, text, path, index, sign, fill, width, precision, subType) {
/* eslint max-params: 1 */
this.type = type;
this.text = text;
this.path = path;
this.index = index;
this.sign = sign;
this.fill = fill;
this.width = width;
this.precision = precision;
this.subType = subType;
}
/**
* @class F2
* */
function F2() {
this.__cache = new LRUDict(255);
this.__types = {};
/**
* @public
* @memberOf {F2}
* @method
*
* @returns {String}
* */
this.format = this.__f2();
}
/**
* @public
* @static
* @memberOf {F2}
* @method
*
* @param {Object} [params]
*
* @returns {F2}
* */
F2.create = function (params) {
return new this(params);
};
/**
* @public
* @memberOf {F2}
* @method
*
* @param {String} name
* @param {Function} formatter
*
* @returns {F2}
* */
F2.prototype.type = function (name, formatter) {
if (typeof name !== 'string' || name.length !== 1) {
throw new TypeError('Type name should be a string character');
}
if (typeof formatter !== 'function') {
throw new TypeError('Type formatter should be a function');
}
this.__types[name] = formatter;
// reset cache
this.__cache.length = 0;
return this;
};
/**
* @public
* @memberOf {F2}
* @method
*
* @param {Array} args
* @param {Number} [offsetLeft]
* @param {Number} [offsetRight]
*
* @returns {String}
* */
F2.prototype.applyArgs = function (args, offsetLeft, offsetRight) {
return this.__applyArgs(args, offsetLeft >> 0, offsetRight >> 0);
};
/**
* @public
* @memberOf {F2}
* @method
*
* @param {String} f
* @param {Array} args
* @param {Number} [offsetLeft]
* @param {Number} [offsetRight]
*
* @returns {String}
* */
F2.prototype.applyArgsTo = function (f, args, offsetLeft, offsetRight) {
return this.__applyArgsTo(f, args, offsetLeft >> 0, offsetRight >> 0);
};
/**
* Check if the passed argument is pattern
*
* @public
* @memberOf {F2}
* @method
*
* @param {String} f
*
* @returns {Boolean}
* */
F2.prototype.isPattern = function (f) {
var tmpl = this.__pickTmpl(f);
return (tmpl.containsKwargs | tmpl.restArgsIndex) > 0;
};
/**
* Check if the passed pattern has key substitution
*
* @public
* @memberOf {F2}
* @method
*
* @param {String} f
* @param {String} name
*
* @returns {Boolean}
* */
F2.prototype.hasKeySub = function (f, name) {
var path = obusParse(name);
return this.__pickTmpl(f).items.some(function (item) {
return item.type === 'KEY' &&
path.every(function (node, i) {
return node === item.path[i];
});
});
};
/**
* Check if the passed pattern has index substitution
*
* @public
* @memberOf {F2}
* @method
*
* @param {String} f
* @param {Number} name
*
* @returns {Boolean}
* */
F2.prototype.hasPosSub = function (f, name) {
name -= 1;
return this.__pickTmpl(f).items.some(function (item) {
return item.type === 'POS' && item.index === name;
});
};
/**
* @protected
* @memberOf {F2}
* @method
*
* @returns {String}
* */
F2.prototype._inspect = function (v) {
return util.inspect(v);
};
F2.prototype.__applyArgs = function (args, offsetLeft, offsetRight) {
if (typeof args[offsetLeft] === 'string') {
return this.__applyArgsTo(args[offsetLeft], args, offsetLeft + 1, offsetRight);
}
return this.__appendRestArgs([], args, offsetLeft, offsetRight);
};
F2.prototype.__applyArgsTo = function (f, args, offsetLeft, offsetRight) {
var tmpl = this.__pickTmpl(f);
offsetRight += Number(tmpl.containsKwargs);
return this.__appendRestArgs([this.__substituteTmplItems(tmpl.items, args, offsetLeft, offsetRight)],
args, offsetLeft + tmpl.restArgsIndex, offsetRight);
};
F2.prototype.__pickTmpl = function (f) {
var tmpl = this.__cache.get(f);
if (!tmpl) {
tmpl = this.__parseF(f);
this.__cache.set(f, tmpl);
}
return tmpl;
};
F2.prototype.__parseF = function (f) {
/*eslint complexity: 0*/
var autoIndex = 0;
var containsKwargs = false;
var itemsCount = 0;
var m = null;
var restArgsIndex = -1;
var tmplItems = [];
LEX.lastIndex = 0;
/*eslint no-cond-assign: 0*/
while (m = LEX.exec(f)) {
if (!m[7] || typeof this.__types[m[7]] !== 'function') {
// text node (no type match, or no type formatter)
if (itemsCount > 0 && tmplItems[itemsCount - 1].type === 'TXT') {
// merge sibling text nodes
tmplItems[itemsCount - 1].text += m[8] || m[9] || m[0];
} else {
itemsCount = tmplItems.push(new TmplItem('TXT',
m[8] || m[9] || m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7]));
}
continue;
}
if (m[1]) {
// keyword argument
itemsCount = tmplItems.push(new TmplItem('KEY',
m[0], obusParse(m[1]), m[2], m[3], m[3], m[5], m[6], m[7]));
containsKwargs = true;
continue;
}
// positional arg
if (m[2]) {
// explicit index like `%1$s`
m[2] -= 1;
} else {
// implicit index like `%s`
m[2] = autoIndex;
autoIndex += 1;
}
restArgsIndex = Math.max(m[2], restArgsIndex);
itemsCount = tmplItems.push(new TmplItem('POS',
m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7]));
}
restArgsIndex += 1;
return {
containsKwargs: containsKwargs,
items: tmplItems,
restArgsIndex: restArgsIndex
};
};
F2.prototype.__appendRestArgs = function (parts, args, offsetLeft, offsetRight) {
var i = offsetLeft;
var l = args.length - offsetRight;
var j = parts.length;
while (i < l) {
parts[j] = this._inspect(args[i]);
j += 1;
i += 1;
}
return parts.join(' ');
};
F2.prototype.__substituteTmplItems = function (tmplItems, args, offsetLeft, offsetRight) {
var argc = args.length - offsetRight;
var tmplItem;
var tmplItemsCount = tmplItems.length;
var chunks = new Array(tmplItemsCount);
var subst;
while (tmplItemsCount) {
tmplItemsCount -= 1;
tmplItem = tmplItems[tmplItemsCount];
if (tmplItem.type === 'TXT') {
chunks[tmplItemsCount] = tmplItem.text;
continue;
}
if (tmplItem.type === 'KEY') {
subst = obusGet(args[argc], tmplItem.path);
} else
// tmplItem.type === 'POS'
if (tmplItem.index + offsetLeft < argc) {
// use positional arg if index fit to range
subst = args[tmplItem.index + offsetLeft];
} else {
// index is not fit to range
subst = undefined;
}
chunks[tmplItemsCount] = this.__formatPlaceholder(tmplItem, subst);
}
return chunks.join('');
};
F2.prototype.__formatPlaceholder = function (ph, subst) {
if (typeof subst === 'function') {
subst = subst();
}
return this.__types[ph.subType](subst, ph.sign, ph.fill, ph.width, ph.precision);
};
F2.prototype.__f2 = function () {
var self = this;
function format() {
// clone arguments to allow V8 optimize the function
var argc = arguments.length;
var args = new Array(argc);
while (argc) {
argc -= 1;
args[argc] = arguments[argc];
}
return self.__applyArgs(args, 0, 0);
}
return format;
};
module.exports = F2;