1 | //
|
---|
2 | // format - printf-like string formatting for JavaScript
|
---|
3 | // github.com/samsonjs/format
|
---|
4 | // @_sjs
|
---|
5 | //
|
---|
6 | // Copyright 2010 - 2013 Sami Samhuri <sami@samhuri.net>
|
---|
7 | //
|
---|
8 | // MIT License
|
---|
9 | // http://sjs.mit-license.org
|
---|
10 | //
|
---|
11 |
|
---|
12 | ;(function() {
|
---|
13 |
|
---|
14 | //// Export the API
|
---|
15 | var namespace;
|
---|
16 |
|
---|
17 | // CommonJS / Node module
|
---|
18 | if (typeof module !== 'undefined') {
|
---|
19 | namespace = module.exports = format;
|
---|
20 | }
|
---|
21 |
|
---|
22 | // Browsers and other environments
|
---|
23 | else {
|
---|
24 | // Get the global object. Works in ES3, ES5, and ES5 strict mode.
|
---|
25 | namespace = (function(){ return this || (1,eval)('this') }());
|
---|
26 | }
|
---|
27 |
|
---|
28 | namespace.format = format;
|
---|
29 | namespace.vsprintf = vsprintf;
|
---|
30 |
|
---|
31 | if (typeof console !== 'undefined' && typeof console.log === 'function') {
|
---|
32 | namespace.printf = printf;
|
---|
33 | }
|
---|
34 |
|
---|
35 | function printf(/* ... */) {
|
---|
36 | console.log(format.apply(null, arguments));
|
---|
37 | }
|
---|
38 |
|
---|
39 | function vsprintf(fmt, replacements) {
|
---|
40 | return format.apply(null, [fmt].concat(replacements));
|
---|
41 | }
|
---|
42 |
|
---|
43 | function format(fmt) {
|
---|
44 | var argIndex = 1 // skip initial format argument
|
---|
45 | , args = [].slice.call(arguments)
|
---|
46 | , i = 0
|
---|
47 | , n = fmt.length
|
---|
48 | , result = ''
|
---|
49 | , c
|
---|
50 | , escaped = false
|
---|
51 | , arg
|
---|
52 | , tmp
|
---|
53 | , leadingZero = false
|
---|
54 | , precision
|
---|
55 | , nextArg = function() { return args[argIndex++]; }
|
---|
56 | , slurpNumber = function() {
|
---|
57 | var digits = '';
|
---|
58 | while (/\d/.test(fmt[i])) {
|
---|
59 | digits += fmt[i++];
|
---|
60 | c = fmt[i];
|
---|
61 | }
|
---|
62 | return digits.length > 0 ? parseInt(digits) : null;
|
---|
63 | }
|
---|
64 | ;
|
---|
65 | for (; i < n; ++i) {
|
---|
66 | c = fmt[i];
|
---|
67 | if (escaped) {
|
---|
68 | escaped = false;
|
---|
69 | if (c == '.') {
|
---|
70 | leadingZero = false;
|
---|
71 | c = fmt[++i];
|
---|
72 | }
|
---|
73 | else if (c == '0' && fmt[i + 1] == '.') {
|
---|
74 | leadingZero = true;
|
---|
75 | i += 2;
|
---|
76 | c = fmt[i];
|
---|
77 | }
|
---|
78 | else {
|
---|
79 | leadingZero = true;
|
---|
80 | }
|
---|
81 | precision = slurpNumber();
|
---|
82 | switch (c) {
|
---|
83 | case 'b': // number in binary
|
---|
84 | result += parseInt(nextArg(), 10).toString(2);
|
---|
85 | break;
|
---|
86 | case 'c': // character
|
---|
87 | arg = nextArg();
|
---|
88 | if (typeof arg === 'string' || arg instanceof String)
|
---|
89 | result += arg;
|
---|
90 | else
|
---|
91 | result += String.fromCharCode(parseInt(arg, 10));
|
---|
92 | break;
|
---|
93 | case 'd': // number in decimal
|
---|
94 | result += parseInt(nextArg(), 10);
|
---|
95 | break;
|
---|
96 | case 'f': // floating point number
|
---|
97 | tmp = String(parseFloat(nextArg()).toFixed(precision || 6));
|
---|
98 | result += leadingZero ? tmp : tmp.replace(/^0/, '');
|
---|
99 | break;
|
---|
100 | case 'j': // JSON
|
---|
101 | result += JSON.stringify(nextArg());
|
---|
102 | break;
|
---|
103 | case 'o': // number in octal
|
---|
104 | result += '0' + parseInt(nextArg(), 10).toString(8);
|
---|
105 | break;
|
---|
106 | case 's': // string
|
---|
107 | result += nextArg();
|
---|
108 | break;
|
---|
109 | case 'x': // lowercase hexadecimal
|
---|
110 | result += '0x' + parseInt(nextArg(), 10).toString(16);
|
---|
111 | break;
|
---|
112 | case 'X': // uppercase hexadecimal
|
---|
113 | result += '0x' + parseInt(nextArg(), 10).toString(16).toUpperCase();
|
---|
114 | break;
|
---|
115 | default:
|
---|
116 | result += c;
|
---|
117 | break;
|
---|
118 | }
|
---|
119 | } else if (c === '%') {
|
---|
120 | escaped = true;
|
---|
121 | } else {
|
---|
122 | result += c;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | return result;
|
---|
126 | }
|
---|
127 |
|
---|
128 | }());
|
---|