LCOV - code coverage report
Current view: top level - globalscripts - sprintf.js (source / functions) Hit Total Coverage
Test: lcov.info Lines: 8 110 7.3 %
Date: 2023-04-02 12:52:40 Functions: 1 6 16.7 %

          Line data    Source code
       1             : /**
       2             : sprintf() for JavaScript v1.1.2
       3             : https://github.com/alexei/sprintf.js
       4             : 
       5             : Copyright (c) 2007-2013, Alexandru Marasteanu <hello [at) alexei (dot] ro>
       6             : All rights reserved.
       7             : 
       8             : Redistribution and use in source and binary forms, with or without
       9             : modification, are permitted provided that the following conditions are met:
      10             : * Redistributions of source code must retain the above copyright
      11             :   notice, this list of conditions and the following disclaimer.
      12             : * Redistributions in binary form must reproduce the above copyright
      13             :   notice, this list of conditions and the following disclaimer in the
      14             :   documentation and/or other materials provided with the distribution.
      15             : * Neither the name of this software nor the names of its contributors may be
      16             :   used to endorse or promote products derived from this software without
      17             :   specific prior written permission.
      18             : 
      19             : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
      20             : ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
      21             : WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
      22             : DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
      23             : ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
      24             : (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
      25             : LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
      26             : ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      27             : (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
      28             : SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      29             : 
      30             : **/
      31             : /* global window, exports, define */
      32             : 
      33          72 : !function(window)
      34             : {
      35             :     'use strict'
      36             : 
      37          72 :     var re = {
      38             :         not_string: /[^s]/,
      39             :         not_bool: /[^t]/,
      40             :         not_type: /[^T]/,
      41             :         not_primitive: /[^v]/,
      42             :         number: /[diefg]/,
      43             :         numeric_arg: /[bcdiefguxX]/,
      44             :         json: /[j]/,
      45             :         not_json: /[^j]/,
      46             :         text: /^[^\x25]+/,
      47             :         modulo: /^\x25{2}/,
      48             :         placeholder: /^\x25(?:([1-9]\d*)\$|\(([^)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijostTuvxX])/,
      49             :         key: /^([a-z_][a-z_\d]*)/i,
      50             :         key_access: /^\.([a-z_][a-z_\d]*)/i,
      51             :         index_access: /^\[(\d+)\]/,
      52             :         sign: /^[+-]/
      53             :     }
      54             : 
      55             :     function sprintf(key) {
      56             :         // `arguments` is not an array, but should be fine for this call
      57           0 :         return sprintf_format(sprintf_parse(key), arguments)
      58             :     }
      59             : 
      60             :     function vsprintf(fmt, argv) {
      61           0 :         return sprintf.apply(null, [fmt].concat(argv || []))
      62             :     }
      63             : 
      64             :     function sprintf_format(parse_tree, argv) {
      65           0 :         var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign
      66           0 :         for (i = 0; i < tree_length; i++) {
      67           0 :             if (typeof parse_tree[i] === 'string') {
      68           0 :                 output += parse_tree[i]
      69             :             }
      70           0 :             else if (typeof parse_tree[i] === 'object') {
      71           0 :                 ph = parse_tree[i] // convenience purposes only
      72           0 :                 if (ph.keys) { // keyword argument
      73           0 :                     arg = argv[cursor]
      74           0 :                     for (k = 0; k < ph.keys.length; k++) {
      75           0 :                         if (arg == undefined) {
      76           0 :                             throw new Error(sprintf('[sprintf] Cannot access property "%s" of undefined value "%s"', ph.keys[k], ph.keys[k-1]))
      77             :                         }
      78           0 :                         arg = arg[ph.keys[k]]
      79             :                     }
      80             :                 }
      81           0 :                 else if (ph.param_no) { // positional argument (explicit)
      82           0 :                     arg = argv[ph.param_no]
      83             :                 }
      84             :                 else { // positional argument (implicit)
      85           0 :                     arg = argv[cursor++]
      86             :                 }
      87             : 
      88           0 :                 if (re.not_type.test(ph.type) && re.not_primitive.test(ph.type) && arg instanceof Function) {
      89           0 :                     arg = arg()
      90             :                 }
      91             : 
      92           0 :                 if (re.numeric_arg.test(ph.type) && (typeof arg !== 'number' && isNaN(arg))) {
      93           0 :                     throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg))
      94             :                 }
      95             : 
      96           0 :                 if (re.number.test(ph.type)) {
      97           0 :                     is_positive = arg >= 0
      98             :                 }
      99             : 
     100           0 :                 switch (ph.type) {
     101             :                     case 'b':
     102           0 :                         arg = parseInt(arg, 10).toString(2)
     103           0 :                         break
     104             :                     case 'c':
     105           0 :                         arg = String.fromCharCode(parseInt(arg, 10))
     106           0 :                         break
     107             :                     case 'd':
     108             :                     case 'i':
     109           0 :                         arg = parseInt(arg, 10)
     110           0 :                         break
     111             :                     case 'j':
     112           0 :                         arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0)
     113           0 :                         break
     114             :                     case 'e':
     115           0 :                         arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential()
     116           0 :                         break
     117             :                     case 'f':
     118           0 :                         arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg)
     119           0 :                         break
     120             :                     case 'g':
     121           0 :                         arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg)
     122           0 :                         break
     123             :                     case 'o':
     124           0 :                         arg = (parseInt(arg, 10) >>> 0).toString(8)
     125           0 :                         break
     126             :                     case 's':
     127           0 :                         arg = String(arg)
     128           0 :                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
     129           0 :                         break
     130             :                     case 't':
     131           0 :                         arg = String(!!arg)
     132           0 :                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
     133           0 :                         break
     134             :                     case 'T':
     135           0 :                         arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase()
     136           0 :                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
     137           0 :                         break
     138             :                     case 'u':
     139           0 :                         arg = parseInt(arg, 10) >>> 0
     140           0 :                         break
     141             :                     case 'v':
     142           0 :                         arg = arg.valueOf()
     143           0 :                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
     144           0 :                         break
     145             :                     case 'x':
     146           0 :                         arg = (parseInt(arg, 10) >>> 0).toString(16)
     147           0 :                         break
     148             :                     case 'X':
     149           0 :                         arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase()
     150           0 :                         break
     151             :                 }
     152           0 :                 if (re.json.test(ph.type)) {
     153           0 :                     output += arg
     154             :                 }
     155             :                 else {
     156           0 :                     if (re.number.test(ph.type) && (!is_positive || ph.sign)) {
     157           0 :                         sign = is_positive ? '+' : '-'
     158           0 :                         arg = arg.toString().replace(re.sign, '')
     159             :                     }
     160             :                     else {
     161           0 :                         sign = ''
     162             :                     }
     163           0 :                     pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' '
     164           0 :                     pad_length = ph.width - (sign + arg).length
     165           0 :                     pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : ''
     166           0 :                     output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg)
     167             :                 }
     168             :             }
     169             :         }
     170           0 :         return output
     171             :     }
     172             : 
     173          72 :     var sprintf_cache = Object.create(null)
     174             : 
     175             :     function sprintf_parse(fmt) {
     176           0 :         if (sprintf_cache[fmt]) {
     177           0 :             return sprintf_cache[fmt]
     178             :         }
     179             : 
     180           0 :         var _fmt = fmt, match, parse_tree = [], arg_names = 0
     181           0 :         while (_fmt) {
     182           0 :             if ((match = re.text.exec(_fmt)) !== null) {
     183           0 :                 parse_tree.push(match[0])
     184             :             }
     185           0 :             else if ((match = re.modulo.exec(_fmt)) !== null) {
     186           0 :                 parse_tree.push('%')
     187             :             }
     188           0 :             else if ((match = re.placeholder.exec(_fmt)) !== null) {
     189           0 :                 if (match[2]) {
     190           0 :                     arg_names |= 1
     191           0 :                     var field_list = [], replacement_field = match[2], field_match = []
     192           0 :                     if ((field_match = re.key.exec(replacement_field)) !== null) {
     193           0 :                         field_list.push(field_match[1])
     194           0 :                         while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
     195           0 :                             if ((field_match = re.key_access.exec(replacement_field)) !== null) {
     196           0 :                                 field_list.push(field_match[1])
     197             :                             }
     198           0 :                             else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
     199           0 :                                 field_list.push(field_match[1])
     200             :                             }
     201             :                             else {
     202           0 :                                 throw new SyntaxError('[sprintf] failed to parse named argument key')
     203             :                             }
     204             :                         }
     205             :                     }
     206             :                     else {
     207           0 :                         throw new SyntaxError('[sprintf] failed to parse named argument key')
     208             :                     }
     209           0 :                     match[2] = field_list
     210             :                 }
     211             :                 else {
     212           0 :                     arg_names |= 2
     213             :                 }
     214           0 :                 if (arg_names === 3) {
     215           0 :                     throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported')
     216             :                 }
     217             : 
     218           0 :                 parse_tree.push(
     219             :                     {
     220             :                         placeholder: match[0],
     221             :                         param_no:    match[1],
     222             :                         keys:        match[2],
     223             :                         sign:        match[3],
     224             :                         pad_char:    match[4],
     225             :                         align:       match[5],
     226             :                         width:       match[6],
     227             :                         precision:   match[7],
     228             :                         type:        match[8]
     229             :                     }
     230             :                 )
     231             :             }
     232             :             else {
     233           0 :                 throw new SyntaxError('[sprintf] unexpected placeholder')
     234             :             }
     235           0 :             _fmt = _fmt.substring(match[0].length)
     236             :         }
     237           0 :         return sprintf_cache[fmt] = parse_tree
     238             :     }
     239             : 
     240             :     /**
     241             :      * export to either browser or node.js
     242             :      */
     243             :     /* eslint-disable quote-props */
     244          72 :     if (typeof exports !== 'undefined') {
     245           0 :         exports['sprintf'] = sprintf
     246           0 :         exports['vsprintf'] = vsprintf
     247             :     }
     248          72 :     if (typeof window !== 'undefined') {
     249          72 :         window['sprintf'] = sprintf
     250          72 :         window['vsprintf'] = vsprintf
     251             : 
     252          72 :         if (typeof define === 'function' && define['amd']) {
     253           0 :             define(function() {
     254           0 :                 return {
     255             :                     'sprintf': sprintf,
     256             :                     'vsprintf': vsprintf
     257             :                 }
     258             :             })
     259             :         }
     260             :     }
     261             :     /* eslint-enable quote-props */
     262             : }(this); // eslint-disable-line

Generated by: LCOV version 1.14