-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (57 loc) · 1.6 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
"use strict";
const strf_zeros = '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
strf_spaces = ' ';
module.exports = function sprintf(fmt, a, b)
{
var re = /%(-?\d+(?:\.?\d+)?)?([sdfxXb])/g,
args = arguments, index = 0;
function justify(str, width) {
var ws = width.split('.'),
dotw = +(ws[1] | 0);
width = +ws[0];
if (width < 0) {
var is_left = 1;
width *= -1;
}
if (dotw) {
ss = str.split('.');
if (!ss[1])
str += '.00000000000000000000000000000'.slice(0, dotw + 1);
else {
var dot_pad = dotw - ss[1].length;
if (dot_pad > 0)
str += '000000000000000000000000000000'.slice(0, dot_pad);
}
width += dotw + 1;
}
var fill = width - str.length;
if (fill < 0)
fill = 0;
fill = (ws[0].slice(0, 1) == '0' ? strf_zeros : strf_spaces).slice(0, fill);
return is_left ? str + fill : fill + str;
}
function to_string(o) {
switch (typeof o) {
case 'string': return o;
case 'undefined': return 'undefined';
case 'function':
case 'Boolean':
case 'number': return o.toString();
default: // 'object'
if (o === null)
return 'null';
}
return o.toString ? o.toString() : '[Object]';
}
return fmt.replace(re, function (match, width, type, offset, string) {
var a = args[++index];
if (type === 's')
a = to_string(a);
else {
a = (+a).toString([10, 10, 16, 16, 2]['dfxXb'.indexOf(type)]);
if (type === 'X')
a = a.toUpperCase();
}
return justify(a, width || '');
});
};