@ptolemy2002/list-object-utils 1.0.0 → 1.1.0
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/index.js +26 -18
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -29,6 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
29
29
|
// src/index.js
|
|
30
30
|
var src_exports = {};
|
|
31
31
|
__export(src_exports, {
|
|
32
|
+
flattenKeys: () => flattenKeys,
|
|
32
33
|
isSet: () => isSet,
|
|
33
34
|
listDifference: () => listDifference,
|
|
34
35
|
listPush: () => listPush,
|
|
@@ -103,50 +104,57 @@ function isSet(list) {
|
|
|
103
104
|
}
|
|
104
105
|
return true;
|
|
105
106
|
}
|
|
106
|
-
function listDifference(a, b
|
|
107
|
-
if (!(0, import_is_callable.default)(keyConvert)) keyConvert = (x) => x;
|
|
108
|
-
if (!(0, import_is_callable.default)(valueConvert)) valueConvert = (x) => x;
|
|
107
|
+
function listDifference(a, b) {
|
|
109
108
|
const result = {};
|
|
110
109
|
a.forEach((element, i) => {
|
|
111
110
|
if (b[i] !== element) {
|
|
112
111
|
if (Array.isArray(element) && Array.isArray(b[i])) {
|
|
113
|
-
|
|
114
|
-
result[
|
|
115
|
-
if (result[k].length === 0) delete result[k];
|
|
112
|
+
result[i] = listDifference(element, b[i]);
|
|
113
|
+
if (result[i].length === 0) delete result[i];
|
|
116
114
|
} else if (typeof element === "object" && typeof b[i] === "object") {
|
|
117
|
-
|
|
118
|
-
result[
|
|
119
|
-
if (Object.keys(result[k]).length === 0) delete result[k];
|
|
115
|
+
result[i] = objectDifference(element, b[i]);
|
|
116
|
+
if (Object.keys(result[i]).length === 0) delete result[i];
|
|
120
117
|
} else {
|
|
121
|
-
result[
|
|
118
|
+
result[i] = b[i];
|
|
122
119
|
}
|
|
123
120
|
}
|
|
124
121
|
});
|
|
125
122
|
if (b.length > a.length) {
|
|
126
123
|
for (let i = a.length; i < b.length; i++) {
|
|
127
|
-
result[
|
|
124
|
+
result[i] = b[i];
|
|
128
125
|
}
|
|
129
126
|
}
|
|
130
127
|
return result;
|
|
131
128
|
}
|
|
132
|
-
function objectDifference(a, b
|
|
133
|
-
if (!(0, import_is_callable.default)(keyConvert)) keyConvert = (x) => x;
|
|
134
|
-
if (!(0, import_is_callable.default)(valueConvert)) valueConvert = (x) => x;
|
|
129
|
+
function objectDifference(a, b) {
|
|
135
130
|
const result = {};
|
|
136
131
|
for (const key in a) {
|
|
137
132
|
if (b[key] !== a[key]) {
|
|
138
133
|
if (Array.isArray(a[key]) && Array.isArray(b[key])) {
|
|
139
|
-
result[
|
|
134
|
+
result[key] = listDifference(a[key], b[key]);
|
|
140
135
|
} else if (typeof a[key] === "object" && typeof b[key] === "object") {
|
|
141
|
-
result[
|
|
136
|
+
result[key] = objectDifference(a[key], b[key]);
|
|
142
137
|
} else {
|
|
143
|
-
result[
|
|
138
|
+
result[key] = b[key];
|
|
144
139
|
}
|
|
145
140
|
}
|
|
146
141
|
}
|
|
147
142
|
for (const key in b) {
|
|
148
143
|
if (!a.hasOwnProperty(key)) {
|
|
149
|
-
result[
|
|
144
|
+
result[key] = b[key];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
function flattenKeys(obj, prefix = "") {
|
|
150
|
+
const result = {};
|
|
151
|
+
for (const key in obj) {
|
|
152
|
+
if (typeof obj[key] === "object" && !Array.isArray(obj[key])) {
|
|
153
|
+
const flat = flattenKeys(obj[key], prefix + key + ".");
|
|
154
|
+
Object.assign(result, flat);
|
|
155
|
+
if (Object.keys(flat).length === 0) result[prefix + key] = obj[key];
|
|
156
|
+
} else {
|
|
157
|
+
result[prefix + key] = obj[key];
|
|
150
158
|
}
|
|
151
159
|
}
|
|
152
160
|
return result;
|