b2b-platform-utils 1.1.8 → 1.1.9

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.
Files changed (2) hide show
  1. package/amountFormat.js +97 -0
  2. package/package.json +1 -1
@@ -0,0 +1,97 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * @module amountFormat
5
+ * @description
6
+ * Utility for consistent conversion between major (e.g. 17.00 EUR)
7
+ * and minor units (e.g. 1700 cents).
8
+ *
9
+ * All conversions are currency-aware and respect currency exponents:
10
+ * EUR/USD/GBP → 2 decimals, JPY → 0 decimals, KWD → 3 decimals.
11
+ */
12
+
13
+ const { getBaseCurrency } = require('./localCache');
14
+
15
+ /**
16
+ * Convert minor units (e.g. 1700) to major amount (e.g. 17.00)
17
+ *
18
+ * @param {number|string} amount - Minor amount in integer form.
19
+ * @param {string} [currency=getBaseCurrency()] - ISO 4217 currency code.
20
+ * @returns {number} Major amount.
21
+ * @throws {Error} If amount is not numeric.
22
+ */
23
+ function toMajorAmount(amount, currency = getBaseCurrency()) {
24
+ if (amount == null) return 0;
25
+
26
+ const exponent = getCurrencyExponent(currency);
27
+ const num = Number(amount);
28
+
29
+ if (!Number.isFinite(num)) {
30
+ throw new Error('Invalid amount');
31
+ }
32
+
33
+ // Avoid unnecessary floating-point drift.
34
+ const factor = Math.pow(10, exponent);
35
+ const major = num / factor;
36
+
37
+ return parseFloat(major.toFixed(exponent));
38
+ }
39
+
40
+ /**
41
+ * Convert major amount (e.g. 17.00) to minor units (e.g. 1700)
42
+ *
43
+ * @param {number|string} amount - Major amount.
44
+ * @param {string} [currency=getBaseCurrency()] - ISO 4217 currency code.
45
+ * @returns {number} Minor amount as integer.
46
+ * @throws {Error} If amount is missing or invalid.
47
+ */
48
+ function toMinorAmount(amount, currency = getBaseCurrency()) {
49
+ if (amount == null) {
50
+ throw new Error('Amount is required');
51
+ }
52
+
53
+ const exponent = getCurrencyExponent(currency);
54
+ const normalized = String(amount).replace(',', '.'); // support "17,5"
55
+ const num = Number(normalized);
56
+
57
+ if (!Number.isFinite(num)) {
58
+ throw new Error('Invalid amount');
59
+ }
60
+
61
+ const fixed = num.toFixed(exponent);
62
+ // Using Math.round avoids string replace for integers like 0 decimals
63
+ const minor = Math.round(num * Math.pow(10, exponent));
64
+
65
+ // fallback to parsed integer if rounding is not safe
66
+ return Number.isSafeInteger(minor)
67
+ ? minor
68
+ : parseInt(fixed.replace('.', ''), 10);
69
+ }
70
+
71
+ /**
72
+ * Return decimal exponent for currency.
73
+ *
74
+ * @param {string} currency - ISO 4217 currency code.
75
+ * @returns {number} Number of decimal places (default = 2).
76
+ */
77
+ function getCurrencyExponent(currency) {
78
+ const map = {
79
+ EUR: 2,
80
+ USD: 2,
81
+ GBP: 2,
82
+ CHF: 2,
83
+ JPY: 0,
84
+ KWD: 3
85
+ };
86
+
87
+ const code = typeof currency === 'string'
88
+ ? currency.trim().toUpperCase()
89
+ : '';
90
+
91
+ return map[code] ?? 2;
92
+ }
93
+
94
+ module.exports = {
95
+ toMajorAmount,
96
+ toMinorAmount
97
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "b2b-platform-utils",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "Shared utilities for Node.js microservices: errors map, local cache, logger, numbers, dates, filesystem, media optimization, paginator, slugger, crypto wrapper, sanitize HTML, sorting.",
5
5
  "type": "commonjs",
6
6
  "license": "KingSizer",