@coderich/util 2.1.0 → 2.1.1
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/package.json +1 -1
- package/src/index.js +9 -30
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -76,37 +76,16 @@ exports.flatten = (mixed, options = {}) => {
|
|
|
76
76
|
exports.unflatten = (data, options = {}) => {
|
|
77
77
|
const typeFn = options.safe ? exports.isPlainObject : exports.isPlainObjectOrArray;
|
|
78
78
|
|
|
79
|
-
const compactArrays = (prev, key, value) => {
|
|
80
|
-
const parts = key.split('.');
|
|
81
|
-
const last = parts.at(-1);
|
|
82
|
-
|
|
83
|
-
if (/^\d+$/.test(last)) {
|
|
84
|
-
// This is a numeric segment — treat parent as a dense array
|
|
85
|
-
const parentPath = parts.slice(0, -1).join('.');
|
|
86
|
-
|
|
87
|
-
// Get existing parent, or create it as an array
|
|
88
|
-
let parent = parentPath ? exports.get(prev, parentPath) : prev;
|
|
89
|
-
if (!Array.isArray(parent)) {
|
|
90
|
-
parent = [];
|
|
91
|
-
if (parentPath) {
|
|
92
|
-
exports.set(prev, parentPath, parent);
|
|
93
|
-
} else {
|
|
94
|
-
// Top-level array
|
|
95
|
-
return [value]; // shortcut if root is just an array
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
parent.push(value);
|
|
100
|
-
return prev;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return exports.set(prev, key, value);
|
|
104
|
-
};
|
|
105
|
-
|
|
106
79
|
return exports.map(data, (el) => {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
80
|
+
const cache = {};
|
|
81
|
+
|
|
82
|
+
return typeFn(el) ? Object.entries(exports.flatten(el, options)).reduce((prev, [key, value]) => {
|
|
83
|
+
const $key = options.compactArrays ? key.replace(/\d+/g, (digit, offset, str) => {
|
|
84
|
+
const prefix = str.slice(0, offset);
|
|
85
|
+
cache[prefix] ??= 0;
|
|
86
|
+
return cache[prefix]++;
|
|
87
|
+
}) : key;
|
|
88
|
+
return exports.set(prev, $key, value);
|
|
110
89
|
}, {}) : el;
|
|
111
90
|
});
|
|
112
91
|
};
|