svghmi/widget_display.ysl2
changeset 3360 746e3e3f6537
parent 3241 fe945f1f48b7
child 3524 27d298c6f961
equal deleted inserted replaced
3359:2c924cf26161 3360:746e3e3f6537
    70     |     init: function() {
    70     |     init: function() {
    71     |       this.format = svg_text_to_multiline(this.format_elt);
    71     |       this.format = svg_text_to_multiline(this.format_elt);
    72     |     },
    72     |     },
    73     }
    73     }
    74 }
    74 }
    75 
       
    76 emit "preamble:display"
       
    77 ||
       
    78 /* https://github.com/alexei/sprintf.js/blob/master/src/sprintf.js */
       
    79 /* global window, exports, define */
       
    80 
       
    81 !function() {
       
    82     'use strict'
       
    83 
       
    84     var re = {
       
    85         not_string: /[^s]/,
       
    86         not_bool: /[^t]/,
       
    87         not_type: /[^T]/,
       
    88         not_primitive: /[^v]/,
       
    89         number: /[diefg]/,
       
    90         numeric_arg: /[bcdiefguxX]/,
       
    91         json: /[j]/,
       
    92         not_json: /[^j]/,
       
    93         text: /^[^\x25]+/,
       
    94         modulo: /^\x25{2}/,
       
    95         placeholder: /^\x25(?:([1-9]\d*)\$|\(([^)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijostTuvxX])/,
       
    96         key: /^([a-z_][a-z_\d]*)/i,
       
    97         key_access: /^\.([a-z_][a-z_\d]*)/i,
       
    98         index_access: /^\[(\d+)\]/,
       
    99         sign: /^[+-]/
       
   100     }
       
   101 
       
   102     function sprintf(key) {
       
   103         // arguments is not an array, but should be fine for this call
       
   104         return sprintf_format(sprintf_parse(key), arguments)
       
   105     }
       
   106 
       
   107     function vsprintf(fmt, argv) {
       
   108         return sprintf.apply(null, [fmt].concat(argv || []))
       
   109     }
       
   110 
       
   111     function sprintf_format(parse_tree, argv) {
       
   112         var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign
       
   113         for (i = 0; i < tree_length; i++) {
       
   114             if (typeof parse_tree[i] === 'string') {
       
   115                 output += parse_tree[i]
       
   116             }
       
   117             else if (typeof parse_tree[i] === 'object') {
       
   118                 ph = parse_tree[i] // convenience purposes only
       
   119                 if (ph.keys) { // keyword argument
       
   120                     arg = argv[cursor]
       
   121                     for (k = 0; k < ph.keys.length; k++) {
       
   122                         if (arg == undefined) {
       
   123                             throw new Error(sprintf('[sprintf] Cannot access property "%s" of undefined value "%s"', ph.keys[k], ph.keys[k-1]))
       
   124                         }
       
   125                         arg = arg[ph.keys[k]]
       
   126                     }
       
   127                 }
       
   128                 else if (ph.param_no) { // positional argument (explicit)
       
   129                     arg = argv[ph.param_no]
       
   130                 }
       
   131                 else { // positional argument (implicit)
       
   132                     arg = argv[cursor++]
       
   133                 }
       
   134 
       
   135                 if (re.not_type.test(ph.type) && re.not_primitive.test(ph.type) && arg instanceof Function) {
       
   136                     arg = arg()
       
   137                 }
       
   138 
       
   139                 if (re.numeric_arg.test(ph.type) && (typeof arg !== 'number' && isNaN(arg))) {
       
   140                     throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg))
       
   141                 }
       
   142 
       
   143                 if (re.number.test(ph.type)) {
       
   144                     is_positive = arg >= 0
       
   145                 }
       
   146 
       
   147                 switch (ph.type) {
       
   148                     case 'b':
       
   149                         arg = parseInt(arg, 10).toString(2)
       
   150                         break
       
   151                     case 'c':
       
   152                         arg = String.fromCharCode(parseInt(arg, 10))
       
   153                         break
       
   154                     case 'd':
       
   155                     case 'i':
       
   156                         arg = parseInt(arg, 10)
       
   157                         break
       
   158                     case 'j':
       
   159                         arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0)
       
   160                         break
       
   161                     case 'e':
       
   162                         arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential()
       
   163                         break
       
   164                     case 'f':
       
   165                         arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg)
       
   166                         break
       
   167                     case 'g':
       
   168                         arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg)
       
   169                         break
       
   170                     case 'o':
       
   171                         arg = (parseInt(arg, 10) >>> 0).toString(8)
       
   172                         break
       
   173                     case 's':
       
   174                         arg = String(arg)
       
   175                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
       
   176                         break
       
   177                     case 't':
       
   178                         arg = String(!!arg)
       
   179                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
       
   180                         break
       
   181                     case 'T':
       
   182                         arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase()
       
   183                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
       
   184                         break
       
   185                     case 'u':
       
   186                         arg = parseInt(arg, 10) >>> 0
       
   187                         break
       
   188                     case 'v':
       
   189                         arg = arg.valueOf()
       
   190                         arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
       
   191                         break
       
   192                     case 'x':
       
   193                         arg = (parseInt(arg, 10) >>> 0).toString(16)
       
   194                         break
       
   195                     case 'X':
       
   196                         arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase()
       
   197                         break
       
   198                 }
       
   199                 if (re.json.test(ph.type)) {
       
   200                     output += arg
       
   201                 }
       
   202                 else {
       
   203                     if (re.number.test(ph.type) && (!is_positive || ph.sign)) {
       
   204                         sign = is_positive ? '+' : '-'
       
   205                         arg = arg.toString().replace(re.sign, '')
       
   206                     }
       
   207                     else {
       
   208                         sign = ''
       
   209                     }
       
   210                     pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' '
       
   211                     pad_length = ph.width - (sign + arg).length
       
   212                     pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : ''
       
   213                     output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg)
       
   214                 }
       
   215             }
       
   216         }
       
   217         return output
       
   218     }
       
   219 
       
   220     var sprintf_cache = Object.create(null)
       
   221 
       
   222     function sprintf_parse(fmt) {
       
   223         if (sprintf_cache[fmt]) {
       
   224             return sprintf_cache[fmt]
       
   225         }
       
   226 
       
   227         var _fmt = fmt, match, parse_tree = [], arg_names = 0
       
   228         while (_fmt) {
       
   229             if ((match = re.text.exec(_fmt)) !== null) {
       
   230                 parse_tree.push(match[0])
       
   231             }
       
   232             else if ((match = re.modulo.exec(_fmt)) !== null) {
       
   233                 parse_tree.push('%')
       
   234             }
       
   235             else if ((match = re.placeholder.exec(_fmt)) !== null) {
       
   236                 if (match[2]) {
       
   237                     arg_names |= 1
       
   238                     var field_list = [], replacement_field = match[2], field_match = []
       
   239                     if ((field_match = re.key.exec(replacement_field)) !== null) {
       
   240                         field_list.push(field_match[1])
       
   241                         while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
       
   242                             if ((field_match = re.key_access.exec(replacement_field)) !== null) {
       
   243                                 field_list.push(field_match[1])
       
   244                             }
       
   245                             else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
       
   246                                 field_list.push(field_match[1])
       
   247                             }
       
   248                             else {
       
   249                                 throw new SyntaxError('[sprintf] failed to parse named argument key')
       
   250                             }
       
   251                         }
       
   252                     }
       
   253                     else {
       
   254                         throw new SyntaxError('[sprintf] failed to parse named argument key')
       
   255                     }
       
   256                     match[2] = field_list
       
   257                 }
       
   258                 else {
       
   259                     arg_names |= 2
       
   260                 }
       
   261                 if (arg_names === 3) {
       
   262                     throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported')
       
   263                 }
       
   264 
       
   265                 parse_tree.push(
       
   266                     {
       
   267                         placeholder: match[0],
       
   268                         param_no:    match[1],
       
   269                         keys:        match[2],
       
   270                         sign:        match[3],
       
   271                         pad_char:    match[4],
       
   272                         align:       match[5],
       
   273                         width:       match[6],
       
   274                         precision:   match[7],
       
   275                         type:        match[8]
       
   276                     }
       
   277                 )
       
   278             }
       
   279             else {
       
   280                 throw new SyntaxError('[sprintf] unexpected placeholder')
       
   281             }
       
   282             _fmt = _fmt.substring(match[0].length)
       
   283         }
       
   284         return sprintf_cache[fmt] = parse_tree
       
   285     }
       
   286 
       
   287     /**
       
   288      * export to either browser or node.js
       
   289      */
       
   290     /* eslint-disable quote-props */
       
   291     if (typeof exports !== 'undefined') {
       
   292         exports['sprintf'] = sprintf
       
   293         exports['vsprintf'] = vsprintf
       
   294     }
       
   295     if (typeof window !== 'undefined') {
       
   296         window['sprintf'] = sprintf
       
   297         window['vsprintf'] = vsprintf
       
   298 
       
   299         if (typeof define === 'function' && define['amd']) {
       
   300             define(function() {
       
   301                 return {
       
   302                     'sprintf': sprintf,
       
   303                     'vsprintf': vsprintf
       
   304                 }
       
   305             })
       
   306         }
       
   307     }
       
   308     /* eslint-enable quote-props */
       
   309 }(); // eslint-disable-line    
       
   310 ||