@konplit-services/common 1.0.360 → 1.0.362

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.
@@ -5,6 +5,28 @@ export declare const formatMoneyToString: (money: ReturnType<typeof dinero>) =>
5
5
  export declare const formatMoneyToNumber: (money: ReturnType<typeof dinero>) => number;
6
6
  export declare const sumMoney: (a: ReturnType<typeof dinero>, b: ReturnType<typeof dinero>) => import("dinero.js").Dinero<number>;
7
7
  export declare const subtractMoney: (a: ReturnType<typeof dinero>, b: ReturnType<typeof dinero>) => import("dinero.js").Dinero<number>;
8
+ /**
9
+ * Multiplies a Dinero.js monetary amount by a factor, typically for percentage-based calculations.
10
+ * The scale is dynamically calculated based on the number of decimal places in the factor,
11
+ * plus 2 for the percentage conversion (÷100) and 2 for scaling (*100).
12
+ *
13
+ * @param a - The Dinero.js object representing the monetary amount (e.g., in kobo for NGN).
14
+ * @param factor - The multiplier, typically a percentage rate (e.g., 1.4 for 1.4%).
15
+ * @returns A new Dinero.js object representing the result of the multiplication.
16
+ * @throws Error if the resulting amount is invalid (e.g., exceeds Number.MAX_SAFE_INTEGER).
17
+ *
18
+ * @example
19
+ * // Calculate 1.4% fee on 7100 kobo (71.00 NGN)
20
+ * const amount = dinero({ amount: 7100, currency: NGN });
21
+ * const result = multiplyMoney(amount, 1.4);
22
+ * // Result: { amount: 994, currency: NGN, scale: 2 } (99.4 kobo)
23
+ *
24
+ * @example
25
+ * // Calculate 0.05% fee on 10000 kobo (100.00 NGN)
26
+ * const amount = dinero({ amount: 10000, currency: NGN });
27
+ * const result = multiplyMoney(amount, 0.05);
28
+ * // Result: { amount: 50, currency: NGN, scale: 2 } (5 kobo)
29
+ */
8
30
  export declare const multiplyMoney: (a: ReturnType<typeof dinero>, factor: number) => import("dinero.js").Dinero<number>;
9
31
  export declare const divideMoney: (money: ReturnType<typeof dinero>, divisor: number) => import("dinero.js").Dinero<number>;
10
32
  export declare const toSmalletUnit: (naira: number | string) => number;
@@ -27,10 +27,42 @@ const subtractMoney = (a, b) => {
27
27
  return (0, dinero_js_1.subtract)(a, b);
28
28
  };
29
29
  exports.subtractMoney = subtractMoney;
30
+ /**
31
+ * Multiplies a Dinero.js monetary amount by a factor, typically for percentage-based calculations.
32
+ * The scale is dynamically calculated based on the number of decimal places in the factor,
33
+ * plus 2 for the percentage conversion (÷100) and 2 for scaling (*100).
34
+ *
35
+ * @param a - The Dinero.js object representing the monetary amount (e.g., in kobo for NGN).
36
+ * @param factor - The multiplier, typically a percentage rate (e.g., 1.4 for 1.4%).
37
+ * @returns A new Dinero.js object representing the result of the multiplication.
38
+ * @throws Error if the resulting amount is invalid (e.g., exceeds Number.MAX_SAFE_INTEGER).
39
+ *
40
+ * @example
41
+ * // Calculate 1.4% fee on 7100 kobo (71.00 NGN)
42
+ * const amount = dinero({ amount: 7100, currency: NGN });
43
+ * const result = multiplyMoney(amount, 1.4);
44
+ * // Result: { amount: 994, currency: NGN, scale: 2 } (99.4 kobo)
45
+ *
46
+ * @example
47
+ * // Calculate 0.05% fee on 10000 kobo (100.00 NGN)
48
+ * const amount = dinero({ amount: 10000, currency: NGN });
49
+ * const result = multiplyMoney(amount, 0.05);
50
+ * // Result: { amount: 50, currency: NGN, scale: 2 } (5 kobo)
51
+ */
30
52
  const multiplyMoney = (a, factor) => {
31
- return (0, dinero_js_1.multiply)(a, { amount: factor * 100, scale: 2 }); // Convert decimal to Dinero scale
53
+ const decimalPlaces = getDecimalPlaces(factor);
54
+ const scale = decimalPlaces + 2 + 2; // Decimal places + 2 (*100) + 2 (÷100)
55
+ const multiplierAmount = Math.round(factor * 100); // Ensure integer
56
+ return (0, dinero_js_1.multiply)(a, { amount: multiplierAmount, scale });
32
57
  };
33
58
  exports.multiplyMoney = multiplyMoney;
59
+ function getDecimalPlaces(num) {
60
+ const str = num.toString();
61
+ const decimalIndex = str.indexOf(".");
62
+ if (decimalIndex === -1)
63
+ return 0;
64
+ return str.length - decimalIndex - 1;
65
+ }
34
66
  const divideMoney = (money, divisor) => {
35
67
  const decimal = parseFloat((0, exports.formatMoneyToString)(money));
36
68
  const result = Math.round((decimal / divisor) * 100);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konplit-services/common",
3
- "version": "1.0.360",
3
+ "version": "1.0.362",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",