@ztimson/utils 0.27.0 → 0.27.2
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/dist/index.cjs +15 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +15 -34
- package/dist/index.mjs.map +1 -1
- package/dist/objects.d.ts +7 -7
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -121,44 +121,25 @@ ${opts.message || this.desc}`;
|
|
|
121
121
|
`;
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
|
-
function applyDeltas(
|
|
125
|
-
for (
|
|
126
|
-
|
|
127
|
-
if (value === null) {
|
|
128
|
-
delete target[key];
|
|
129
|
-
} else if (Array.isArray(value)) {
|
|
130
|
-
target[key] = [...value];
|
|
131
|
-
} else if (typeof value === "object") {
|
|
132
|
-
if (typeof target[key] !== "object" || Array.isArray(target[key]) || !target[key])
|
|
133
|
-
target[key] = {};
|
|
134
|
-
applyDeltas(target[key], [value]);
|
|
135
|
-
} else {
|
|
136
|
-
target[key] = value;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return target;
|
|
124
|
+
function applyDeltas(base, ...deltas) {
|
|
125
|
+
for (let d of deltas) base = applyDeltas(base, d);
|
|
126
|
+
return base;
|
|
141
127
|
}
|
|
142
|
-
function calcDelta(old,
|
|
128
|
+
function calcDelta(old, updated) {
|
|
129
|
+
if (updated == null) return null;
|
|
143
130
|
const delta = {};
|
|
144
|
-
const
|
|
145
|
-
for (const key of keys) {
|
|
146
|
-
const
|
|
147
|
-
const
|
|
148
|
-
if (
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (JSON.stringify(val1) !== JSON.stringify(val2)) delta[key] = val1;
|
|
154
|
-
} else if (typeof val1 === "object" && typeof val2 === "object" && val1 && val2) {
|
|
155
|
-
const nested = calcDelta(val1, val2);
|
|
156
|
-
if (Object.keys(nested).length) delta[key] = nested;
|
|
157
|
-
} else if (val1 !== val2) {
|
|
158
|
-
delta[key] = val1;
|
|
131
|
+
const isObj = (v) => v && typeof v === "object" && !Array.isArray(v);
|
|
132
|
+
for (const key of /* @__PURE__ */ new Set([...old ? Object.keys(old) : [], ...updated ? Object.keys(updated) : []])) {
|
|
133
|
+
const oldVal = old == null ? void 0 : old[key];
|
|
134
|
+
const newVal = updated == null ? void 0 : updated[key];
|
|
135
|
+
if (isObj(oldVal) && isObj(newVal)) {
|
|
136
|
+
const nested = calcDelta(oldVal, newVal);
|
|
137
|
+
if (nested !== null && Object.keys(nested).length > 0) delta[key] = nested;
|
|
138
|
+
} else if (JSON.stringify(oldVal) !== JSON.stringify(newVal)) {
|
|
139
|
+
delta[key] = newVal;
|
|
159
140
|
}
|
|
160
141
|
}
|
|
161
|
-
return delta;
|
|
142
|
+
return Object.keys(delta).length === 0 ? {} : delta;
|
|
162
143
|
}
|
|
163
144
|
function clean(obj, undefinedOnly = false) {
|
|
164
145
|
if (obj == null) throw new Error("Cannot clean a NULL value");
|