Bug 1406311 - sprintfjs: remove getType;r=bgrins draft
authorJulian Descottes <jdescottes@mozilla.com>
Tue, 17 Oct 2017 21:04:58 +0200
changeset 681763 9c249323612995278007b2ecfc9a3ca991dcd81b
parent 681717 39837c4c6039e77c189ce9db2e12d4b7b26171c7
child 681764 380a3ef1d87df17cd477cef581e28fb9f2377a47
child 681765 c2a17afaa40c257e84f5f17fc7e9c8427529501f
child 681767 283e6a5aca89d21e9baab8b3221931f77da57409
child 681816 2e469aa4ee4cb2df867e3eb328454aad3dc26a2e
push id84922
push userjdescottes@mozilla.com
push dateTue, 17 Oct 2017 19:34:30 +0000
reviewersbgrins
bugs1406311
milestone58.0a1
Bug 1406311 - sprintfjs: remove getType;r=bgrins MozReview-Commit-ID: Ar1iSbqew2L
devtools/shared/sprintfjs/sprintf.js
--- 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]) {