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