b2b-platform-utils 1.1.4 → 1.1.6
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/localCache.js +29 -0
- package/objectWrapper.js +48 -0
- package/package.json +1 -1
package/localCache.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* Process-scoped in-memory key-value store for configuration.
|
|
7
7
|
* - Backward compatible with existing API surface.
|
|
8
8
|
* - Adds currency helpers with explicit opt-in ENV fallback.
|
|
9
|
+
* - Provides default GEO and Locale helpers for runtime consumers.
|
|
9
10
|
*
|
|
10
11
|
* Design goals:
|
|
11
12
|
* 1) No side effects on import (ENV is read lazily, only if enabled).
|
|
@@ -27,6 +28,9 @@ let bufferChunkSize = 512000;
|
|
|
27
28
|
/** @type {string} */
|
|
28
29
|
const GEO_DEFAULT = 'WW';
|
|
29
30
|
|
|
31
|
+
/** @type {string} */
|
|
32
|
+
const LOCALE_DEFAULT = 'en-US';
|
|
33
|
+
|
|
30
34
|
/** @type {boolean} */
|
|
31
35
|
let envBaseCurrencyFallbackEnabled = false; // opt-in per process
|
|
32
36
|
|
|
@@ -46,6 +50,7 @@ function ensureCommonBag() {
|
|
|
46
50
|
/**
|
|
47
51
|
* Normalize to a 3-letter uppercase currency code.
|
|
48
52
|
* Returns null if invalid.
|
|
53
|
+
*
|
|
49
54
|
* @param {unknown} value
|
|
50
55
|
* @returns {string|null}
|
|
51
56
|
*/
|
|
@@ -59,6 +64,7 @@ function normalizeCurrencyCode(value) {
|
|
|
59
64
|
|
|
60
65
|
/**
|
|
61
66
|
* Set a top-level key in the cache.
|
|
67
|
+
*
|
|
62
68
|
* @param {string} key
|
|
63
69
|
* @param {any} value
|
|
64
70
|
*/
|
|
@@ -68,6 +74,7 @@ function setValue(key, value) {
|
|
|
68
74
|
|
|
69
75
|
/**
|
|
70
76
|
* Get a top-level value from the cache.
|
|
77
|
+
*
|
|
71
78
|
* @param {string} key
|
|
72
79
|
* @returns {any|null}
|
|
73
80
|
*/
|
|
@@ -79,6 +86,7 @@ function getValue(key) {
|
|
|
79
86
|
|
|
80
87
|
/**
|
|
81
88
|
* Shallow-merge a config object into the cache.
|
|
89
|
+
*
|
|
82
90
|
* @param {Record<string, any>} data
|
|
83
91
|
*/
|
|
84
92
|
function applyConfig(data) {
|
|
@@ -91,6 +99,7 @@ function applyConfig(data) {
|
|
|
91
99
|
|
|
92
100
|
/**
|
|
93
101
|
* Return a direct reference to the whole cache (mutable).
|
|
102
|
+
*
|
|
94
103
|
* @returns {Record<string, any>}
|
|
95
104
|
*/
|
|
96
105
|
function getData() {
|
|
@@ -101,6 +110,7 @@ function getData() {
|
|
|
101
110
|
|
|
102
111
|
/**
|
|
103
112
|
* Return current microservice key.
|
|
113
|
+
*
|
|
104
114
|
* @returns {string}
|
|
105
115
|
*/
|
|
106
116
|
function getService() {
|
|
@@ -109,6 +119,7 @@ function getService() {
|
|
|
109
119
|
|
|
110
120
|
/**
|
|
111
121
|
* Override microservice key at runtime.
|
|
122
|
+
*
|
|
112
123
|
* @param {string} key
|
|
113
124
|
*/
|
|
114
125
|
function setService(key) {
|
|
@@ -119,6 +130,7 @@ function setService(key) {
|
|
|
119
130
|
|
|
120
131
|
/**
|
|
121
132
|
* Get configured buffer chunk size.
|
|
133
|
+
*
|
|
122
134
|
* @returns {number}
|
|
123
135
|
*/
|
|
124
136
|
function getBufferChunkSize() {
|
|
@@ -128,6 +140,7 @@ function getBufferChunkSize() {
|
|
|
128
140
|
/**
|
|
129
141
|
* Initialize environment/customer from process params.
|
|
130
142
|
* Kept for compatibility with existing bootstrap flows.
|
|
143
|
+
*
|
|
131
144
|
* @param {string[]} params
|
|
132
145
|
*/
|
|
133
146
|
function parseInitConfig(params) {
|
|
@@ -139,6 +152,7 @@ function parseInitConfig(params) {
|
|
|
139
152
|
|
|
140
153
|
/**
|
|
141
154
|
* Return the "common" bag snapshot ({} if missing).
|
|
155
|
+
*
|
|
142
156
|
* @returns {Record<string, any>}
|
|
143
157
|
*/
|
|
144
158
|
function getCommonValue() {
|
|
@@ -151,16 +165,28 @@ function getCommonValue() {
|
|
|
151
165
|
|
|
152
166
|
/**
|
|
153
167
|
* Read GEODefault from "common" or return fallback.
|
|
168
|
+
*
|
|
154
169
|
* @returns {string}
|
|
155
170
|
*/
|
|
156
171
|
function getDefaultGEO() {
|
|
157
172
|
return getValue('common')?.GEODefault || GEO_DEFAULT;
|
|
158
173
|
}
|
|
159
174
|
|
|
175
|
+
/**
|
|
176
|
+
* Read LocaleDefault from "common" or return fallback.
|
|
177
|
+
* Locale codes must follow the pattern: en-US, de-DE, pt-BR etc.
|
|
178
|
+
*
|
|
179
|
+
* @returns {string}
|
|
180
|
+
*/
|
|
181
|
+
function getDefaultLocale() {
|
|
182
|
+
return getValue('common')?.LocaleDefault || LOCALE_DEFAULT;
|
|
183
|
+
}
|
|
184
|
+
|
|
160
185
|
/**
|
|
161
186
|
* Enable or disable ENV fallback for BaseCurrency.
|
|
162
187
|
* When enabled and "common.BaseCurrency" is not set,
|
|
163
188
|
* getBaseCurrency() will use process.env.BASE_CURRENCY.
|
|
189
|
+
*
|
|
164
190
|
* @param {boolean} enabled
|
|
165
191
|
*/
|
|
166
192
|
function enableEnvBaseCurrencyFallback(enabled = true) {
|
|
@@ -170,6 +196,7 @@ function enableEnvBaseCurrencyFallback(enabled = true) {
|
|
|
170
196
|
/**
|
|
171
197
|
* Get BaseCurrency from "common", otherwise (optionally) from ENV.
|
|
172
198
|
* Returns null if nothing is set or valid.
|
|
199
|
+
*
|
|
173
200
|
* @returns {string|null}
|
|
174
201
|
*/
|
|
175
202
|
function getBaseCurrency() {
|
|
@@ -190,6 +217,7 @@ function getBaseCurrency() {
|
|
|
190
217
|
|
|
191
218
|
/**
|
|
192
219
|
* Explicitly set BaseCurrency into the "common" bag.
|
|
220
|
+
*
|
|
193
221
|
* @param {string} code - 3-letter code, e.g., "USD", "EUR", "GBP".
|
|
194
222
|
* @throws {Error} If code is invalid.
|
|
195
223
|
*/
|
|
@@ -222,6 +250,7 @@ module.exports = {
|
|
|
222
250
|
|
|
223
251
|
// Domain helpers
|
|
224
252
|
getDefaultGEO,
|
|
253
|
+
getDefaultLocale,
|
|
225
254
|
|
|
226
255
|
// Currency helpers
|
|
227
256
|
getBaseCurrency,
|
package/objectWrapper.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Merges multiple arrays of objects by `date_time` key.
|
|
5
|
+
* If objects share the same `date_time`, their properties are combined into one.
|
|
6
|
+
*
|
|
7
|
+
* @param {Array<Array<Object>>} arrayOfArrays - Array of arrays containing objects with a `date_time` field
|
|
8
|
+
* @returns {Array<Object>} Merged array of objects
|
|
9
|
+
*/
|
|
10
|
+
function mergeArraysByDateTime(arrayOfArrays) {
|
|
11
|
+
const resultMap = new Map();
|
|
12
|
+
|
|
13
|
+
for (const array of arrayOfArrays) {
|
|
14
|
+
for (const item of array) {
|
|
15
|
+
const { date_time } = item;
|
|
16
|
+
if (!resultMap.has(date_time)) {
|
|
17
|
+
resultMap.set(date_time, { date_time });
|
|
18
|
+
}
|
|
19
|
+
Object.assign(resultMap.get(date_time), item);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return Array.from(resultMap.values());
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Groups objects by `date_time` and merges their non-date properties into an `items` array.
|
|
28
|
+
*
|
|
29
|
+
* @param {Array<Object>} arrayOfObjects - Array of objects containing a `date_time` field
|
|
30
|
+
* @returns {Array<Object>} Array of grouped objects: { date_time, items: [...] }
|
|
31
|
+
*/
|
|
32
|
+
function mergeItemsByDateTime(arrayOfObjects) {
|
|
33
|
+
const grouped = arrayOfObjects.reduce((acc, obj) => {
|
|
34
|
+
const { date_time, ...rest } = obj;
|
|
35
|
+
if (!acc[date_time]) {
|
|
36
|
+
acc[date_time] = { date_time, items: [] };
|
|
37
|
+
}
|
|
38
|
+
acc[date_time].items.push(rest);
|
|
39
|
+
return acc;
|
|
40
|
+
}, {});
|
|
41
|
+
|
|
42
|
+
return Object.values(grouped);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = {
|
|
46
|
+
mergeArraysByDateTime,
|
|
47
|
+
mergeItemsByDateTime,
|
|
48
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "b2b-platform-utils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6",
|
|
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",
|