svghmi/sprintf.js
changeset 3360 746e3e3f6537
child 3451 302efcf746e0
equal deleted inserted replaced
3359:2c924cf26161 3360:746e3e3f6537
       
     1 /* https://github.com/alexei/sprintf.js/blob/master/src/sprintf.js */
       
     2 /* global window, exports, define */
       
     3 
       
     4 !function() {
       
     5     'use strict'
       
     6 
       
     7     var re = {
       
     8         not_string: /[^s]/,
       
     9         not_bool: /[^t]/,
       
    10         not_type: /[^T]/,
       
    11         not_primitive: /[^v]/,
       
    12         number: /[diefg]/,
       
    13         numeric_arg: /[bcdiefguxX]/,
       
    14         json: /[j]/,
       
    15         not_json: /[^j]/,
       
    16         text: /^[^\x25]+/,
       
    17         modulo: /^\x25{2}/,
       
    18         placeholder: /^\x25(?:([1-9]\d*)\$|\(([^)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijostTuvxX])/,
       
    19         key: /^([a-z_][a-z_\d]*)/i,
       
    20         key_access: /^\.([a-z_][a-z_\d]*)/i,
       
    21         index_access: /^\[(\d+)\]/,
       
    22         sign: /^[+-]/
       
    23     }
       
    24 
       
    25     function sprintf(key) {
       
    26         // arguments is not an array, but should be fine for this call
       
    27         return sprintf_format(sprintf_parse(key), arguments)
       
    28     }
       
    29 
       
    30     function vsprintf(fmt, argv) {
       
    31         return sprintf.apply(null, [fmt].concat(argv || []))
       
    32     }
       
    33 
       
    34     function sprintf_format(parse_tree, argv) {
       
    35         var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign
       
    36         for (i = 0; i < tree_length; i++) {
       
    37             if (typeof parse_tree[i] === 'string') {
       
    38                 output += parse_tree[i]
       
    39             }
       
    40             else if (typeof parse_tree[i] === 'object') {
       
    41                 ph = parse_tree[i] // convenience purposes only
       
    42                 if (ph.keys) { // keyword argument
       
    43                     arg = argv[cursor]
       
    44                     for (k = 0; k < ph.keys.length; k++) {
       
    45                         if (arg == undefined) {
       
    46                             throw new Error(sprintf('[sprintf] Cannot access property "%s" of undefined value "%s"', ph.keys[k], ph.keys[k-1]))
       
    47                         }
       
    48                         arg = arg[ph.keys[k]]
       
    49                     }
       
    50                 }
       
    51                 else if (ph.param_no) { // positional argument (explicit)
       
    52                     arg = argv[ph.param_no]
       
    53                 }
       
    54                 else { // positional argument (implicit)
       
    55                     arg = argv[cursor++]
       
    56                 }
       
    57 
       
    58                 if (re.not_type.test(ph.type) && re.not_primitive.test(ph.type) && arg instanceof Function) {
       
    59                     arg = arg()
       
    60                 }
       
    61 
       
    62                 if (re.numeric_arg.test(ph.type) && (typeof arg !== 'number' && isNaN(arg))) {
       
    63                     throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg))
       
    64                 }
       
    65 
       
    66                 if (re.number.test(ph.type)) {
       
    67                     is_positive = arg >= 0
       
    68                 }
       
    69 
       
    70                 switch (ph.type) {
       
    71                     case 'b':
       
    72                         arg = parseInt(arg, 10).toString(2)
       
    73                         break
       
    74                     case 'c':
       
    75                         arg = String.fromCharCode(parseInt(arg, 10))
       
    76                         break
       
    77                     case 'd':
       
    78                     case 'i':
       
    79                         arg = parseInt(arg, 10)
       
    80                         break
       
    81                     case 'j':
       
    82                         arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0)
       
    83                         break
       
    84                     case 'e':
       
    85                         arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential()
       
    86                         break
       
    87                     case 'f':
       
    88                         arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg)
       
    89                         break
       
    90                     case 'g':
       
    91                         arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg)
       
    92                         break
       
    93                     case 'o':
       
    94                         arg = (parseInt(arg, 10) >>> 0).toString(8)
       
    95                         break
       
    96                     case 's':
       
    97                         arg = String(arg)
       
    98                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
       
    99                         break
       
   100                     case 't':
       
   101                         arg = String(!!arg)
       
   102                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
       
   103                         break
       
   104                     case 'T':
       
   105                         arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase()
       
   106                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
       
   107                         break
       
   108                     case 'u':
       
   109                         arg = parseInt(arg, 10) >>> 0
       
   110                         break
       
   111                     case 'v':
       
   112                         arg = arg.valueOf()
       
   113                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
       
   114                         break
       
   115                     case 'x':
       
   116                         arg = (parseInt(arg, 10) >>> 0).toString(16)
       
   117                         break
       
   118                     case 'X':
       
   119                         arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase()
       
   120                         break
       
   121                 }
       
   122                 if (re.json.test(ph.type)) {
       
   123                     output += arg
       
   124                 }
       
   125                 else {
       
   126                     if (re.number.test(ph.type) && (!is_positive || ph.sign)) {
       
   127                         sign = is_positive ? '+' : '-'
       
   128                         arg = arg.toString().replace(re.sign, '')
       
   129                     }
       
   130                     else {
       
   131                         sign = ''
       
   132                     }
       
   133                     pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' '
       
   134                     pad_length = ph.width - (sign + arg).length
       
   135                     pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : ''
       
   136                     output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg)
       
   137                 }
       
   138             }
       
   139         }
       
   140         return output
       
   141     }
       
   142 
       
   143     var sprintf_cache = Object.create(null)
       
   144 
       
   145     function sprintf_parse(fmt) {
       
   146         if (sprintf_cache[fmt]) {
       
   147             return sprintf_cache[fmt]
       
   148         }
       
   149 
       
   150         var _fmt = fmt, match, parse_tree = [], arg_names = 0
       
   151         while (_fmt) {
       
   152             if ((match = re.text.exec(_fmt)) !== null) {
       
   153                 parse_tree.push(match[0])
       
   154             }
       
   155             else if ((match = re.modulo.exec(_fmt)) !== null) {
       
   156                 parse_tree.push('%')
       
   157             }
       
   158             else if ((match = re.placeholder.exec(_fmt)) !== null) {
       
   159                 if (match[2]) {
       
   160                     arg_names |= 1
       
   161                     var field_list = [], replacement_field = match[2], field_match = []
       
   162                     if ((field_match = re.key.exec(replacement_field)) !== null) {
       
   163                         field_list.push(field_match[1])
       
   164                         while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
       
   165                             if ((field_match = re.key_access.exec(replacement_field)) !== null) {
       
   166                                 field_list.push(field_match[1])
       
   167                             }
       
   168                             else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
       
   169                                 field_list.push(field_match[1])
       
   170                             }
       
   171                             else {
       
   172                                 throw new SyntaxError('[sprintf] failed to parse named argument key')
       
   173                             }
       
   174                         }
       
   175                     }
       
   176                     else {
       
   177                         throw new SyntaxError('[sprintf] failed to parse named argument key')
       
   178                     }
       
   179                     match[2] = field_list
       
   180                 }
       
   181                 else {
       
   182                     arg_names |= 2
       
   183                 }
       
   184                 if (arg_names === 3) {
       
   185                     throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported')
       
   186                 }
       
   187 
       
   188                 parse_tree.push(
       
   189                     {
       
   190                         placeholder: match[0],
       
   191                         param_no:    match[1],
       
   192                         keys:        match[2],
       
   193                         sign:        match[3],
       
   194                         pad_char:    match[4],
       
   195                         align:       match[5],
       
   196                         width:       match[6],
       
   197                         precision:   match[7],
       
   198                         type:        match[8]
       
   199                     }
       
   200                 )
       
   201             }
       
   202             else {
       
   203                 throw new SyntaxError('[sprintf] unexpected placeholder')
       
   204             }
       
   205             _fmt = _fmt.substring(match[0].length)
       
   206         }
       
   207         return sprintf_cache[fmt] = parse_tree
       
   208     }
       
   209 
       
   210     /**
       
   211      * export to either browser or node.js
       
   212      */
       
   213     /* eslint-disable quote-props */
       
   214     if (typeof exports !== 'undefined') {
       
   215         exports['sprintf'] = sprintf
       
   216         exports['vsprintf'] = vsprintf
       
   217     }
       
   218     if (typeof window !== 'undefined') {
       
   219         window['sprintf'] = sprintf
       
   220         window['vsprintf'] = vsprintf
       
   221 
       
   222         if (typeof define === 'function' && define['amd']) {
       
   223             define(function() {
       
   224                 return {
       
   225                     'sprintf': sprintf,
       
   226                     'vsprintf': vsprintf
       
   227                 }
       
   228             })
       
   229         }
       
   230     }
       
   231     /* eslint-enable quote-props */
       
   232 }(); // eslint-disable-line