Bug 1406311 - string-format: string-format: use typeof instead of getType();r=bgrins draft
authorJulian Descottes <jdescottes@mozilla.com>
Thu, 12 Oct 2017 14:21:08 +0200
changeset 680933 93216fe3a4d098caf06c28c9df4a5543ba71bdad
parent 680932 d3ab166939991453ced70634db84a2b23f514a8a
child 680934 19db44fe15d2deb3723b4ed0dd5e47b161cee8a9
push id84682
push userjdescottes@mozilla.com
push dateMon, 16 Oct 2017 16:12:33 +0000
reviewersbgrins
bugs1406311
milestone58.0a1
Bug 1406311 - string-format: string-format: use typeof instead of getType();r=bgrins MozReview-Commit-ID: BKIH7DzqbUB
devtools/shared/string-format.js
--- 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];