b2b-platform-utils 1.1.4 → 1.1.5
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/objectWrapper.js +48 -0
- package/package.json +1 -1
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.5",
|
|
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",
|