@ztimson/utils 0.26.24 → 0.26.26
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 +41 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +41 -0
- package/dist/index.mjs.map +1 -1
- package/dist/objects.d.ts +17 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -121,6 +121,45 @@ ${opts.message || this.desc}`;
|
|
|
121
121
|
`;
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
|
+
function applyDeltas(target, deltas) {
|
|
125
|
+
for (const delta of deltas) {
|
|
126
|
+
for (const [key, value] of Object.entries(delta)) {
|
|
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;
|
|
141
|
+
}
|
|
142
|
+
function calcDelta(old, target) {
|
|
143
|
+
const delta = {};
|
|
144
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(old || {}), ...Object.keys(target || {})]);
|
|
145
|
+
for (const key of keys) {
|
|
146
|
+
const val1 = old == null ? void 0 : old[key];
|
|
147
|
+
const val2 = target == null ? void 0 : target[key];
|
|
148
|
+
if (!(key in target)) {
|
|
149
|
+
delta[key] = val1;
|
|
150
|
+
} else if (!(key in old)) {
|
|
151
|
+
delta[key] = null;
|
|
152
|
+
} else if (Array.isArray(val1) || Array.isArray(val2)) {
|
|
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;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return delta;
|
|
162
|
+
}
|
|
124
163
|
function clean(obj, undefinedOnly = false) {
|
|
125
164
|
if (obj == null) throw new Error("Cannot clean a NULL value");
|
|
126
165
|
if (Array.isArray(obj)) {
|
|
@@ -2334,7 +2373,9 @@ ${opts.message || this.desc}`;
|
|
|
2334
2373
|
exports2.UnauthorizedError = UnauthorizedError;
|
|
2335
2374
|
exports2.addUnique = addUnique;
|
|
2336
2375
|
exports2.adjustedInterval = adjustedInterval;
|
|
2376
|
+
exports2.applyDeltas = applyDeltas;
|
|
2337
2377
|
exports2.arrayDiff = arrayDiff;
|
|
2378
|
+
exports2.calcDelta = calcDelta;
|
|
2338
2379
|
exports2.camelCase = camelCase;
|
|
2339
2380
|
exports2.caseInsensitiveSort = caseInsensitiveSort;
|
|
2340
2381
|
exports2.clean = clean;
|