@ztimson/utils 0.27.0 → 0.27.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/dist/index.cjs +23 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +23 -34
- package/dist/index.mjs.map +1 -1
- package/dist/objects.d.ts +5 -5
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -121,44 +121,33 @@ ${opts.message || this.desc}`;
|
|
|
121
121
|
`;
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
|
-
function
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
target[key] = {};
|
|
134
|
-
applyDeltas(target[key], [value]);
|
|
135
|
-
} else {
|
|
136
|
-
target[key] = value;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
124
|
+
function applyDelta(base, deltas) {
|
|
125
|
+
if (deltas === null) return null;
|
|
126
|
+
if (typeof base !== "object" || base === null) return deltas === void 0 ? base : deltas;
|
|
127
|
+
const result = Array.isArray(base) ? [...base] : { ...base };
|
|
128
|
+
for (const key in deltas) {
|
|
129
|
+
const val = deltas[key];
|
|
130
|
+
if (val === void 0) delete result[key];
|
|
131
|
+
else if (typeof val === "object" && val !== null && !Array.isArray(val)) result[key] = applyDelta(result[key], val);
|
|
132
|
+
else result[key] = val;
|
|
139
133
|
}
|
|
140
|
-
return
|
|
134
|
+
return result;
|
|
141
135
|
}
|
|
142
|
-
function calcDelta(old,
|
|
136
|
+
function calcDelta(old, updated) {
|
|
137
|
+
if (updated == null) return null;
|
|
143
138
|
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;
|
|
139
|
+
const isObj = (v) => v && typeof v === "object" && !Array.isArray(v);
|
|
140
|
+
for (const key of /* @__PURE__ */ new Set([...old ? Object.keys(old) : [], ...updated ? Object.keys(updated) : []])) {
|
|
141
|
+
const oldVal = old == null ? void 0 : old[key];
|
|
142
|
+
const newVal = updated == null ? void 0 : updated[key];
|
|
143
|
+
if (isObj(oldVal) && isObj(newVal)) {
|
|
144
|
+
const nested = calcDelta(oldVal, newVal);
|
|
145
|
+
if (nested !== null && Object.keys(nested).length > 0) delta[key] = nested;
|
|
146
|
+
} else if (JSON.stringify(oldVal) !== JSON.stringify(newVal)) {
|
|
147
|
+
delta[key] = newVal;
|
|
159
148
|
}
|
|
160
149
|
}
|
|
161
|
-
return delta;
|
|
150
|
+
return Object.keys(delta).length === 0 ? {} : delta;
|
|
162
151
|
}
|
|
163
152
|
function clean(obj, undefinedOnly = false) {
|
|
164
153
|
if (obj == null) throw new Error("Cannot clean a NULL value");
|
|
@@ -2378,7 +2367,7 @@ ${opts.message || this.desc}`;
|
|
|
2378
2367
|
exports2.UnauthorizedError = UnauthorizedError;
|
|
2379
2368
|
exports2.addUnique = addUnique;
|
|
2380
2369
|
exports2.adjustedInterval = adjustedInterval;
|
|
2381
|
-
exports2.
|
|
2370
|
+
exports2.applyDelta = applyDelta;
|
|
2382
2371
|
exports2.arrayDiff = arrayDiff;
|
|
2383
2372
|
exports2.calcDelta = calcDelta;
|
|
2384
2373
|
exports2.camelCase = camelCase;
|