--- a/devtools/shared/string-format.js
+++ b/devtools/shared/string-format.js
@@ -57,20 +57,21 @@ function stringFormat() {
return stringFormat.format.call(null, cache[key], arguments);
}
// eslint-disable-next-line complexity
stringFormat.format = function (parseTree, argv) {
let cursor = 1, treeLength = parseTree.length, nodeType = "", arg, output = [], i, k,
match, pad, padCharacter, padLength, isPositive = true, sign = "";
for (i = 0; i < treeLength; i++) {
- nodeType = getType(parseTree[i]);
+ nodeType = typeof parseTree[i];
+ // parseTree item is either a string or an array (return of match() call).
if (nodeType === "string") {
output[output.length] = parseTree[i];
- } else if (nodeType === "array") {
+ } else if (nodeType === "object") {
match = parseTree[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(stringFormat("[stringFormat] property '%s' does not exist",
match[2][k]));
}
@@ -78,23 +79,23 @@ stringFormat.format = function (parseTre
}
} else if (match[1]) { // positional argument (explicit)
arg = argv[match[1]];
} else { // positional argument (implicit)
arg = argv[cursor++];
}
if (re.notType.test(match[8]) && re.notPrimitive.test(match[8]) &&
- getType(arg) == "function") {
+ typeof arg == "function") {
arg = arg();
}
- if (re.numericArg.test(match[8]) && (getType(arg) != "number" && isNaN(arg))) {
+ if (re.numericArg.test(match[8]) && (typeof arg != "number" && isNaN(arg))) {
throw new TypeError(stringFormat("[stringFormat] expecting number but found %s",
- getType(arg)));
+ typeof arg));
}
if (re.number.test(match[8])) {
isPositive = arg >= 0;
}
switch (match[8]) {
case "b":
@@ -128,17 +129,17 @@ stringFormat.format = function (parseTre
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 = getType(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);
@@ -226,29 +227,16 @@ stringFormat.parse = function (fmt) {
} else {
throw new SyntaxError("[stringFormat] unexpected placeholder");
}
_fmt = _fmt.substring(match[0].length);
}
return parseTree;
};
-/**
- * helpers
- */
-function getType(variable) {
- if (typeof variable === "number") {
- return "number";
- } else if (typeof variable === "string") {
- return "string";
- }
-
- return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase();
-}
-
var preformattedPadding = {
"0": ["", "0", "00", "000", "0000", "00000", "000000", "0000000"],
" ": ["", " ", " ", " ", " ", " ", " ", " "],
"_": ["", "_", "__", "___", "____", "_____", "______", "_______"],
};
function strRepeat(input, multiplier) {
if (multiplier >= 0 && multiplier <= 7 && preformattedPadding[input]) {
return preformattedPadding[input][multiplier];