|
1 | 1 | /* global window, exports, define */
|
2 | 2 |
|
3 | 3 | !function() {
|
4 |
| - 'use strict' |
| 4 | + 'use strict'; |
5 | 5 |
|
6 | 6 | var re = {
|
7 | 7 | not_string: /[^s]/,
|
|
23 | 23 |
|
24 | 24 | function sprintf(key) {
|
25 | 25 | // `arguments` is not an array, but should be fine for this call
|
26 |
| - return sprintf_format(sprintf_parse(key), arguments) |
| 26 | + return sprintf_format(sprintf_parse(key), arguments); |
27 | 27 | }
|
28 | 28 |
|
29 | 29 | function vsprintf(fmt, argv) {
|
30 |
| - return sprintf.apply(null, [fmt].concat(argv || [])) |
| 30 | + return sprintf.apply(null, [fmt].concat(argv || [])); |
31 | 31 | }
|
32 | 32 |
|
33 | 33 | function sprintf_format(parse_tree, argv) {
|
34 |
| - var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign |
| 34 | + var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign; |
35 | 35 | for (i = 0; i < tree_length; i++) {
|
36 | 36 | if (typeof parse_tree[i] === 'string') {
|
37 |
| - output += parse_tree[i] |
| 37 | + output += parse_tree[i]; |
38 | 38 | }
|
39 | 39 | else if (typeof parse_tree[i] === 'object') {
|
40 |
| - ph = parse_tree[i] // convenience purposes only |
| 40 | + ph = parse_tree[i]; // convenience purposes only |
41 | 41 | if (ph.keys) { // keyword argument
|
42 |
| - arg = argv[cursor] |
| 42 | + arg = argv[cursor]; |
43 | 43 | for (k = 0; k < ph.keys.length; k++) {
|
44 | 44 | if (arg == undefined) {
|
45 |
| - throw new Error(sprintf('[sprintf] Cannot access property "%s" of undefined value "%s"', ph.keys[k], ph.keys[k-1])) |
| 45 | + throw new Error(sprintf('[sprintf] Cannot access property "%s" of undefined value "%s"', ph.keys[k], ph.keys[k-1])); |
46 | 46 | }
|
47 |
| - arg = arg[ph.keys[k]] |
| 47 | + arg = arg[ph.keys[k]]; |
48 | 48 | }
|
49 | 49 | }
|
50 | 50 | else if (ph.param_no) { // positional argument (explicit)
|
51 |
| - arg = argv[ph.param_no] |
| 51 | + arg = argv[ph.param_no]; |
52 | 52 | }
|
53 | 53 | else { // positional argument (implicit)
|
54 |
| - arg = argv[cursor++] |
| 54 | + arg = argv[cursor++]; |
55 | 55 | }
|
56 | 56 |
|
57 | 57 | if (re.not_type.test(ph.type) && re.not_primitive.test(ph.type) && arg instanceof Function) {
|
58 |
| - arg = arg() |
| 58 | + arg = arg(); |
59 | 59 | }
|
60 | 60 |
|
61 | 61 | if (re.numeric_arg.test(ph.type) && (typeof arg !== 'number' && isNaN(arg))) {
|
62 |
| - throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg)) |
| 62 | + throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg)); |
63 | 63 | }
|
64 | 64 |
|
65 | 65 | if (re.number.test(ph.type)) {
|
66 |
| - is_positive = arg >= 0 |
| 66 | + is_positive = arg >= 0; |
67 | 67 | }
|
68 | 68 |
|
69 | 69 | switch (ph.type) {
|
70 | 70 | case 'b':
|
71 |
| - arg = parseInt(arg, 10).toString(2) |
72 |
| - break |
| 71 | + arg = parseInt(arg, 10).toString(2); |
| 72 | + break; |
73 | 73 | case 'c':
|
74 |
| - arg = String.fromCharCode(parseInt(arg, 10)) |
75 |
| - break |
| 74 | + arg = String.fromCharCode(parseInt(arg, 10)); |
| 75 | + break; |
76 | 76 | case 'd':
|
77 | 77 | case 'i':
|
78 |
| - arg = parseInt(arg, 10) |
79 |
| - break |
| 78 | + arg = parseInt(arg, 10); |
| 79 | + break; |
80 | 80 | case 'j':
|
81 |
| - arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0) |
82 |
| - break |
| 81 | + arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0); |
| 82 | + break; |
83 | 83 | case 'e':
|
84 |
| - arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential() |
85 |
| - break |
| 84 | + arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential(); |
| 85 | + break; |
86 | 86 | case 'f':
|
87 |
| - arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg) |
88 |
| - break |
| 87 | + arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg); |
| 88 | + break; |
89 | 89 | case 'g':
|
90 |
| - arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg) |
91 |
| - break |
| 90 | + arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg); |
| 91 | + break; |
92 | 92 | case 'o':
|
93 |
| - arg = (parseInt(arg, 10) >>> 0).toString(8) |
94 |
| - break |
| 93 | + arg = (parseInt(arg, 10) >>> 0).toString(8); |
| 94 | + break; |
95 | 95 | case 's':
|
96 |
| - arg = String(arg) |
97 |
| - arg = (ph.precision ? arg.substring(0, ph.precision) : arg) |
98 |
| - break |
| 96 | + arg = String(arg); |
| 97 | + arg = (ph.precision ? arg.substring(0, ph.precision) : arg); |
| 98 | + break; |
99 | 99 | case 't':
|
100 |
| - arg = String(!!arg) |
101 |
| - arg = (ph.precision ? arg.substring(0, ph.precision) : arg) |
102 |
| - break |
| 100 | + arg = String(!!arg); |
| 101 | + arg = (ph.precision ? arg.substring(0, ph.precision) : arg); |
| 102 | + break; |
103 | 103 | case 'T':
|
104 |
| - arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase() |
105 |
| - arg = (ph.precision ? arg.substring(0, ph.precision) : arg) |
106 |
| - break |
| 104 | + arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase(); |
| 105 | + arg = (ph.precision ? arg.substring(0, ph.precision) : arg); |
| 106 | + break; |
107 | 107 | case 'u':
|
108 |
| - arg = parseInt(arg, 10) >>> 0 |
109 |
| - break |
| 108 | + arg = parseInt(arg, 10) >>> 0; |
| 109 | + break; |
110 | 110 | case 'v':
|
111 |
| - arg = arg.valueOf() |
112 |
| - arg = (ph.precision ? arg.substring(0, ph.precision) : arg) |
113 |
| - break |
| 111 | + arg = arg.valueOf(); |
| 112 | + arg = (ph.precision ? arg.substring(0, ph.precision) : arg); |
| 113 | + break; |
114 | 114 | case 'x':
|
115 |
| - arg = (parseInt(arg, 10) >>> 0).toString(16) |
116 |
| - break |
| 115 | + arg = (parseInt(arg, 10) >>> 0).toString(16); |
| 116 | + break; |
117 | 117 | case 'X':
|
118 |
| - arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase() |
119 |
| - break |
| 118 | + arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase(); |
| 119 | + break; |
120 | 120 | }
|
121 | 121 | if (re.json.test(ph.type)) {
|
122 |
| - output += arg |
| 122 | + output += arg; |
123 | 123 | }
|
124 | 124 | else {
|
125 | 125 | if (re.number.test(ph.type) && (!is_positive || ph.sign)) {
|
126 |
| - sign = is_positive ? '+' : '-' |
127 |
| - arg = arg.toString().replace(re.sign, '') |
| 126 | + sign = is_positive ? '+' : '-'; |
| 127 | + arg = arg.toString().replace(re.sign, ''); |
128 | 128 | }
|
129 | 129 | else {
|
130 |
| - sign = '' |
| 130 | + sign = ''; |
131 | 131 | }
|
132 |
| - pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' ' |
133 |
| - pad_length = ph.width - (sign + arg).length |
134 |
| - pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : '' |
135 |
| - output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg) |
| 132 | + pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' '; |
| 133 | + pad_length = ph.width - (sign + arg).length; |
| 134 | + pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : ''; |
| 135 | + output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg); |
136 | 136 | }
|
137 | 137 | }
|
138 | 138 | }
|
139 |
| - return output |
| 139 | + return output; |
140 | 140 | }
|
141 | 141 |
|
142 |
| - var sprintf_cache = Object.create(null) |
| 142 | + var sprintf_cache = Object.create(null); |
143 | 143 |
|
144 | 144 | function sprintf_parse(fmt) {
|
145 | 145 | if (sprintf_cache[fmt]) {
|
146 |
| - return sprintf_cache[fmt] |
| 146 | + return sprintf_cache[fmt]; |
147 | 147 | }
|
148 | 148 |
|
149 |
| - var _fmt = fmt, match, parse_tree = [], arg_names = 0 |
| 149 | + var _fmt = fmt, match, parse_tree = [], arg_names = 0; |
150 | 150 | while (_fmt) {
|
151 | 151 | if ((match = re.text.exec(_fmt)) !== null) {
|
152 |
| - parse_tree.push(match[0]) |
| 152 | + parse_tree.push(match[0]); |
153 | 153 | }
|
154 | 154 | else if ((match = re.modulo.exec(_fmt)) !== null) {
|
155 |
| - parse_tree.push('%') |
| 155 | + parse_tree.push('%'); |
156 | 156 | }
|
157 | 157 | else if ((match = re.placeholder.exec(_fmt)) !== null) {
|
158 | 158 | if (match[2]) {
|
159 |
| - arg_names |= 1 |
160 |
| - var field_list = [], replacement_field = match[2], field_match = [] |
| 159 | + arg_names |= 1; |
| 160 | + var field_list = [], replacement_field = match[2], field_match = []; |
161 | 161 | if ((field_match = re.key.exec(replacement_field)) !== null) {
|
162 |
| - field_list.push(field_match[1]) |
| 162 | + field_list.push(field_match[1]); |
163 | 163 | while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
|
164 | 164 | if ((field_match = re.key_access.exec(replacement_field)) !== null) {
|
165 |
| - field_list.push(field_match[1]) |
| 165 | + field_list.push(field_match[1]); |
166 | 166 | }
|
167 | 167 | else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
|
168 |
| - field_list.push(field_match[1]) |
| 168 | + field_list.push(field_match[1]); |
169 | 169 | }
|
170 | 170 | else {
|
171 |
| - throw new SyntaxError('[sprintf] failed to parse named argument key') |
| 171 | + throw new SyntaxError('[sprintf] failed to parse named argument key'); |
172 | 172 | }
|
173 | 173 | }
|
174 | 174 | }
|
175 | 175 | else {
|
176 |
| - throw new SyntaxError('[sprintf] failed to parse named argument key') |
| 176 | + throw new SyntaxError('[sprintf] failed to parse named argument key'); |
177 | 177 | }
|
178 |
| - match[2] = field_list |
| 178 | + match[2] = field_list; |
179 | 179 | }
|
180 | 180 | else {
|
181 |
| - arg_names |= 2 |
| 181 | + arg_names |= 2; |
182 | 182 | }
|
183 | 183 | if (arg_names === 3) {
|
184 |
| - throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported') |
| 184 | + throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported'); |
185 | 185 | }
|
186 | 186 |
|
187 | 187 | parse_tree.push(
|
|
199 | 199 | )
|
200 | 200 | }
|
201 | 201 | else {
|
202 |
| - throw new SyntaxError('[sprintf] unexpected placeholder') |
| 202 | + throw new SyntaxError('[sprintf] unexpected placeholder'); |
203 | 203 | }
|
204 |
| - _fmt = _fmt.substring(match[0].length) |
| 204 | + _fmt = _fmt.substring(match[0].length); |
205 | 205 | }
|
206 |
| - return sprintf_cache[fmt] = parse_tree |
| 206 | + return sprintf_cache[fmt] = parse_tree; |
207 | 207 | }
|
208 | 208 |
|
209 | 209 | /**
|
210 | 210 | * export to either browser or node.js
|
211 | 211 | */
|
212 | 212 | /* eslint-disable quote-props */
|
213 | 213 | if (typeof exports !== 'undefined') {
|
214 |
| - exports['sprintf'] = sprintf |
215 |
| - exports['vsprintf'] = vsprintf |
| 214 | + exports['sprintf'] = sprintf; |
| 215 | + exports['vsprintf'] = vsprintf; |
216 | 216 | }
|
217 | 217 | if (typeof window !== 'undefined') {
|
218 |
| - window['sprintf'] = sprintf |
219 |
| - window['vsprintf'] = vsprintf |
| 218 | + window['sprintf'] = sprintf; |
| 219 | + window['vsprintf'] = vsprintf; |
220 | 220 |
|
221 | 221 | if (typeof define === 'function' && define['amd']) {
|
222 | 222 | define(function() {
|
|
0 commit comments