@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.mjs CHANGED
@@ -117,44 +117,33 @@ ${opts.message || this.desc}`;
117
117
  `;
118
118
  }
119
119
  }
120
- function applyDeltas(target, deltas) {
121
- for (const delta of deltas) {
122
- for (const [key, value] of Object.entries(delta)) {
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
- }
120
+ function applyDelta(base, deltas) {
121
+ if (deltas === null) return null;
122
+ if (typeof base !== "object" || base === null) return deltas === void 0 ? base : deltas;
123
+ const result = Array.isArray(base) ? [...base] : { ...base };
124
+ for (const key in deltas) {
125
+ const val = deltas[key];
126
+ if (val === void 0) delete result[key];
127
+ else if (typeof val === "object" && val !== null && !Array.isArray(val)) result[key] = applyDelta(result[key], val);
128
+ else result[key] = val;
135
129
  }
136
- return target;
130
+ return result;
137
131
  }
138
- function calcDelta(old, target) {
132
+ function calcDelta(old, updated) {
133
+ if (updated == null) return null;
139
134
  const delta = {};
140
- const keys = /* @__PURE__ */ new Set([...Object.keys(old || {}), ...Object.keys(target || {})]);
141
- for (const key of keys) {
142
- const val1 = old == null ? void 0 : old[key];
143
- const val2 = target == null ? void 0 : target[key];
144
- if (!(key in target)) {
145
- delta[key] = val1;
146
- } else if (!(key in old)) {
147
- delta[key] = null;
148
- } else if (Array.isArray(val1) || Array.isArray(val2)) {
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;
135
+ const isObj = (v) => v && typeof v === "object" && !Array.isArray(v);
136
+ for (const key of /* @__PURE__ */ new Set([...old ? Object.keys(old) : [], ...updated ? Object.keys(updated) : []])) {
137
+ const oldVal = old == null ? void 0 : old[key];
138
+ const newVal = updated == null ? void 0 : updated[key];
139
+ if (isObj(oldVal) && isObj(newVal)) {
140
+ const nested = calcDelta(oldVal, newVal);
141
+ if (nested !== null && Object.keys(nested).length > 0) delta[key] = nested;
142
+ } else if (JSON.stringify(oldVal) !== JSON.stringify(newVal)) {
143
+ delta[key] = newVal;
155
144
  }
156
145
  }
157
- return delta;
146
+ return Object.keys(delta).length === 0 ? {} : delta;
158
147
  }
159
148
  function clean(obj, undefinedOnly = false) {
160
149
  if (obj == null) throw new Error("Cannot clean a NULL value");
@@ -2375,7 +2364,7 @@ export {
2375
2364
  UnauthorizedError,
2376
2365
  addUnique,
2377
2366
  adjustedInterval,
2378
- applyDeltas,
2367
+ applyDelta,
2379
2368
  arrayDiff,
2380
2369
  calcDelta,
2381
2370
  camelCase,