@win2win/shared 1.0.131 → 1.0.133
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -7,3 +7,6 @@ export declare function deserialize(stringFn?: string, options?: DeserializeOpti
|
|
|
7
7
|
export declare function deserializeAndRun(stringFn?: string): any;
|
|
8
8
|
export declare function deserializeWithContext(stringFn?: string, context?: Record<string, any>): any;
|
|
9
9
|
export declare function deserializeWithContextAndRun(stringFn?: string, context?: Record<string, any>): any;
|
|
10
|
+
export declare function toCurrency(value: number, symbol?: string, options?: {
|
|
11
|
+
decimals: number;
|
|
12
|
+
}): string;
|
|
@@ -4,6 +4,7 @@ exports.deserialize = deserialize;
|
|
|
4
4
|
exports.deserializeAndRun = deserializeAndRun;
|
|
5
5
|
exports.deserializeWithContext = deserializeWithContext;
|
|
6
6
|
exports.deserializeWithContextAndRun = deserializeWithContextAndRun;
|
|
7
|
+
exports.toCurrency = toCurrency;
|
|
7
8
|
function deserialize(stringFn, options) {
|
|
8
9
|
const { context = {}, run = false, useInvisibleContext = false, } = options || {};
|
|
9
10
|
if (!stringFn || typeof stringFn !== "string")
|
|
@@ -11,11 +12,12 @@ function deserialize(stringFn, options) {
|
|
|
11
12
|
const isEmpty = (obj) => JSON.stringify(obj || {}) === "{}";
|
|
12
13
|
try {
|
|
13
14
|
const cleanedStringFn = stringFn.replace(/\/\/.*$/gm, "").trim();
|
|
14
|
-
const fn = new Function("context", `return (...args) => { with (context) { return (${cleanedStringFn})(${
|
|
15
|
+
const fn = new Function("context", `return (...args) => { with (context) { return (${cleanedStringFn})(${useInvisibleContext || isEmpty(context) ? "" : "context,"} ...args) } }`);
|
|
15
16
|
const runableFn = fn(context);
|
|
16
17
|
return run ? runableFn() : runableFn;
|
|
17
18
|
}
|
|
18
19
|
catch (error) {
|
|
20
|
+
console.log("Error deserializing function:" + stringFn, { options });
|
|
19
21
|
console.log(error);
|
|
20
22
|
return run ? null : () => null;
|
|
21
23
|
}
|
|
@@ -37,3 +39,18 @@ function deserializeWithContextAndRun(stringFn, context = {}) {
|
|
|
37
39
|
run: true,
|
|
38
40
|
});
|
|
39
41
|
}
|
|
42
|
+
function toCurrency(value, symbol = "EUR", options) {
|
|
43
|
+
const { decimals = 2 } = options || {};
|
|
44
|
+
const locale = {
|
|
45
|
+
EUR: "de-DE",
|
|
46
|
+
USD: "en-US",
|
|
47
|
+
MXN: "es-MX",
|
|
48
|
+
}[symbol] || "de-DE";
|
|
49
|
+
const formatted = new Intl.NumberFormat(locale, {
|
|
50
|
+
style: "currency",
|
|
51
|
+
currency: symbol,
|
|
52
|
+
minimumFractionDigits: decimals,
|
|
53
|
+
maximumFractionDigits: decimals,
|
|
54
|
+
}).format(value);
|
|
55
|
+
return formatted;
|
|
56
|
+
}
|
package/package.json
CHANGED