@konplit-services/common 1.0.361 → 1.0.363
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 +27 -26
- package/build/helper/money.js +43 -27
- package/package.json +1 -1
package/build/helper/money.d.ts
CHANGED
|
@@ -6,40 +6,41 @@ 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 used for percentage-based calculations.
|
|
10
|
-
* The factor is scaled to an integer-based multiplier to ensure precision in Dinero.js.
|
|
11
|
-
* For example, a factor of 1.4 (representing 1.4%) is converted to { amount: 140, scale: 4 } to represent 0.014.
|
|
9
|
+
* Multiplies a Dinero.js monetary amount by a given factor, typically used for percentage-based calculations.
|
|
12
10
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
11
|
+
* This function dynamically calculates the multiplication scale based on the number of decimal places
|
|
12
|
+
* in the factor to maintain precision, then adjusts for percentage scaling by adding 2 to the scale
|
|
13
|
+
* (because factor is a percentage, i.e., factor% = factor/100).
|
|
14
|
+
*
|
|
15
|
+
* The input amount is expected to be in the smallest currency unit (e.g., kobo for NGN, cents for USD).
|
|
16
|
+
*
|
|
17
|
+
* @param a - The Dinero.js monetary amount object, representing money in minor units.
|
|
18
|
+
* @param factor - The multiplier factor, typically a percentage (e.g., 1.4 means 1.4%).
|
|
19
|
+
* Can be any positive decimal number, including those with many decimal places (e.g., 1.00054).
|
|
20
|
+
*
|
|
21
|
+
* @returns A new Dinero.js object representing the result of the multiplication (e.g., the fee or adjusted amount).
|
|
22
|
+
*
|
|
23
|
+
* @throws Error if the calculation results in an invalid amount (e.g., exceeding Number.MAX_SAFE_INTEGER).
|
|
19
24
|
*
|
|
20
25
|
* @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 }
|
|
26
|
+
* // Calculate a 1.4% fee on ₦71.00 (7100 kobo)
|
|
27
|
+
* const amount = dinero({ amount: 7100, currency: NGN });
|
|
28
|
+
* const result = multiplyMoney(amount, 1.4);
|
|
29
|
+
* // result.amount = 994 (kobo) which is ₦9.94
|
|
27
30
|
*
|
|
28
31
|
* @example
|
|
29
|
-
* //
|
|
30
|
-
* const amount = dinero({ amount: 10000, currency:
|
|
31
|
-
* const result = multiplyMoney(amount, 0.05
|
|
32
|
-
* //
|
|
33
|
-
* console.log(result.toJSON()); // { amount: 50, currency: USD, scale: 2 }
|
|
32
|
+
* // Calculate a 0.05% fee on ₦100.00 (10000 kobo)
|
|
33
|
+
* const amount = dinero({ amount: 10000, currency: NGN });
|
|
34
|
+
* const result = multiplyMoney(amount, 0.05);
|
|
35
|
+
* // result.amount = 5 (kobo) which is ₦0.05
|
|
34
36
|
*
|
|
35
37
|
* @example
|
|
36
|
-
* //
|
|
37
|
-
* const amount = dinero({ amount:
|
|
38
|
-
* const result = multiplyMoney(amount,
|
|
39
|
-
* //
|
|
40
|
-
* console.log(result.toJSON()); // { amount: 1000, currency: USD, scale: 2 }
|
|
38
|
+
* // Calculate a 1.00054% fee on ₦2500.00 (250000 kobo)
|
|
39
|
+
* const amount = dinero({ amount: 250000, currency: NGN });
|
|
40
|
+
* const result = multiplyMoney(amount, 1.00054);
|
|
41
|
+
* // result.amount ≈ 2501 (kobo) which is approximately ₦25.01
|
|
41
42
|
*/
|
|
42
|
-
export declare const multiplyMoney: (a: ReturnType<typeof dinero>, factor: number
|
|
43
|
+
export declare const multiplyMoney: (a: ReturnType<typeof dinero>, factor: number) => import("dinero.js").Dinero<number>;
|
|
43
44
|
export declare const divideMoney: (money: ReturnType<typeof dinero>, divisor: number) => import("dinero.js").Dinero<number>;
|
|
44
45
|
export declare const toSmalletUnit: (naira: number | string) => number;
|
|
45
46
|
export {};
|
package/build/helper/money.js
CHANGED
|
@@ -28,43 +28,59 @@ const subtractMoney = (a, b) => {
|
|
|
28
28
|
};
|
|
29
29
|
exports.subtractMoney = subtractMoney;
|
|
30
30
|
/**
|
|
31
|
-
* Multiplies a Dinero.js monetary amount by a factor, typically used for percentage-based calculations.
|
|
32
|
-
* The factor is scaled to an integer-based multiplier to ensure precision in Dinero.js.
|
|
33
|
-
* For example, a factor of 1.4 (representing 1.4%) is converted to { amount: 140, scale: 4 } to represent 0.014.
|
|
31
|
+
* Multiplies a Dinero.js monetary amount by a given factor, typically used for percentage-based calculations.
|
|
34
32
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
33
|
+
* This function dynamically calculates the multiplication scale based on the number of decimal places
|
|
34
|
+
* in the factor to maintain precision, then adjusts for percentage scaling by adding 2 to the scale
|
|
35
|
+
* (because factor is a percentage, i.e., factor% = factor/100).
|
|
36
|
+
*
|
|
37
|
+
* The input amount is expected to be in the smallest currency unit (e.g., kobo for NGN, cents for USD).
|
|
38
|
+
*
|
|
39
|
+
* @param a - The Dinero.js monetary amount object, representing money in minor units.
|
|
40
|
+
* @param factor - The multiplier factor, typically a percentage (e.g., 1.4 means 1.4%).
|
|
41
|
+
* Can be any positive decimal number, including those with many decimal places (e.g., 1.00054).
|
|
42
|
+
*
|
|
43
|
+
* @returns A new Dinero.js object representing the result of the multiplication (e.g., the fee or adjusted amount).
|
|
44
|
+
*
|
|
45
|
+
* @throws Error if the calculation results in an invalid amount (e.g., exceeding Number.MAX_SAFE_INTEGER).
|
|
41
46
|
*
|
|
42
47
|
* @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 }
|
|
48
|
+
* // Calculate a 1.4% fee on ₦71.00 (7100 kobo)
|
|
49
|
+
* const amount = dinero({ amount: 7100, currency: NGN });
|
|
50
|
+
* const result = multiplyMoney(amount, 1.4);
|
|
51
|
+
* // result.amount = 994 (kobo) which is ₦9.94
|
|
49
52
|
*
|
|
50
53
|
* @example
|
|
51
|
-
* //
|
|
52
|
-
* const amount = dinero({ amount: 10000, currency:
|
|
53
|
-
* const result = multiplyMoney(amount, 0.05
|
|
54
|
-
* //
|
|
55
|
-
* console.log(result.toJSON()); // { amount: 50, currency: USD, scale: 2 }
|
|
54
|
+
* // Calculate a 0.05% fee on ₦100.00 (10000 kobo)
|
|
55
|
+
* const amount = dinero({ amount: 10000, currency: NGN });
|
|
56
|
+
* const result = multiplyMoney(amount, 0.05);
|
|
57
|
+
* // result.amount = 5 (kobo) which is ₦0.05
|
|
56
58
|
*
|
|
57
59
|
* @example
|
|
58
|
-
* //
|
|
59
|
-
* const amount = dinero({ amount:
|
|
60
|
-
* const result = multiplyMoney(amount,
|
|
61
|
-
* //
|
|
62
|
-
* console.log(result.toJSON()); // { amount: 1000, currency: USD, scale: 2 }
|
|
60
|
+
* // Calculate a 1.00054% fee on ₦2500.00 (250000 kobo)
|
|
61
|
+
* const amount = dinero({ amount: 250000, currency: NGN });
|
|
62
|
+
* const result = multiplyMoney(amount, 1.00054);
|
|
63
|
+
* // result.amount ≈ 2501 (kobo) which is approximately ₦25.01
|
|
63
64
|
*/
|
|
64
|
-
const multiplyMoney = (a, factor
|
|
65
|
-
|
|
65
|
+
const multiplyMoney = (a, factor) => {
|
|
66
|
+
const decimalPlaces = getDecimalPlaces(factor);
|
|
67
|
+
const scale = decimalPlaces;
|
|
68
|
+
const multiplierAmount = Math.round(factor * Math.pow(10, scale));
|
|
69
|
+
return (0, dinero_js_1.multiply)(a, { amount: multiplierAmount, scale: scale + 2 });
|
|
66
70
|
};
|
|
67
71
|
exports.multiplyMoney = multiplyMoney;
|
|
72
|
+
function getDecimalPlaces(num) {
|
|
73
|
+
const str = num.toString();
|
|
74
|
+
// Handle exponential notation (e.g., 1e-7)
|
|
75
|
+
if (str.includes("e-")) {
|
|
76
|
+
const [, exp] = str.split("e-");
|
|
77
|
+
return parseInt(exp, 10);
|
|
78
|
+
}
|
|
79
|
+
const decimalIndex = str.indexOf(".");
|
|
80
|
+
if (decimalIndex === -1)
|
|
81
|
+
return 0;
|
|
82
|
+
return str.length - decimalIndex - 1;
|
|
83
|
+
}
|
|
68
84
|
const divideMoney = (money, divisor) => {
|
|
69
85
|
const decimal = parseFloat((0, exports.formatMoneyToString)(money));
|
|
70
86
|
const result = Math.round((decimal / divisor) * 100);
|