@stackedapp/utils 2.74.1 → 2.74.2
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.
- package/dist/currency.d.ts +11 -6
- package/dist/currency.js +22 -10
- package/package.json +1 -1
package/dist/currency.d.ts
CHANGED
|
@@ -19,12 +19,17 @@ declare const currency: {
|
|
|
19
19
|
roundDecimalPlaces(val: number, decimalPlaces: number): number;
|
|
20
20
|
roundDownDecimalPlaces(val: number, decimalPlaces: number): number;
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
* Floor `val` to `decimalPlaces`. The scaled value is first snapped to 15
|
|
23
|
+
* significant digits so binary float noise cannot push a value that is exact in
|
|
24
|
+
* decimal below the floor boundary (Math.floor(4.35 * 100) is 434, not 435).
|
|
25
|
+
*/
|
|
26
|
+
floorToDecimalPlaces(val: number, decimalPlaces: number): number;
|
|
27
|
+
/**
|
|
28
|
+
* Floor a reward amount based on reward kind.
|
|
29
|
+
* - loyalty_currency: floor to `roundDecimalPlaces` (the platform Currency's setting, default 0)
|
|
30
|
+
* - coins: floor to `roundDecimalPlaces` (the org asset library currency's `decimalPlaces`, default 1)
|
|
31
|
+
* - discount: returned unchanged (the amount is a fraction)
|
|
32
|
+
* - all others: floor to a whole number
|
|
28
33
|
*/
|
|
29
34
|
roundRewardAmount(amount: number, rewardKind: string, roundDecimalPlaces?: number): number;
|
|
30
35
|
};
|
package/dist/currency.js
CHANGED
|
@@ -51,18 +51,30 @@ const currency = {
|
|
|
51
51
|
return Math.floor(val * factor) / factor;
|
|
52
52
|
},
|
|
53
53
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
54
|
+
* Floor `val` to `decimalPlaces`. The scaled value is first snapped to 15
|
|
55
|
+
* significant digits so binary float noise cannot push a value that is exact in
|
|
56
|
+
* decimal below the floor boundary (Math.floor(4.35 * 100) is 434, not 435).
|
|
57
|
+
*/
|
|
58
|
+
floorToDecimalPlaces(val, decimalPlaces) {
|
|
59
|
+
const factor = 10 ** Math.max(0, Math.trunc(decimalPlaces));
|
|
60
|
+
return Math.floor(Number((val * factor).toPrecision(15))) / factor;
|
|
61
|
+
},
|
|
62
|
+
/**
|
|
63
|
+
* Floor a reward amount based on reward kind.
|
|
64
|
+
* - loyalty_currency: floor to `roundDecimalPlaces` (the platform Currency's setting, default 0)
|
|
65
|
+
* - coins: floor to `roundDecimalPlaces` (the org asset library currency's `decimalPlaces`, default 1)
|
|
66
|
+
* - discount: returned unchanged (the amount is a fraction)
|
|
67
|
+
* - all others: floor to a whole number
|
|
60
68
|
*/
|
|
61
69
|
roundRewardAmount(amount, rewardKind, roundDecimalPlaces) {
|
|
62
|
-
if (rewardKind === '
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
|
|
70
|
+
if (rewardKind === 'discount')
|
|
71
|
+
return amount;
|
|
72
|
+
const decimalPlaces = rewardKind === 'loyalty_currency'
|
|
73
|
+
? (roundDecimalPlaces ?? 0)
|
|
74
|
+
: rewardKind === 'coins'
|
|
75
|
+
? (roundDecimalPlaces ?? 1)
|
|
76
|
+
: 0;
|
|
77
|
+
return currency.floorToDecimalPlaces(amount, decimalPlaces);
|
|
66
78
|
},
|
|
67
79
|
};
|
|
68
80
|
exports.default = currency;
|