aboutsummaryrefslogtreecommitdiff
path: root/extension/lib/util.js
blob: 71bfce4d393ce66ca6821bad43361fe6fa55e7a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
/**
 * Parse an amount that is specified like '5.42 EUR'.
 * Returns a {currency,value,fraction} object or null
 * if the input is invalid.
 */
function amount_parse_pretty(s) {
    let pattern = /(\d+)(.\d+)?\s*([a-zA-Z]+)/;
    let matches = pattern.exec(s);
    if (null == matches) {
        return null;
    }
    return {
        // Always succeeds due to regex
        value: parseInt(matches[1]),
        // Should we warn / fail on lost precision?
        fraction: Math.round(parseFloat(matches[2] || "0") * 1000000),
        currency: matches[3],
    };
}
function format(s, ...args) {
    function r(m, n) {
        let i = parseInt(n);
        return args[i];
    }
    s = s.replace(/{{/g, '{');
    s = s.replace(/}}/g, '}');
    s = s.replace(/{([0-9]+)}/g, r);
    return s;
}
function promiseFinally(p, fn) {
    return p.then((x) => { fn(); return x; })
        .catch((e) => { fn(); throw e; });
}