@stackedapp/utils 2.74.1 → 2.75.0

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.
@@ -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
- * Round a reward amount based on reward kind.
23
- * - loyalty_currency: round down to min using the currency's decimal places
24
- * - coins: round down to min at 1 decimal place
25
- * - all others: round down to min at 0 decimal places (integer)
26
- *
27
- * "Round down to min" means floor, but if the result would be 0, return the smallest unit instead.
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
- * Round a reward amount based on reward kind.
55
- * - loyalty_currency: round down to min using the currency's decimal places
56
- * - coins: round down to min at 1 decimal place
57
- * - all others: round down to min at 0 decimal places (integer)
58
- *
59
- * "Round down to min" means floor, but if the result would be 0, return the smallest unit instead.
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 === 'loyalty_currency') {
63
- return currency.roundDownDecimalPlaces(amount, roundDecimalPlaces ?? 0);
64
- }
65
- return currency.roundDownDecimalPlaces(amount, rewardKind === 'coins' ? 1 : 0);
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stackedapp/utils",
3
- "version": "2.74.1",
3
+ "version": "2.75.0",
4
4
  "description": "Public utilities for Stacked platform SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",