Bug 1406311 - sprintfjs: only keep support for %S %s %d %i
MozReview-Commit-ID: 3sbSdKsR5mI
--- a/devtools/shared/sprintfjs/sprintf.js
+++ b/devtools/shared/sprintfjs/sprintf.js
@@ -66,108 +66,45 @@
var cursor = 1, tree_length = parse_tree.length, arg, output = [], i, k, match, pad, pad_character, pad_length, is_positive = true, sign = ''
for (i = 0; i < tree_length; i++) {
// parse tree contains either substrings or match results (array)
if (typeof parse_tree[i] === 'string') {
output[output.length] = parse_tree[i]
} else {
match = parse_tree[i] // convenience purposes only
- if (match[2]) { // keyword argument
- arg = argv[cursor]
- for (k = 0; k < match[2].length; k++) {
- if (!arg.hasOwnProperty(match[2][k])) {
- throw new Error(sprintf('[sprintf] property "%s" does not exist', match[2][k]))
- }
- arg = arg[match[2][k]]
- }
- }
- else if (match[1]) { // positional argument (explicit)
+ if (match[1]) { // positional argument (explicit)
arg = argv[match[1]]
}
else { // positional argument (implicit)
arg = argv[cursor++]
}
- if (re.not_type.test(match[8]) && re.not_primitive.test(match[8]) && get_type(arg) == 'function') {
- arg = arg()
- }
-
- if (re.numeric_arg.test(match[8]) && (get_type(arg) != 'number' && isNaN(arg))) {
- throw new TypeError(sprintf("[sprintf] expecting number but found %s", get_type(arg)))
- }
-
- if (re.number.test(match[8])) {
- is_positive = arg >= 0
- }
-
switch (match[8]) {
case 'S':
case 's':
arg = String(arg)
arg = (match[7] ? arg.substring(0, match[7]) : arg)
break
- case 'b':
- arg = parseInt(arg, 10).toString(2)
- break
- case 'c':
- arg = String.fromCharCode(parseInt(arg, 10))
- break
case 'd':
case 'i':
+ if ((get_type(arg) != 'number' && isNaN(arg))) {
+ throw new TypeError(sprintf("[sprintf] expecting number but found %s", get_type(arg)))
+ }
+ is_positive = arg >= 0
arg = parseInt(arg, 10)
- break
- case 'j':
- arg = JSON.stringify(arg, null, match[6] ? parseInt(match[6]) : 0)
- break
- case 'e':
- arg = match[7] ? parseFloat(arg).toExponential(match[7]) : parseFloat(arg).toExponential()
- break
- case 'f':
- arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg)
- break
- case 'g':
- arg = match[7] ? parseFloat(arg).toPrecision(match[7]) : parseFloat(arg)
- break
- case 'o':
- arg = arg.toString(8)
- break
- case 't':
- arg = String(!!arg)
- arg = (match[7] ? arg.substring(0, match[7]) : arg)
- break
- case 'T':
- arg = get_type(arg)
- arg = (match[7] ? arg.substring(0, match[7]) : arg)
- break
- case 'u':
- arg = parseInt(arg, 10) >>> 0
- break
- case 'v':
- arg = arg.valueOf()
- arg = (match[7] ? arg.substring(0, match[7]) : arg)
- break
- case 'x':
- arg = parseInt(arg, 10).toString(16)
- break
- case 'X':
- arg = parseInt(arg, 10).toString(16).toUpperCase()
+ if (!is_positive || match[3]) {
+ sign = is_positive ? '+' : '-'
+ arg = arg.toString().replace(re.sign, '')
+ }
break
}
- if (re.json.test(match[8])) {
- output[output.length] = arg
- }
- else {
- if (re.number.test(match[8]) && (!is_positive || match[3])) {
- sign = is_positive ? '+' : '-'
- arg = arg.toString().replace(re.sign, '')
- }
- else {
- sign = ''
- }
+ if (!match[6]) {
+ output[output.length] = sign + arg;
+ } else {
pad_character = match[4] ? match[4] === '0' ? '0' : match[4].charAt(1) : ' '
pad_length = match[6] - (sign + arg).length
pad = match[6] ? (pad_length > 0 ? str_repeat(pad_character, pad_length) : '') : ''
output[output.length] = match[5] ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg)
}
}
}
return output.join('')