This repository has been archived by the owner on Aug 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
format.js
128 lines (118 loc) · 3.26 KB
/
format.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
//
// format - printf-like string formatting for JavaScript
// github.com/samsonjs/format
// @_sjs
//
// Copyright 2010 - 2013 Sami Samhuri <[email protected]>
//
// MIT License
// http://sjs.mit-license.org
//
;(function() {
//// Export the API
var namespace;
// CommonJS / Node module
if (typeof module !== 'undefined') {
namespace = module.exports = format;
}
// Browsers and other environments
else {
// Get the global object. Works in ES3, ES5, and ES5 strict mode.
namespace = (function(){ return this || (1,eval)('this') }());
}
namespace.format = format;
namespace.vsprintf = vsprintf;
if (typeof console !== 'undefined' && typeof console.log === 'function') {
namespace.printf = printf;
}
function printf(/* ... */) {
console.log(format.apply(null, arguments));
}
function vsprintf(fmt, replacements) {
return format.apply(null, [fmt].concat(replacements));
}
function format(fmt) {
var argIndex = 1 // skip initial format argument
, args = [].slice.call(arguments)
, i = 0
, n = fmt.length
, result = ''
, c
, escaped = false
, arg
, tmp
, leadingZero = false
, precision
, nextArg = function() { return args[argIndex++]; }
, slurpNumber = function() {
var digits = '';
while (/\d/.test(fmt[i])) {
digits += fmt[i++];
c = fmt[i];
}
return digits.length > 0 ? parseInt(digits) : null;
}
;
for (; i < n; ++i) {
c = fmt[i];
if (escaped) {
escaped = false;
if (c == '.') {
leadingZero = false;
c = fmt[++i];
}
else if (c == '0' && fmt[i + 1] == '.') {
leadingZero = true;
i += 2;
c = fmt[i];
}
else {
leadingZero = true;
}
precision = slurpNumber();
switch (c) {
case 'b': // number in binary
result += parseInt(nextArg(), 10).toString(2);
break;
case 'c': // character
arg = nextArg();
if (typeof arg === 'string' || arg instanceof String)
result += arg;
else
result += String.fromCharCode(parseInt(arg, 10));
break;
case 'd': // number in decimal
result += parseInt(nextArg(), 10);
break;
case 'f': // floating point number
tmp = String(parseFloat(nextArg()).toFixed(precision || 6));
result += leadingZero ? tmp : tmp.replace(/^0/, '');
break;
case 'j': // JSON
result += JSON.stringify(nextArg());
break;
case 'o': // number in octal
result += '0' + parseInt(nextArg(), 10).toString(8);
break;
case 's': // string
result += nextArg();
break;
case 'x': // lowercase hexadecimal
result += '0x' + parseInt(nextArg(), 10).toString(16);
break;
case 'X': // uppercase hexadecimal
result += '0x' + parseInt(nextArg(), 10).toString(16).toUpperCase();
break;
default:
result += c;
break;
}
} else if (c === '%') {
escaped = true;
} else {
result += c;
}
}
return result;
}
}());