--- a/devtools/shared/sprintfjs/sprintf.js
+++ b/devtools/shared/sprintfjs/sprintf.js
@@ -21,16 +21,17 @@
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
+/* eslint-disable */
/* globals window, exports, define */
(function(window) {
'use strict'
var re = {
not_string: /[^s]/,
not_bool: /[^t]/,
@@ -55,21 +56,25 @@
cache[key] = sprintf.parse(key)
}
return sprintf.format.call(null, cache[key], arguments)
}
sprintf.format = function(parse_tree, argv) {
var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length, is_positive = true, sign = ''
for (i = 0; i < tree_length; i++) {
- node_type = get_type(parse_tree[i])
+ node_type = typeof parse_tree[i]
+ // The items of parse tree are either strings or results of a match() call.
if (node_type === 'string') {
+ // this is not a placeholder, this is just a string.
output[output.length] = parse_tree[i]
}
- else if (node_type === 'array') {
+ else {
+ // this is a placeholder, need to identify its type, options and replace
+ // it with the appropriate argument.
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]]
@@ -77,22 +82,22 @@
}
else 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') {
+ if (re.not_type.test(match[8]) && re.not_primitive.test(match[8]) && typeof 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.numeric_arg.test(match[8]) && (typeof arg != 'number' && isNaN(arg))) {
+ throw new TypeError(sprintf("[sprintf] expecting number but found %s", typeof arg))
}
if (re.number.test(match[8])) {
is_positive = arg >= 0
}
switch (match[8]) {
case 'b':
@@ -125,17 +130,17 @@
arg = String(arg)
arg = (match[7] ? arg.substring(0, match[7]) : arg)
break
case 't':
arg = String(!!arg)
arg = (match[7] ? arg.substring(0, match[7]) : arg)
break
case 'T':
- arg = get_type(arg)
+ arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase()
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)
@@ -222,27 +227,16 @@
_argv = (argv || []).slice(0)
_argv.splice(0, 0, fmt)
return sprintf.apply(null, _argv)
}
/**
* helpers
*/
- function get_type(variable) {
- if (typeof variable === 'number') {
- return 'number'
- }
- else if (typeof variable === 'string') {
- return 'string'
- }
- else {
- return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase()
- }
- }
var preformattedPadding = {
'0': ['', '0', '00', '000', '0000', '00000', '000000', '0000000'],
' ': ['', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
'_': ['', '_', '__', '___', '____', '_____', '______', '_______'],
}
function str_repeat(input, multiplier) {
if (multiplier >= 0 && multiplier <= 7 && preformattedPadding[input]) {