author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Mon, 15 Apr 2024 09:09:59 +0200 | |
changeset 3932 | 62d04933d569 |
parent 3837 | efe0b5b21842 |
permissions | -rw-r--r-- |
3360
746e3e3f6537
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 */ |
746e3e3f6537
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 */ |
746e3e3f6537
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 |
|
746e3e3f6537
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() { |
746e3e3f6537
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' |
746e3e3f6537
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 |
|
746e3e3f6537
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 = { |
746e3e3f6537
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]/, |
746e3e3f6537
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]/, |
746e3e3f6537
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]/, |
746e3e3f6537
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]/, |
746e3e3f6537
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]/, |
746e3e3f6537
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]/, |
746e3e3f6537
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]/, |
746e3e3f6537
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]/, |
746e3e3f6537
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]+/, |
746e3e3f6537
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:
3360
diff
changeset
|
18 |
placeholder: /^\x25(?:([1-9]\d*)\$|\(([^)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijostTuvxXD])/, |
3360
746e3e3f6537
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, |
746e3e3f6537
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, |
746e3e3f6537
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+)\]/, |
746e3e3f6537
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: /^[+-]/ |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
|
746e3e3f6537
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) { |
746e3e3f6537
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 |
746e3e3f6537
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) |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
|
746e3e3f6537
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) { |
746e3e3f6537
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 || [])) |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
|
746e3e3f6537
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) { |
746e3e3f6537
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 |
746e3e3f6537
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++) { |
746e3e3f6537
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') { |
746e3e3f6537
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] |
746e3e3f6537
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 |
} |
746e3e3f6537
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') { |
746e3e3f6537
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 |
746e3e3f6537
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 |
746e3e3f6537
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] |
746e3e3f6537
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++) { |
746e3e3f6537
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) { |
746e3e3f6537
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])) |
746e3e3f6537
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 |
} |
746e3e3f6537
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]] |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
} |
746e3e3f6537
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) |
746e3e3f6537
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] |
746e3e3f6537
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 |
} |
746e3e3f6537
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) |
746e3e3f6537
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++] |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
|
746e3e3f6537
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) { |
746e3e3f6537
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() |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
|
3837
efe0b5b21842
SVGHMI: add support of all IEC61131 interger types.
Edouard Tisserant
parents:
3486
diff
changeset
|
62 |
if (re.numeric_arg.test(ph.type)){ |
efe0b5b21842
SVGHMI: add support of all IEC61131 interger types.
Edouard Tisserant
parents:
3486
diff
changeset
|
63 |
let argtype = typeof arg; |
efe0b5b21842
SVGHMI: add support of all IEC61131 interger types.
Edouard Tisserant
parents:
3486
diff
changeset
|
64 |
if ( argtype !== 'bigint') { |
efe0b5b21842
SVGHMI: add support of all IEC61131 interger types.
Edouard Tisserant
parents:
3486
diff
changeset
|
65 |
if ( argtype !== 'number' && isNaN(arg) ) { |
efe0b5b21842
SVGHMI: add support of all IEC61131 interger types.
Edouard Tisserant
parents:
3486
diff
changeset
|
66 |
throw new TypeError(sprintf('[sprintf] expecting number but found %T', arg)) |
efe0b5b21842
SVGHMI: add support of all IEC61131 interger types.
Edouard Tisserant
parents:
3486
diff
changeset
|
67 |
} |
efe0b5b21842
SVGHMI: add support of all IEC61131 interger types.
Edouard Tisserant
parents:
3486
diff
changeset
|
68 |
} |
3360
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
|
746e3e3f6537
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 |
if (re.number.test(ph.type)) { |
746e3e3f6537
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 |
is_positive = arg >= 0 |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
|
746e3e3f6537
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 |
switch (ph.type) { |
746e3e3f6537
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 |
case 'b': |
746e3e3f6537
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 |
arg = parseInt(arg, 10).toString(2) |
746e3e3f6537
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 |
break |
746e3e3f6537
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 |
case 'c': |
746e3e3f6537
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 |
arg = String.fromCharCode(parseInt(arg, 10)) |
746e3e3f6537
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
|
81 |
break |
746e3e3f6537
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
|
82 |
case 'd': |
746e3e3f6537
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
|
83 |
case 'i': |
746e3e3f6537
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
|
84 |
arg = parseInt(arg, 10) |
746e3e3f6537
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
|
85 |
break |
3451
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
86 |
case 'D': |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
87 |
/* |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
88 |
|
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
89 |
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:
3360
diff
changeset
|
90 |
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:
3360
diff
changeset
|
91 |
%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:
3360
diff
changeset
|
92 |
%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:
3360
diff
changeset
|
93 |
%.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:
3360
diff
changeset
|
94 |
%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:
3360
diff
changeset
|
95 |
%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:
3360
diff
changeset
|
96 |
%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:
3360
diff
changeset
|
97 |
%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:
3360
diff
changeset
|
98 |
%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:
3360
diff
changeset
|
99 |
|
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
100 |
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:
3360
diff
changeset
|
101 |
*/ |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
102 |
|
3472
2fb9849c6721
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 |
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:
3360
diff
changeset
|
104 |
1: "short", |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
105 |
2: "medium", |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
106 |
3: "long", |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
107 |
4: "full" |
3472
2fb9849c6721
SVGHMI: fix syntax error in sprintf.js and force makefile to regen xslt files when sprintf.js changes
Edouard Tisserant
parents:
3451
diff
changeset
|
108 |
}[val])); |
3451
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
109 |
|
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
110 |
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:
3360
diff
changeset
|
111 |
timestyle = "short"; |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
112 |
} |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
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:
3360
diff
changeset
|
114 |
let options = { |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
115 |
dateStyle: datestyle, |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
116 |
timeStyle: timestyle, |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
117 |
hour12: false |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
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:
3360
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:
3360
diff
changeset
|
120 |
/* 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:
3360
diff
changeset
|
121 |
let lang = get_current_lang_code(); |
3486
0d5bb9038e5b
SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents:
3472
diff
changeset
|
122 |
let f; |
0d5bb9038e5b
SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents:
3472
diff
changeset
|
123 |
try{ |
0d5bb9038e5b
SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents:
3472
diff
changeset
|
124 |
f = new Intl.DateTimeFormat(lang, options); |
0d5bb9038e5b
SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents:
3472
diff
changeset
|
125 |
} catch(e) { |
0d5bb9038e5b
SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents:
3472
diff
changeset
|
126 |
f = new Intl.DateTimeFormat('en-US', options); |
0d5bb9038e5b
SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents:
3472
diff
changeset
|
127 |
} |
0d5bb9038e5b
SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents:
3472
diff
changeset
|
128 |
arg = f.format(arg); |
0d5bb9038e5b
SVGHMI: prevent exception with date/time formating in sprintf.js if lang wasn't already set.
Edouard Tisserant
parents:
3472
diff
changeset
|
129 |
|
3451
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
130 |
/* |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
131 |
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:
3360
diff
changeset
|
132 |
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:
3360
diff
changeset
|
133 |
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:
3360
diff
changeset
|
134 |
*/ |
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
135 |
|
302efcf746e0
SVGHMI: add localized Date and Time support to sprintf.js. Use with "%D" or "%2.1D" format style.
Edouard Tisserant
parents:
3360
diff
changeset
|
136 |
break |
3360
746e3e3f6537
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 |
case 'j': |
746e3e3f6537
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 |
arg = JSON.stringify(arg, null, ph.width ? parseInt(ph.width) : 0) |
746e3e3f6537
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 |
break |
746e3e3f6537
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 |
case 'e': |
746e3e3f6537
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 |
arg = ph.precision ? parseFloat(arg).toExponential(ph.precision) : parseFloat(arg).toExponential() |
746e3e3f6537
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 |
break |
746e3e3f6537
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 |
case 'f': |
746e3e3f6537
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 |
arg = ph.precision ? parseFloat(arg).toFixed(ph.precision) : parseFloat(arg) |
746e3e3f6537
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 |
break |
746e3e3f6537
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 |
case 'g': |
746e3e3f6537
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 ? String(Number(arg.toPrecision(ph.precision))) : parseFloat(arg) |
746e3e3f6537
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 |
746e3e3f6537
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 'o': |
746e3e3f6537
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 = (parseInt(arg, 10) >>> 0).toString(8) |
746e3e3f6537
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 |
break |
746e3e3f6537
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 |
case 's': |
746e3e3f6537
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 |
arg = String(arg) |
746e3e3f6537
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 = (ph.precision ? arg.substring(0, ph.precision) : arg) |
746e3e3f6537
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 |
746e3e3f6537
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 't': |
746e3e3f6537
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 = String(!!arg) |
746e3e3f6537
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) |
746e3e3f6537
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 |
746e3e3f6537
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 'T': |
746e3e3f6537
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 = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase() |
746e3e3f6537
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 |
arg = (ph.precision ? arg.substring(0, ph.precision) : arg) |
746e3e3f6537
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 |
break |
746e3e3f6537
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 |
case 'u': |
746e3e3f6537
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 |
arg = parseInt(arg, 10) >>> 0 |
746e3e3f6537
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 |
break |
746e3e3f6537
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 |
case 'v': |
746e3e3f6537
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 |
arg = arg.valueOf() |
746e3e3f6537
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 |
arg = (ph.precision ? arg.substring(0, ph.precision) : arg) |
746e3e3f6537
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 |
break |
746e3e3f6537
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 |
case 'x': |
746e3e3f6537
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 |
arg = (parseInt(arg, 10) >>> 0).toString(16) |
746e3e3f6537
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 |
break |
746e3e3f6537
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 |
case 'X': |
746e3e3f6537
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 |
arg = (parseInt(arg, 10) >>> 0).toString(16).toUpperCase() |
746e3e3f6537
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 |
break |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
if (re.json.test(ph.type)) { |
746e3e3f6537
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 |
output += arg |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
else { |
746e3e3f6537
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 |
if (re.number.test(ph.type) && (!is_positive || ph.sign)) { |
746e3e3f6537
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 |
sign = is_positive ? '+' : '-' |
746e3e3f6537
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 |
arg = arg.toString().replace(re.sign, '') |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
else { |
746e3e3f6537
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 |
sign = '' |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
pad_character = ph.pad_char ? ph.pad_char === '0' ? '0' : ph.pad_char.charAt(1) : ' ' |
746e3e3f6537
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 |
pad_length = ph.width - (sign + arg).length |
746e3e3f6537
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 |
pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : '' |
746e3e3f6537
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 |
output += ph.align ? sign + arg + pad : (pad_character === '0' ? sign + pad + arg : pad + sign + arg) |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
return output |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
|
746e3e3f6537
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 |
var sprintf_cache = Object.create(null) |
746e3e3f6537
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 |
|
746e3e3f6537
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 |
function sprintf_parse(fmt) { |
746e3e3f6537
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 |
if (sprintf_cache[fmt]) { |
746e3e3f6537
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 |
return sprintf_cache[fmt] |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
|
746e3e3f6537
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 _fmt = fmt, match, parse_tree = [], arg_names = 0 |
746e3e3f6537
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 |
while (_fmt) { |
746e3e3f6537
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 |
if ((match = re.text.exec(_fmt)) !== null) { |
746e3e3f6537
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 |
parse_tree.push(match[0]) |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
else if ((match = re.modulo.exec(_fmt)) !== null) { |
746e3e3f6537
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 |
parse_tree.push('%') |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
else if ((match = re.placeholder.exec(_fmt)) !== null) { |
746e3e3f6537
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 |
if (match[2]) { |
746e3e3f6537
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 |
arg_names |= 1 |
746e3e3f6537
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 |
var field_list = [], replacement_field = match[2], field_match = [] |
746e3e3f6537
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 |
if ((field_match = re.key.exec(replacement_field)) !== null) { |
746e3e3f6537
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 |
field_list.push(field_match[1]) |
746e3e3f6537
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 |
while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') { |
746e3e3f6537
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 |
if ((field_match = re.key_access.exec(replacement_field)) !== null) { |
746e3e3f6537
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 |
field_list.push(field_match[1]) |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
else if ((field_match = re.index_access.exec(replacement_field)) !== null) { |
746e3e3f6537
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 |
field_list.push(field_match[1]) |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
else { |
746e3e3f6537
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 |
throw new SyntaxError('[sprintf] failed to parse named argument key') |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
else { |
746e3e3f6537
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 |
throw new SyntaxError('[sprintf] failed to parse named argument key') |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
match[2] = field_list |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
else { |
746e3e3f6537
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 |
arg_names |= 2 |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
if (arg_names === 3) { |
746e3e3f6537
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 |
throw new Error('[sprintf] mixing positional and named placeholders is not (yet) supported') |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
|
746e3e3f6537
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 |
parse_tree.push( |
746e3e3f6537
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 |
{ |
746e3e3f6537
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 |
placeholder: match[0], |
746e3e3f6537
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 |
param_no: match[1], |
746e3e3f6537
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 |
keys: match[2], |
746e3e3f6537
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 |
sign: match[3], |
746e3e3f6537
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 |
pad_char: match[4], |
746e3e3f6537
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 |
align: match[5], |
746e3e3f6537
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 |
width: match[6], |
746e3e3f6537
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 |
precision: match[7], |
746e3e3f6537
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 |
type: match[8] |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
) |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
else { |
746e3e3f6537
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 |
throw new SyntaxError('[sprintf] unexpected placeholder') |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
_fmt = _fmt.substring(match[0].length) |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
return sprintf_cache[fmt] = parse_tree |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
|
746e3e3f6537
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 |
/** |
746e3e3f6537
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 |
* export to either browser or node.js |
746e3e3f6537
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 |
*/ |
746e3e3f6537
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 |
/* eslint-disable quote-props */ |
746e3e3f6537
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 |
if (typeof exports !== 'undefined') { |
746e3e3f6537
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 |
exports['sprintf'] = sprintf |
746e3e3f6537
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 |
exports['vsprintf'] = vsprintf |
746e3e3f6537
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 |
} |
746e3e3f6537
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 |
if (typeof window !== 'undefined') { |
746e3e3f6537
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 |
window['sprintf'] = sprintf |
746e3e3f6537
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 |
window['vsprintf'] = vsprintf |
746e3e3f6537
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 |
|
746e3e3f6537
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
|
278 |
if (typeof define === 'function' && define['amd']) { |
746e3e3f6537
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
|
279 |
define(function() { |
746e3e3f6537
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
|
280 |
return { |
746e3e3f6537
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
|
281 |
'sprintf': sprintf, |
746e3e3f6537
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
|
282 |
'vsprintf': vsprintf |
746e3e3f6537
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
|
283 |
} |
746e3e3f6537
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
|
284 |
}) |
746e3e3f6537
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
|
285 |
} |
746e3e3f6537
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
|
286 |
} |
746e3e3f6537
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
|
287 |
/* eslint-enable quote-props */ |
746e3e3f6537
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
|
288 |
}(); // eslint-disable-line |