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