@konplit-services/common 1.0.359 → 1.0.361
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 +35 -1
- package/build/helper/money.js +36 -2
- package/build/swagger/config.js +5 -0
- package/package.json +1 -1
package/build/helper/money.d.ts
CHANGED
|
@@ -5,7 +5,41 @@ 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
|
-
|
|
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.
|
|
12
|
+
*
|
|
13
|
+
* @param a - The Dinero.js object representing the monetary amount (e.g., in the currency's smallest unit like kobo for NGN).
|
|
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
|
+
* @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 or is non-integer).
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // Example 1: Calculate 1.4% fee on 7100 kobo (71.00 NGN)
|
|
22
|
+
* import { dinero, USD } from 'dinero.js';
|
|
23
|
+
* const amount = dinero({ amount: 7100, currency: USD }); // 71.00 USD (assuming USD for simplicity)
|
|
24
|
+
* const result = multiplyMoney(amount, 1.4, 4);
|
|
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 }
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* // Example 2: Calculate 0.05% fee on 10000 kobo (100.00 NGN) with higher precision
|
|
30
|
+
* const amount = dinero({ amount: 10000, currency: USD });
|
|
31
|
+
* const result = multiplyMoney(amount, 0.05, 5);
|
|
32
|
+
* // Result: Dinero object with amount ≈ 50 (5 cents, or 0.05 USD)
|
|
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 }
|
|
41
|
+
*/
|
|
42
|
+
export declare const multiplyMoney: (a: ReturnType<typeof dinero>, factor: number, scale?: number) => import("dinero.js").Dinero<number>;
|
|
9
43
|
export declare const divideMoney: (money: ReturnType<typeof dinero>, divisor: number) => import("dinero.js").Dinero<number>;
|
|
10
44
|
export declare const toSmalletUnit: (naira: number | string) => number;
|
|
11
45
|
export {};
|
package/build/helper/money.js
CHANGED
|
@@ -27,8 +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
|
-
|
|
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.
|
|
34
|
+
*
|
|
35
|
+
* @param a - The Dinero.js object representing the monetary amount (e.g., in the currency's smallest unit like kobo for NGN).
|
|
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
|
+
* @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 or is non-integer).
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* // Example 1: Calculate 1.4% fee on 7100 kobo (71.00 NGN)
|
|
44
|
+
* import { dinero, USD } from 'dinero.js';
|
|
45
|
+
* const amount = dinero({ amount: 7100, currency: USD }); // 71.00 USD (assuming USD for simplicity)
|
|
46
|
+
* const result = multiplyMoney(amount, 1.4, 4);
|
|
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 }
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* // Example 2: Calculate 0.05% fee on 10000 kobo (100.00 NGN) with higher precision
|
|
52
|
+
* const amount = dinero({ amount: 10000, currency: USD });
|
|
53
|
+
* const result = multiplyMoney(amount, 0.05, 5);
|
|
54
|
+
* // Result: Dinero object with amount ≈ 50 (5 cents, or 0.05 USD)
|
|
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 }
|
|
63
|
+
*/
|
|
64
|
+
const multiplyMoney = (a, factor, scale = 2) => {
|
|
65
|
+
return (0, dinero_js_1.multiply)(a, { amount: factor * 100, scale }); // Convert decimal to Dinero scale
|
|
32
66
|
};
|
|
33
67
|
exports.multiplyMoney = multiplyMoney;
|
|
34
68
|
const divideMoney = (money, divisor) => {
|
package/build/swagger/config.js
CHANGED
|
@@ -55,5 +55,10 @@ const configureSwagger = (app, paths) => {
|
|
|
55
55
|
// customCssUrl:
|
|
56
56
|
// "https://cdn.jsdelivr.net/npm/swagger-ui-themes@3.0.0/themes/3.x/theme-newspaper.css",
|
|
57
57
|
}));
|
|
58
|
+
// ➕ Serve raw Swagger JSON
|
|
59
|
+
app.get("/api-json", (_, res) => {
|
|
60
|
+
res.setHeader("Content-Type", "application/json");
|
|
61
|
+
res.send(swaggerSpec);
|
|
62
|
+
});
|
|
58
63
|
};
|
|
59
64
|
exports.configureSwagger = configureSwagger;
|