b2b-platform-utils 1.1.8 → 1.1.10

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.
@@ -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/errorsMap.js CHANGED
@@ -89,24 +89,23 @@ const api = {
89
89
  list: () => [...STATIC_ERRORS]
90
90
  };
91
91
 
92
- // --- Dynamic shortcuts: errorsMap.<errorKey>() returns JSON string ---
93
- // This keeps old style calls working across all services without hand-written wrappers.
92
+ // --- Dynamic shortcuts: errorsMap.<errorKey>() returns OBJECT, not string ---
94
93
  module.exports = new Proxy(api, {
95
94
  get(target, prop, receiver) {
96
95
  // If method exists (get, getString, etc.) → return it
97
96
  if (prop in target) return Reflect.get(target, prop, receiver);
98
97
 
99
- // If property is a known errorKey → return a function () => getString(errorKey)
98
+ // If property is a known errorKey → return a function () => get(errorKey)
100
99
  if (typeof prop === 'string' && STATIC_BY_KEY[prop]) {
101
- return () => target.getString(prop);
100
+ return () => target.get(prop); // ✅ return object, not string
102
101
  }
103
102
 
104
- // Otherwise, undefined (so optional chaining ?.() is safe)
103
+ // Otherwise undefined
105
104
  return undefined;
106
105
  },
107
106
 
108
- // Optional: make `prop in errorsMap` truthy for known keys
109
107
  has(target, prop) {
110
108
  return prop in target || (typeof prop === 'string' && !!STATIC_BY_KEY[prop]);
111
109
  }
112
110
  });
111
+
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.10",
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",