svghmi/sprintf.js
author Edouard Tisserant
Fri, 06 May 2022 11:02:03 +0200
changeset 3468 3725487d5468
parent 3451 302efcf746e0
child 3486 0d5bb9038e5b
permissions -rw-r--r--
SVGHMI: fix syntax error in sprintf.js and force makefile to regen xslt files when sprintf.js changes
3329
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
/* https://github.com/alexei/sprintf.js/blob/master/src/sprintf.js */
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
/* global window, exports, define */
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
!function() {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
    'use strict'
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     7
    var re = {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     8
        not_string: /[^s]/,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     9
        not_bool: /[^t]/,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
        not_type: /[^T]/,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
        not_primitive: /[^v]/,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
        number: /[diefg]/,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    13
        numeric_arg: /[bcdiefguxX]/,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
        json: /[j]/,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    15
        not_json: /[^j]/,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    16
        text: /^[^\x25]+/,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    17
        modulo: /^\x25{2}/,
3451
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    18
        placeholder: /^\x25(?:([1-9]\d*)\$|\(([^)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijostTuvxXD])/,
3329
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    19
        key: /^([a-z_][a-z_\d]*)/i,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    20
        key_access: /^\.([a-z_][a-z_\d]*)/i,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
        index_access: /^\[(\d+)\]/,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
        sign: /^[+-]/
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    25
    function sprintf(key) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    26
        // arguments is not an array, but should be fine for this call
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    27
        return sprintf_format(sprintf_parse(key), arguments)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    28
    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    29
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
    function vsprintf(fmt, argv) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
        return sprintf.apply(null, [fmt].concat(argv || []))
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    32
    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    33
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    34
    function sprintf_format(parse_tree, argv) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    35
        var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
        for (i = 0; i < tree_length; i++) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
            if (typeof parse_tree[i] === 'string') {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
                output += parse_tree[i]
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
            }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    40
            else if (typeof parse_tree[i] === 'object') {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    41
                ph = parse_tree[i] // convenience purposes only
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    42
                if (ph.keys) { // keyword argument
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    43
                    arg = argv[cursor]
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    44
                    for (k = 0; k < ph.keys.length; k++) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    45
                        if (arg == undefined) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    46
                            throw new Error(sprintf('[sprintf] Cannot access property "%s" of undefined value "%s"', ph.keys[k], ph.keys[k-1]))
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    47
                        }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    48
                        arg = arg[ph.keys[k]]
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    49
                    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    50
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    51
                else if (ph.param_no) { // positional argument (explicit)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    52
                    arg = argv[ph.param_no]
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    53
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    54
                else { // positional argument (implicit)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    55
                    arg = argv[cursor++]
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    56
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    57
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    58
                if (re.not_type.test(ph.type) && re.not_primitive.test(ph.type) && arg instanceof Function) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    59
                    arg = arg()
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    60
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    61
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    62
                if (re.numeric_arg.test(ph.type) && (typeof arg !== 'number' && isNaN(arg))) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    63
                    throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg))
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    64
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    65
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    66
                if (re.number.test(ph.type)) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    67
                    is_positive = arg >= 0
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    68
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    69
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    70
                switch (ph.type) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    71
                    case 'b':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    72
                        arg = parseInt(arg, 10).toString(2)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    73
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    74
                    case 'c':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    75
                        arg = String.fromCharCode(parseInt(arg, 10))
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    76
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    77
                    case 'd':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    78
                    case 'i':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    79
                        arg = parseInt(arg, 10)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    80
                        break
3451
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    81
                    case 'D':
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    82
                        /*  
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    83
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    84
                            select date format with width
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    85
                            select time format with precision
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    86
                            %D  => 13:31 AM (default)
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    87
                            %1D  => 13:31 AM
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    88
                            %.1D  => 07/07/20
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    89
                            %1.1D  => 07/07/20, 13:31 AM
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    90
                            %1.2D  => 07/07/20, 13:31:55 AM
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    91
                            %2.2D  => May 5, 2022, 9:29:16 AM
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    92
                            %3.3D  => May 5, 2022 at 9:28:16 AM GMT+2
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    93
                            %4.4D  => Thursday, May 5, 2022 at 9:26:59 AM Central European Summer Time
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    94
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    95
                            see meaning of DateTimeFormat's options "datestyle" and "timestyle" in MDN 
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    96
                        */
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    97
3468
3725487d5468 SVGHMI: fix syntax error in sprintf.js and force makefile to regen xslt files when sprintf.js changes
Edouard Tisserant
parents: 3451
diff changeset
    98
                        let [datestyle, timestyle] = [ph.width, ph.precision].map(val => ({
3451
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
    99
                            1: "short",
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   100
                            2: "medium",
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   101
                            3: "long",
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   102
                            4: "full"
3468
3725487d5468 SVGHMI: fix syntax error in sprintf.js and force makefile to regen xslt files when sprintf.js changes
Edouard Tisserant
parents: 3451
diff changeset
   103
                        }[val]));
3451
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   104
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   105
                        if(timestyle === undefined && datestyle === undefined){
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   106
                            timestyle = "short";
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   107
                        }
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   108
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   109
                        let options = {
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   110
                            dateStyle: datestyle,
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   111
                            timeStyle: timestyle,
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   112
                            hour12: false
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   113
                        }
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   114
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   115
                        /* get lang from globals */
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   116
                        let lang = get_current_lang_code();
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   117
                        arg = Date(arg).toLocaleString('en-US', options);
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   118
                        
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   119
                        /*    
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   120
                            TODO: select with padding char
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   121
                                  a: absolute time and date (default)
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   122
                                  r: relative time
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   123
                        */
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   124
302efcf746e0 SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents: 3329
diff changeset
   125
                        break
3329
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   126
                    case 'j':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   127
                        arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   128
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   129
                    case 'e':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   130
                        arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential()
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   131
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   132
                    case 'f':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   133
                        arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   134
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   135
                    case 'g':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   136
                        arg = ph.precision ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   137
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   138
                    case 'o':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   139
                        arg = (parseInt(arg, 10) >>> 0).toString(8)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   140
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   141
                    case 's':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   142
                        arg = String(arg)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   143
                        arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   144
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   145
                    case 't':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   146
                        arg = String(!!arg)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   147
                        arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   148
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   149
                    case 'T':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   150
                        arg = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase()
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   151
                        arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   152
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   153
                    case 'u':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   154
                        arg = parseInt(arg, 10) >>> 0
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   155
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   156
                    case 'v':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   157
                        arg = arg.valueOf()
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   158
                        arg = (ph.precision ? arg.substring(0, ph.precision) : arg)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   159
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   160
                    case 'x':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   161
                        arg = (parseInt(arg, 10) >>> 0).toString(16)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   162
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   163
                    case 'X':
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   164
                        arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase()
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   165
                        break
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   166
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   167
                if (re.json.test(ph.type)) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   168
                    output += arg
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   169
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   170
                else {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   171
                    if (re.number.test(ph.type) && (!is_positive || ph.sign)) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   172
                        sign = is_positive ? '+' : '-'
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   173
                        arg = arg.toString().replace(re.sign, '')
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   174
                    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   175
                    else {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   176
                        sign = ''
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   177
                    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   178
                    pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' '
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   179
                    pad_length = ph.width - (sign + arg).length
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   180
                    pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : ''
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   181
                    output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   182
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   183
            }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   184
        }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   185
        return output
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   186
    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   187
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   188
    var sprintf_cache = Object.create(null)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   189
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   190
    function sprintf_parse(fmt) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   191
        if (sprintf_cache[fmt]) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   192
            return sprintf_cache[fmt]
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   193
        }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   194
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   195
        var _fmt = fmt, match, parse_tree = [], arg_names = 0
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   196
        while (_fmt) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   197
            if ((match = re.text.exec(_fmt)) !== null) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   198
                parse_tree.push(match[0])
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   199
            }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   200
            else if ((match = re.modulo.exec(_fmt)) !== null) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   201
                parse_tree.push('%')
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   202
            }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   203
            else if ((match = re.placeholder.exec(_fmt)) !== null) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   204
                if (match[2]) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   205
                    arg_names |= 1
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   206
                    var field_list = [], replacement_field = match[2], field_match = []
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   207
                    if ((field_match = re.key.exec(replacement_field)) !== null) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   208
                        field_list.push(field_match[1])
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   209
                        while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   210
                            if ((field_match = re.key_access.exec(replacement_field)) !== null) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   211
                                field_list.push(field_match[1])
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   212
                            }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   213
                            else if ((field_match = re.index_access.exec(replacement_field)) !== null) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   214
                                field_list.push(field_match[1])
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   215
                            }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   216
                            else {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   217
                                throw new SyntaxError('[sprintf] failed to parse named argument key')
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   218
                            }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   219
                        }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   220
                    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   221
                    else {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   222
                        throw new SyntaxError('[sprintf] failed to parse named argument key')
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   223
                    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   224
                    match[2] = field_list
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   225
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   226
                else {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   227
                    arg_names |= 2
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   228
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   229
                if (arg_names === 3) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   230
                    throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported')
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   231
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   232
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   233
                parse_tree.push(
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   234
                    {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   235
                        placeholder: match[0],
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   236
                        param_no:    match[1],
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   237
                        keys:        match[2],
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   238
                        sign:        match[3],
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   239
                        pad_char:    match[4],
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   240
                        align:       match[5],
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   241
                        width:       match[6],
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   242
                        precision:   match[7],
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   243
                        type:        match[8]
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   244
                    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   245
                )
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   246
            }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   247
            else {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   248
                throw new SyntaxError('[sprintf] unexpected placeholder')
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   249
            }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   250
            _fmt = _fmt.substring(match[0].length)
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   251
        }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   252
        return sprintf_cache[fmt] = parse_tree
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   253
    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   254
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   255
    /**
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   256
     * export to either browser or node.js
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   257
     */
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   258
    /* eslint-disable quote-props */
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   259
    if (typeof exports !== 'undefined') {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   260
        exports['sprintf'] = sprintf
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   261
        exports['vsprintf'] = vsprintf
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   262
    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   263
    if (typeof window !== 'undefined') {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   264
        window['sprintf'] = sprintf
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   265
        window['vsprintf'] = vsprintf
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   266
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   267
        if (typeof define === 'function' && define['amd']) {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   268
            define(function() {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   269
                return {
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   270
                    'sprintf': sprintf,
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   271
                    'vsprintf': vsprintf
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   272
                }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   273
            })
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   274
        }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   275
    }
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   276
    /* eslint-enable quote-props */
d44b6346d4c2 SVGHMI: Move sprintf.js out of widget_display.ysl2 since it is used in other widgets as well
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   277
}(); // eslint-disable-line