@konplit-services/common 1.0.361 → 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.
- package/build/helper/money.d.ts +14 -26
- package/build/helper/money.js +25 -27
- package/package.json +1 -1
package/build/helper/money.d.ts
CHANGED
|
@@ -6,40 +6,28 @@ export declare const formatMoneyToNumber: (money: ReturnType<typeof dinero>) =>
|
|
|
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
8
|
/**
|
|
9
|
-
* Multiplies a Dinero.js monetary amount by a factor, typically
|
|
10
|
-
* The
|
|
11
|
-
*
|
|
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
12
|
*
|
|
13
|
-
* @param a - The Dinero.js object representing the monetary amount (e.g., in
|
|
13
|
+
* @param a - The Dinero.js object representing the monetary amount (e.g., in kobo for NGN).
|
|
14
14
|
* @param factor - The multiplier, typically a percentage rate (e.g., 1.4 for 1.4%).
|
|
15
|
-
* @param scale - The scale for the multiplier, determining the decimal precision (default: 2).
|
|
16
|
-
* For percentages, use scale = 4 for one decimal (e.g., 1.4%) or scale = 5 for two decimals (e.g., 1.45%).
|
|
17
15
|
* @returns A new Dinero.js object representing the result of the multiplication.
|
|
18
|
-
* @throws Error if the resulting amount is invalid (e.g., exceeds Number.MAX_SAFE_INTEGER
|
|
16
|
+
* @throws Error if the resulting amount is invalid (e.g., exceeds Number.MAX_SAFE_INTEGER).
|
|
19
17
|
*
|
|
20
18
|
* @example
|
|
21
|
-
* //
|
|
22
|
-
*
|
|
23
|
-
* const
|
|
24
|
-
*
|
|
25
|
-
* // Result: Dinero object with amount ≈ 994 (99.4 cents, or 0.994 USD)
|
|
26
|
-
* console.log(result.toJSON()); // { amount: 994, currency: USD, scale: 2 }
|
|
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)
|
|
27
23
|
*
|
|
28
24
|
* @example
|
|
29
|
-
* //
|
|
30
|
-
* const amount = dinero({ amount: 10000, currency:
|
|
31
|
-
* const result = multiplyMoney(amount, 0.05
|
|
32
|
-
* // Result:
|
|
33
|
-
* console.log(result.toJSON()); // { amount: 50, currency: USD, scale: 2 }
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* // Example 3: Using default scale (2) for a simple multiplier
|
|
37
|
-
* const amount = dinero({ amount: 500, currency: USD }); // 5.00 USD
|
|
38
|
-
* const result = multiplyMoney(amount, 2); // Multiply by 2 (200%)
|
|
39
|
-
* // Result: Dinero object with amount = 1000 (10.00 USD)
|
|
40
|
-
* console.log(result.toJSON()); // { amount: 1000, currency: USD, scale: 2 }
|
|
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)
|
|
41
29
|
*/
|
|
42
|
-
export declare const multiplyMoney: (a: ReturnType<typeof dinero>, factor: number
|
|
30
|
+
export declare const multiplyMoney: (a: ReturnType<typeof dinero>, factor: number) => import("dinero.js").Dinero<number>;
|
|
43
31
|
export declare const divideMoney: (money: ReturnType<typeof dinero>, divisor: number) => import("dinero.js").Dinero<number>;
|
|
44
32
|
export declare const toSmalletUnit: (naira: number | string) => number;
|
|
45
33
|
export {};
|
package/build/helper/money.js
CHANGED
|
@@ -28,43 +28,41 @@ const subtractMoney = (a, b) => {
|
|
|
28
28
|
};
|
|
29
29
|
exports.subtractMoney = subtractMoney;
|
|
30
30
|
/**
|
|
31
|
-
* Multiplies a Dinero.js monetary amount by a factor, typically
|
|
32
|
-
* The
|
|
33
|
-
*
|
|
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
34
|
*
|
|
35
|
-
* @param a - The Dinero.js object representing the monetary amount (e.g., in
|
|
35
|
+
* @param a - The Dinero.js object representing the monetary amount (e.g., in kobo for NGN).
|
|
36
36
|
* @param factor - The multiplier, typically a percentage rate (e.g., 1.4 for 1.4%).
|
|
37
|
-
* @param scale - The scale for the multiplier, determining the decimal precision (default: 2).
|
|
38
|
-
* For percentages, use scale = 4 for one decimal (e.g., 1.4%) or scale = 5 for two decimals (e.g., 1.45%).
|
|
39
37
|
* @returns A new Dinero.js object representing the result of the multiplication.
|
|
40
|
-
* @throws Error if the resulting amount is invalid (e.g., exceeds Number.MAX_SAFE_INTEGER
|
|
38
|
+
* @throws Error if the resulting amount is invalid (e.g., exceeds Number.MAX_SAFE_INTEGER).
|
|
41
39
|
*
|
|
42
40
|
* @example
|
|
43
|
-
* //
|
|
44
|
-
*
|
|
45
|
-
* const
|
|
46
|
-
*
|
|
47
|
-
* // Result: Dinero object with amount ≈ 994 (99.4 cents, or 0.994 USD)
|
|
48
|
-
* console.log(result.toJSON()); // { amount: 994, currency: USD, scale: 2 }
|
|
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)
|
|
49
45
|
*
|
|
50
46
|
* @example
|
|
51
|
-
* //
|
|
52
|
-
* const amount = dinero({ amount: 10000, currency:
|
|
53
|
-
* const result = multiplyMoney(amount, 0.05
|
|
54
|
-
* // Result:
|
|
55
|
-
* console.log(result.toJSON()); // { amount: 50, currency: USD, scale: 2 }
|
|
56
|
-
*
|
|
57
|
-
* @example
|
|
58
|
-
* // Example 3: Using default scale (2) for a simple multiplier
|
|
59
|
-
* const amount = dinero({ amount: 500, currency: USD }); // 5.00 USD
|
|
60
|
-
* const result = multiplyMoney(amount, 2); // Multiply by 2 (200%)
|
|
61
|
-
* // Result: Dinero object with amount = 1000 (10.00 USD)
|
|
62
|
-
* console.log(result.toJSON()); // { amount: 1000, currency: USD, scale: 2 }
|
|
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)
|
|
63
51
|
*/
|
|
64
|
-
const multiplyMoney = (a, factor
|
|
65
|
-
|
|
52
|
+
const multiplyMoney = (a, factor) => {
|
|
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 });
|
|
66
57
|
};
|
|
67
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
|
+
}
|
|
68
66
|
const divideMoney = (money, divisor) => {
|
|
69
67
|
const decimal = parseFloat((0, exports.formatMoneyToString)(money));
|
|
70
68
|
const result = Math.round((decimal / divisor) * 100);
|