@domql/utils 2.31.25 → 2.31.27
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/cjs/object.js +43 -22
- package/dist/esm/object.js +43 -22
- package/object.js +53 -17
- package/package.json +2 -2
package/dist/cjs/object.js
CHANGED
|
@@ -23,15 +23,16 @@ __export(object_exports, {
|
|
|
23
23
|
createObjectWithoutPrototype: () => createObjectWithoutPrototype,
|
|
24
24
|
deepClone: () => deepClone,
|
|
25
25
|
deepContains: () => deepContains,
|
|
26
|
-
|
|
26
|
+
deepDestringifyFunctions: () => deepDestringifyFunctions,
|
|
27
27
|
deepDiff: () => deepDiff,
|
|
28
28
|
deepMerge: () => deepMerge,
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
deepStringifyFunctions: () => deepStringifyFunctions,
|
|
30
|
+
deepStringifyFunctionsWithMaxDepth: () => deepStringifyFunctionsWithMaxDepth,
|
|
31
31
|
detachFunctionsFromObject: () => detachFunctionsFromObject,
|
|
32
32
|
detectInfiniteLoop: () => detectInfiniteLoop,
|
|
33
33
|
diff: () => diff,
|
|
34
34
|
diffObjects: () => diffObjects,
|
|
35
|
+
evalStringToObject: () => evalStringToObject,
|
|
35
36
|
excludeKeysFromObject: () => excludeKeysFromObject,
|
|
36
37
|
exec: () => exec,
|
|
37
38
|
flattenRecursive: () => flattenRecursive,
|
|
@@ -53,8 +54,7 @@ __export(object_exports, {
|
|
|
53
54
|
overwriteShallow: () => overwriteShallow,
|
|
54
55
|
removeFromObject: () => removeFromObject,
|
|
55
56
|
removeNestedKeyByPath: () => removeNestedKeyByPath,
|
|
56
|
-
setInObjectByPath: () => setInObjectByPath
|
|
57
|
-
stringToObject: () => stringToObject
|
|
57
|
+
setInObjectByPath: () => setInObjectByPath
|
|
58
58
|
});
|
|
59
59
|
module.exports = __toCommonJS(object_exports);
|
|
60
60
|
var import_globals = require("./globals.js");
|
|
@@ -182,7 +182,7 @@ const deepClone = (obj, options = {}) => {
|
|
|
182
182
|
}
|
|
183
183
|
return clone2;
|
|
184
184
|
};
|
|
185
|
-
const
|
|
185
|
+
const deepStringifyFunctions = (obj, stringified = {}) => {
|
|
186
186
|
var _a, _b;
|
|
187
187
|
if (!obj) return;
|
|
188
188
|
if (obj.node || obj.__ref || obj.parent || obj.__element || obj.parse) {
|
|
@@ -199,13 +199,13 @@ const deepStringify = (obj, stringified = {}) => {
|
|
|
199
199
|
stringified[prop] = objProp.toString();
|
|
200
200
|
} else if ((0, import_types.isObject)(objProp)) {
|
|
201
201
|
stringified[prop] = {};
|
|
202
|
-
|
|
202
|
+
deepStringifyFunctions(objProp, stringified[prop]);
|
|
203
203
|
} else if ((0, import_types.isArray)(objProp)) {
|
|
204
204
|
stringified[prop] = [];
|
|
205
205
|
objProp.forEach((v, i) => {
|
|
206
206
|
if ((0, import_types.isObject)(v)) {
|
|
207
207
|
stringified[prop][i] = {};
|
|
208
|
-
|
|
208
|
+
deepStringifyFunctions(v, stringified[prop][i]);
|
|
209
209
|
} else if ((0, import_types.isFunction)(v)) {
|
|
210
210
|
stringified[prop][i] = v.toString();
|
|
211
211
|
} else {
|
|
@@ -219,7 +219,7 @@ const deepStringify = (obj, stringified = {}) => {
|
|
|
219
219
|
return stringified;
|
|
220
220
|
};
|
|
221
221
|
const MAX_DEPTH = 100;
|
|
222
|
-
const
|
|
222
|
+
const deepStringifyFunctionsWithMaxDepth = (obj, stringified = {}, depth = 0, path = "") => {
|
|
223
223
|
if (depth > MAX_DEPTH) {
|
|
224
224
|
console.warn(
|
|
225
225
|
`Maximum depth exceeded at path: ${path}. Possible circular reference.`
|
|
@@ -233,7 +233,7 @@ const deepStringifyWithMaxDepth = (obj, stringified = {}, depth = 0, path = "")
|
|
|
233
233
|
stringified[prop] = objProp.toString();
|
|
234
234
|
} else if ((0, import_types.isObject)(objProp)) {
|
|
235
235
|
stringified[prop] = {};
|
|
236
|
-
|
|
236
|
+
deepStringifyFunctionsWithMaxDepth(
|
|
237
237
|
objProp,
|
|
238
238
|
stringified[prop],
|
|
239
239
|
depth + 1,
|
|
@@ -245,7 +245,7 @@ const deepStringifyWithMaxDepth = (obj, stringified = {}, depth = 0, path = "")
|
|
|
245
245
|
const itemPath = `${currentPath}[${i}]`;
|
|
246
246
|
if ((0, import_types.isObject)(v)) {
|
|
247
247
|
stringified[prop][i] = {};
|
|
248
|
-
|
|
248
|
+
deepStringifyFunctionsWithMaxDepth(
|
|
249
249
|
v,
|
|
250
250
|
stringified[prop][i],
|
|
251
251
|
depth + 1,
|
|
@@ -342,7 +342,7 @@ const detachFunctionsFromObject = (obj, detached = {}) => {
|
|
|
342
342
|
if ((0, import_types.isFunction)(objProp)) continue;
|
|
343
343
|
else if ((0, import_types.isObject)(objProp)) {
|
|
344
344
|
detached[prop] = {};
|
|
345
|
-
|
|
345
|
+
deepStringifyFunctions(objProp, detached[prop]);
|
|
346
346
|
} else if ((0, import_types.isArray)(objProp)) {
|
|
347
347
|
detached[prop] = [];
|
|
348
348
|
objProp.forEach((v, i) => {
|
|
@@ -381,7 +381,7 @@ const hasFunction = (str) => {
|
|
|
381
381
|
const isJSONLike = /^["[{]/.test(trimmed) && !trimmed.includes("=>");
|
|
382
382
|
return (isFunction2 || isClass) && !isObjectLiteral && !isArrayLiteral && !isJSONLike;
|
|
383
383
|
};
|
|
384
|
-
const
|
|
384
|
+
const deepDestringifyFunctions = (obj, destringified = {}) => {
|
|
385
385
|
for (const prop in obj) {
|
|
386
386
|
const hasOwnProperty2 = Object.prototype.hasOwnProperty.call(obj, prop);
|
|
387
387
|
if (!hasOwnProperty2) continue;
|
|
@@ -412,33 +412,49 @@ const deepDestringify = (obj, destringified = {}) => {
|
|
|
412
412
|
destringified[prop].push(arrProp);
|
|
413
413
|
}
|
|
414
414
|
} else if ((0, import_types.isObject)(arrProp)) {
|
|
415
|
-
destringified[prop].push(
|
|
415
|
+
destringified[prop].push(deepDestringifyFunctions(arrProp));
|
|
416
416
|
} else {
|
|
417
417
|
destringified[prop].push(arrProp);
|
|
418
418
|
}
|
|
419
419
|
});
|
|
420
420
|
} else if ((0, import_types.isObject)(objProp)) {
|
|
421
|
-
destringified[prop] =
|
|
421
|
+
destringified[prop] = deepDestringifyFunctions(
|
|
422
|
+
objProp,
|
|
423
|
+
destringified[prop]
|
|
424
|
+
);
|
|
422
425
|
} else {
|
|
423
426
|
destringified[prop] = objProp;
|
|
424
427
|
}
|
|
425
428
|
}
|
|
426
429
|
return destringified;
|
|
427
430
|
};
|
|
428
|
-
const
|
|
431
|
+
const evalStringToObject = (str, opts = { verbose: true }) => {
|
|
429
432
|
try {
|
|
430
|
-
return str ? import_globals.window.eval("(" + str + ")") : {};
|
|
433
|
+
return str ? (opts.window || import_globals.window).eval("(" + str + ")") : {};
|
|
431
434
|
} catch (e) {
|
|
432
435
|
if (opts.verbose) console.warn(e);
|
|
433
|
-
if (opts.
|
|
436
|
+
if (opts.onError) return opts.onError(e);
|
|
434
437
|
}
|
|
435
438
|
};
|
|
436
439
|
const diffObjects = (original, objToDiff, cache, opts) => {
|
|
437
440
|
let hasDiff = false;
|
|
438
|
-
|
|
441
|
+
const originalKeys = Object.keys(original);
|
|
442
|
+
const diffKeys = Object.keys(objToDiff);
|
|
443
|
+
const allKeys = [.../* @__PURE__ */ new Set([...originalKeys, ...diffKeys])];
|
|
444
|
+
const originalKeyOrder = originalKeys.join(",");
|
|
445
|
+
const diffKeyOrder = diffKeys.filter((k) => originalKeys.includes(k)).join(",");
|
|
446
|
+
if (originalKeyOrder !== diffKeyOrder) {
|
|
447
|
+
hasDiff = true;
|
|
448
|
+
}
|
|
449
|
+
for (const key of allKeys) {
|
|
439
450
|
if (key === "ref") continue;
|
|
440
451
|
const originalProp = original[key];
|
|
441
452
|
const objToDiffProp = objToDiff[key];
|
|
453
|
+
if (!(key in objToDiff)) {
|
|
454
|
+
cache[key] = void 0;
|
|
455
|
+
hasDiff = true;
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
442
458
|
if ((0, import_types.isObject)(originalProp) && (0, import_types.isObject)(objToDiffProp)) {
|
|
443
459
|
const nestedDiff = diff(originalProp, objToDiffProp, {}, opts);
|
|
444
460
|
if (nestedDiff && Object.keys(nestedDiff).length > 0) {
|
|
@@ -463,6 +479,11 @@ const diffArrays = (original, objToDiff, cache, opts) => {
|
|
|
463
479
|
return objToDiff;
|
|
464
480
|
}
|
|
465
481
|
let hasDiff = false;
|
|
482
|
+
const originalStringified = original.map((item) => JSON.stringify(item));
|
|
483
|
+
const diffStringified = objToDiff.map((item) => JSON.stringify(item));
|
|
484
|
+
if (originalStringified.join(",") !== diffStringified.join(",")) {
|
|
485
|
+
hasDiff = true;
|
|
486
|
+
}
|
|
466
487
|
for (let i = 0; i < original.length; i++) {
|
|
467
488
|
const diffObj = diff(original[i], objToDiff[i], {}, opts);
|
|
468
489
|
if (diffObj && ((0, import_types.isObject)(diffObj) ? Object.keys(diffObj).length > 0 : true)) {
|
|
@@ -470,15 +491,15 @@ const diffArrays = (original, objToDiff, cache, opts) => {
|
|
|
470
491
|
hasDiff = true;
|
|
471
492
|
}
|
|
472
493
|
}
|
|
473
|
-
return hasDiff ?
|
|
494
|
+
return hasDiff ? objToDiff : void 0;
|
|
474
495
|
};
|
|
475
496
|
const diff = (original, objToDiff, cache = {}, opts = {}) => {
|
|
476
497
|
if (opts.cloneInstances) {
|
|
477
498
|
original = deepClone(original);
|
|
478
499
|
objToDiff = deepClone(objToDiff);
|
|
479
500
|
}
|
|
480
|
-
original =
|
|
481
|
-
objToDiff =
|
|
501
|
+
original = deepStringifyFunctions(original);
|
|
502
|
+
objToDiff = deepStringifyFunctions(objToDiff);
|
|
482
503
|
if ((0, import_types.isArray)(original) && (0, import_types.isArray)(objToDiff)) {
|
|
483
504
|
const result = diffArrays(original, objToDiff, [], opts);
|
|
484
505
|
return result === void 0 ? {} : result;
|
package/dist/esm/object.js
CHANGED
|
@@ -151,7 +151,7 @@ const deepClone = (obj, options = {}) => {
|
|
|
151
151
|
}
|
|
152
152
|
return clone2;
|
|
153
153
|
};
|
|
154
|
-
const
|
|
154
|
+
const deepStringifyFunctions = (obj, stringified = {}) => {
|
|
155
155
|
var _a, _b;
|
|
156
156
|
if (!obj) return;
|
|
157
157
|
if (obj.node || obj.__ref || obj.parent || obj.__element || obj.parse) {
|
|
@@ -168,13 +168,13 @@ const deepStringify = (obj, stringified = {}) => {
|
|
|
168
168
|
stringified[prop] = objProp.toString();
|
|
169
169
|
} else if (isObject(objProp)) {
|
|
170
170
|
stringified[prop] = {};
|
|
171
|
-
|
|
171
|
+
deepStringifyFunctions(objProp, stringified[prop]);
|
|
172
172
|
} else if (isArray(objProp)) {
|
|
173
173
|
stringified[prop] = [];
|
|
174
174
|
objProp.forEach((v, i) => {
|
|
175
175
|
if (isObject(v)) {
|
|
176
176
|
stringified[prop][i] = {};
|
|
177
|
-
|
|
177
|
+
deepStringifyFunctions(v, stringified[prop][i]);
|
|
178
178
|
} else if (isFunction(v)) {
|
|
179
179
|
stringified[prop][i] = v.toString();
|
|
180
180
|
} else {
|
|
@@ -188,7 +188,7 @@ const deepStringify = (obj, stringified = {}) => {
|
|
|
188
188
|
return stringified;
|
|
189
189
|
};
|
|
190
190
|
const MAX_DEPTH = 100;
|
|
191
|
-
const
|
|
191
|
+
const deepStringifyFunctionsWithMaxDepth = (obj, stringified = {}, depth = 0, path = "") => {
|
|
192
192
|
if (depth > MAX_DEPTH) {
|
|
193
193
|
console.warn(
|
|
194
194
|
`Maximum depth exceeded at path: ${path}. Possible circular reference.`
|
|
@@ -202,7 +202,7 @@ const deepStringifyWithMaxDepth = (obj, stringified = {}, depth = 0, path = "")
|
|
|
202
202
|
stringified[prop] = objProp.toString();
|
|
203
203
|
} else if (isObject(objProp)) {
|
|
204
204
|
stringified[prop] = {};
|
|
205
|
-
|
|
205
|
+
deepStringifyFunctionsWithMaxDepth(
|
|
206
206
|
objProp,
|
|
207
207
|
stringified[prop],
|
|
208
208
|
depth + 1,
|
|
@@ -214,7 +214,7 @@ const deepStringifyWithMaxDepth = (obj, stringified = {}, depth = 0, path = "")
|
|
|
214
214
|
const itemPath = `${currentPath}[${i}]`;
|
|
215
215
|
if (isObject(v)) {
|
|
216
216
|
stringified[prop][i] = {};
|
|
217
|
-
|
|
217
|
+
deepStringifyFunctionsWithMaxDepth(
|
|
218
218
|
v,
|
|
219
219
|
stringified[prop][i],
|
|
220
220
|
depth + 1,
|
|
@@ -311,7 +311,7 @@ const detachFunctionsFromObject = (obj, detached = {}) => {
|
|
|
311
311
|
if (isFunction(objProp)) continue;
|
|
312
312
|
else if (isObject(objProp)) {
|
|
313
313
|
detached[prop] = {};
|
|
314
|
-
|
|
314
|
+
deepStringifyFunctions(objProp, detached[prop]);
|
|
315
315
|
} else if (isArray(objProp)) {
|
|
316
316
|
detached[prop] = [];
|
|
317
317
|
objProp.forEach((v, i) => {
|
|
@@ -350,7 +350,7 @@ const hasFunction = (str) => {
|
|
|
350
350
|
const isJSONLike = /^["[{]/.test(trimmed) && !trimmed.includes("=>");
|
|
351
351
|
return (isFunction2 || isClass) && !isObjectLiteral && !isArrayLiteral && !isJSONLike;
|
|
352
352
|
};
|
|
353
|
-
const
|
|
353
|
+
const deepDestringifyFunctions = (obj, destringified = {}) => {
|
|
354
354
|
for (const prop in obj) {
|
|
355
355
|
const hasOwnProperty2 = Object.prototype.hasOwnProperty.call(obj, prop);
|
|
356
356
|
if (!hasOwnProperty2) continue;
|
|
@@ -381,33 +381,49 @@ const deepDestringify = (obj, destringified = {}) => {
|
|
|
381
381
|
destringified[prop].push(arrProp);
|
|
382
382
|
}
|
|
383
383
|
} else if (isObject(arrProp)) {
|
|
384
|
-
destringified[prop].push(
|
|
384
|
+
destringified[prop].push(deepDestringifyFunctions(arrProp));
|
|
385
385
|
} else {
|
|
386
386
|
destringified[prop].push(arrProp);
|
|
387
387
|
}
|
|
388
388
|
});
|
|
389
389
|
} else if (isObject(objProp)) {
|
|
390
|
-
destringified[prop] =
|
|
390
|
+
destringified[prop] = deepDestringifyFunctions(
|
|
391
|
+
objProp,
|
|
392
|
+
destringified[prop]
|
|
393
|
+
);
|
|
391
394
|
} else {
|
|
392
395
|
destringified[prop] = objProp;
|
|
393
396
|
}
|
|
394
397
|
}
|
|
395
398
|
return destringified;
|
|
396
399
|
};
|
|
397
|
-
const
|
|
400
|
+
const evalStringToObject = (str, opts = { verbose: true }) => {
|
|
398
401
|
try {
|
|
399
|
-
return str ? window.eval("(" + str + ")") : {};
|
|
402
|
+
return str ? (opts.window || window).eval("(" + str + ")") : {};
|
|
400
403
|
} catch (e) {
|
|
401
404
|
if (opts.verbose) console.warn(e);
|
|
402
|
-
if (opts.
|
|
405
|
+
if (opts.onError) return opts.onError(e);
|
|
403
406
|
}
|
|
404
407
|
};
|
|
405
408
|
const diffObjects = (original, objToDiff, cache, opts) => {
|
|
406
409
|
let hasDiff = false;
|
|
407
|
-
|
|
410
|
+
const originalKeys = Object.keys(original);
|
|
411
|
+
const diffKeys = Object.keys(objToDiff);
|
|
412
|
+
const allKeys = [.../* @__PURE__ */ new Set([...originalKeys, ...diffKeys])];
|
|
413
|
+
const originalKeyOrder = originalKeys.join(",");
|
|
414
|
+
const diffKeyOrder = diffKeys.filter((k) => originalKeys.includes(k)).join(",");
|
|
415
|
+
if (originalKeyOrder !== diffKeyOrder) {
|
|
416
|
+
hasDiff = true;
|
|
417
|
+
}
|
|
418
|
+
for (const key of allKeys) {
|
|
408
419
|
if (key === "ref") continue;
|
|
409
420
|
const originalProp = original[key];
|
|
410
421
|
const objToDiffProp = objToDiff[key];
|
|
422
|
+
if (!(key in objToDiff)) {
|
|
423
|
+
cache[key] = void 0;
|
|
424
|
+
hasDiff = true;
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
411
427
|
if (isObject(originalProp) && isObject(objToDiffProp)) {
|
|
412
428
|
const nestedDiff = diff(originalProp, objToDiffProp, {}, opts);
|
|
413
429
|
if (nestedDiff && Object.keys(nestedDiff).length > 0) {
|
|
@@ -432,6 +448,11 @@ const diffArrays = (original, objToDiff, cache, opts) => {
|
|
|
432
448
|
return objToDiff;
|
|
433
449
|
}
|
|
434
450
|
let hasDiff = false;
|
|
451
|
+
const originalStringified = original.map((item) => JSON.stringify(item));
|
|
452
|
+
const diffStringified = objToDiff.map((item) => JSON.stringify(item));
|
|
453
|
+
if (originalStringified.join(",") !== diffStringified.join(",")) {
|
|
454
|
+
hasDiff = true;
|
|
455
|
+
}
|
|
435
456
|
for (let i = 0; i < original.length; i++) {
|
|
436
457
|
const diffObj = diff(original[i], objToDiff[i], {}, opts);
|
|
437
458
|
if (diffObj && (isObject(diffObj) ? Object.keys(diffObj).length > 0 : true)) {
|
|
@@ -439,15 +460,15 @@ const diffArrays = (original, objToDiff, cache, opts) => {
|
|
|
439
460
|
hasDiff = true;
|
|
440
461
|
}
|
|
441
462
|
}
|
|
442
|
-
return hasDiff ?
|
|
463
|
+
return hasDiff ? objToDiff : void 0;
|
|
443
464
|
};
|
|
444
465
|
const diff = (original, objToDiff, cache = {}, opts = {}) => {
|
|
445
466
|
if (opts.cloneInstances) {
|
|
446
467
|
original = deepClone(original);
|
|
447
468
|
objToDiff = deepClone(objToDiff);
|
|
448
469
|
}
|
|
449
|
-
original =
|
|
450
|
-
objToDiff =
|
|
470
|
+
original = deepStringifyFunctions(original);
|
|
471
|
+
objToDiff = deepStringifyFunctions(objToDiff);
|
|
451
472
|
if (isArray(original) && isArray(objToDiff)) {
|
|
452
473
|
const result = diffArrays(original, objToDiff, [], opts);
|
|
453
474
|
return result === void 0 ? {} : result;
|
|
@@ -755,15 +776,16 @@ export {
|
|
|
755
776
|
createObjectWithoutPrototype,
|
|
756
777
|
deepClone,
|
|
757
778
|
deepContains,
|
|
758
|
-
|
|
779
|
+
deepDestringifyFunctions,
|
|
759
780
|
deepDiff,
|
|
760
781
|
deepMerge,
|
|
761
|
-
|
|
762
|
-
|
|
782
|
+
deepStringifyFunctions,
|
|
783
|
+
deepStringifyFunctionsWithMaxDepth,
|
|
763
784
|
detachFunctionsFromObject,
|
|
764
785
|
detectInfiniteLoop,
|
|
765
786
|
diff,
|
|
766
787
|
diffObjects,
|
|
788
|
+
evalStringToObject,
|
|
767
789
|
excludeKeysFromObject,
|
|
768
790
|
exec,
|
|
769
791
|
flattenRecursive,
|
|
@@ -785,6 +807,5 @@ export {
|
|
|
785
807
|
overwriteShallow,
|
|
786
808
|
removeFromObject,
|
|
787
809
|
removeNestedKeyByPath,
|
|
788
|
-
setInObjectByPath
|
|
789
|
-
stringToObject
|
|
810
|
+
setInObjectByPath
|
|
790
811
|
};
|
package/object.js
CHANGED
|
@@ -208,7 +208,7 @@ export const deepClone = (obj, options = {}) => {
|
|
|
208
208
|
/**
|
|
209
209
|
* Stringify object
|
|
210
210
|
*/
|
|
211
|
-
export const
|
|
211
|
+
export const deepStringifyFunctions = (obj, stringified = {}) => {
|
|
212
212
|
if (!obj) return
|
|
213
213
|
if (obj.node || obj.__ref || obj.parent || obj.__element || obj.parse) {
|
|
214
214
|
;(obj.__element || obj.parent?.__element).warn(
|
|
@@ -224,13 +224,13 @@ export const deepStringify = (obj, stringified = {}) => {
|
|
|
224
224
|
stringified[prop] = objProp.toString()
|
|
225
225
|
} else if (isObject(objProp)) {
|
|
226
226
|
stringified[prop] = {}
|
|
227
|
-
|
|
227
|
+
deepStringifyFunctions(objProp, stringified[prop])
|
|
228
228
|
} else if (isArray(objProp)) {
|
|
229
229
|
stringified[prop] = []
|
|
230
230
|
objProp.forEach((v, i) => {
|
|
231
231
|
if (isObject(v)) {
|
|
232
232
|
stringified[prop][i] = {}
|
|
233
|
-
|
|
233
|
+
deepStringifyFunctions(v, stringified[prop][i])
|
|
234
234
|
} else if (isFunction(v)) {
|
|
235
235
|
stringified[prop][i] = v.toString()
|
|
236
236
|
} else {
|
|
@@ -246,7 +246,7 @@ export const deepStringify = (obj, stringified = {}) => {
|
|
|
246
246
|
}
|
|
247
247
|
|
|
248
248
|
const MAX_DEPTH = 100 // Adjust this value as needed
|
|
249
|
-
export const
|
|
249
|
+
export const deepStringifyFunctionsWithMaxDepth = (
|
|
250
250
|
obj,
|
|
251
251
|
stringified = {},
|
|
252
252
|
depth = 0,
|
|
@@ -267,7 +267,7 @@ export const deepStringifyWithMaxDepth = (
|
|
|
267
267
|
stringified[prop] = objProp.toString()
|
|
268
268
|
} else if (isObject(objProp)) {
|
|
269
269
|
stringified[prop] = {}
|
|
270
|
-
|
|
270
|
+
deepStringifyFunctionsWithMaxDepth(
|
|
271
271
|
objProp,
|
|
272
272
|
stringified[prop],
|
|
273
273
|
depth + 1,
|
|
@@ -279,7 +279,7 @@ export const deepStringifyWithMaxDepth = (
|
|
|
279
279
|
const itemPath = `${currentPath}[${i}]`
|
|
280
280
|
if (isObject(v)) {
|
|
281
281
|
stringified[prop][i] = {}
|
|
282
|
-
|
|
282
|
+
deepStringifyFunctionsWithMaxDepth(
|
|
283
283
|
v,
|
|
284
284
|
stringified[prop][i],
|
|
285
285
|
depth + 1,
|
|
@@ -389,7 +389,7 @@ export const detachFunctionsFromObject = (obj, detached = {}) => {
|
|
|
389
389
|
if (isFunction(objProp)) continue
|
|
390
390
|
else if (isObject(objProp)) {
|
|
391
391
|
detached[prop] = {}
|
|
392
|
-
|
|
392
|
+
deepStringifyFunctions(objProp, detached[prop])
|
|
393
393
|
} else if (isArray(objProp)) {
|
|
394
394
|
detached[prop] = []
|
|
395
395
|
objProp.forEach((v, i) => {
|
|
@@ -440,7 +440,7 @@ export const hasFunction = (str) => {
|
|
|
440
440
|
)
|
|
441
441
|
}
|
|
442
442
|
|
|
443
|
-
export const
|
|
443
|
+
export const deepDestringifyFunctions = (obj, destringified = {}) => {
|
|
444
444
|
for (const prop in obj) {
|
|
445
445
|
const hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, prop)
|
|
446
446
|
if (!hasOwnProperty) continue
|
|
@@ -473,13 +473,16 @@ export const deepDestringify = (obj, destringified = {}) => {
|
|
|
473
473
|
destringified[prop].push(arrProp)
|
|
474
474
|
}
|
|
475
475
|
} else if (isObject(arrProp)) {
|
|
476
|
-
destringified[prop].push(
|
|
476
|
+
destringified[prop].push(deepDestringifyFunctions(arrProp))
|
|
477
477
|
} else {
|
|
478
478
|
destringified[prop].push(arrProp)
|
|
479
479
|
}
|
|
480
480
|
})
|
|
481
481
|
} else if (isObject(objProp)) {
|
|
482
|
-
destringified[prop] =
|
|
482
|
+
destringified[prop] = deepDestringifyFunctions(
|
|
483
|
+
objProp,
|
|
484
|
+
destringified[prop]
|
|
485
|
+
)
|
|
483
486
|
} else {
|
|
484
487
|
destringified[prop] = objProp
|
|
485
488
|
}
|
|
@@ -487,24 +490,45 @@ export const deepDestringify = (obj, destringified = {}) => {
|
|
|
487
490
|
return destringified
|
|
488
491
|
}
|
|
489
492
|
|
|
490
|
-
export const
|
|
493
|
+
export const evalStringToObject = (str, opts = { verbose: true }) => {
|
|
491
494
|
try {
|
|
492
|
-
return str ? window.eval('(' + str + ')') : {} // eslint-disable-line
|
|
495
|
+
return str ? (opts.window || window).eval('(' + str + ')') : {} // eslint-disable-line
|
|
493
496
|
} catch (e) {
|
|
494
497
|
if (opts.verbose) console.warn(e)
|
|
495
|
-
if (opts.
|
|
498
|
+
if (opts.onError) return opts.onError(e)
|
|
496
499
|
}
|
|
497
500
|
}
|
|
498
501
|
|
|
499
502
|
export const diffObjects = (original, objToDiff, cache, opts) => {
|
|
500
503
|
let hasDiff = false
|
|
501
504
|
|
|
502
|
-
|
|
505
|
+
// Use union of keys maintaining original order where possible
|
|
506
|
+
const originalKeys = Object.keys(original)
|
|
507
|
+
const diffKeys = Object.keys(objToDiff)
|
|
508
|
+
const allKeys = [...new Set([...originalKeys, ...diffKeys])]
|
|
509
|
+
|
|
510
|
+
// Check if key order has changed
|
|
511
|
+
const originalKeyOrder = originalKeys.join(',')
|
|
512
|
+
const diffKeyOrder = diffKeys
|
|
513
|
+
.filter((k) => originalKeys.includes(k))
|
|
514
|
+
.join(',')
|
|
515
|
+
if (originalKeyOrder !== diffKeyOrder) {
|
|
516
|
+
hasDiff = true
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
for (const key of allKeys) {
|
|
503
520
|
if (key === 'ref') continue
|
|
504
521
|
|
|
505
522
|
const originalProp = original[key]
|
|
506
523
|
const objToDiffProp = objToDiff[key]
|
|
507
524
|
|
|
525
|
+
// Handle deleted keys (missing in objToDiff)
|
|
526
|
+
if (!(key in objToDiff)) {
|
|
527
|
+
cache[key] = undefined
|
|
528
|
+
hasDiff = true
|
|
529
|
+
continue
|
|
530
|
+
}
|
|
531
|
+
|
|
508
532
|
if (isObject(originalProp) && isObject(objToDiffProp)) {
|
|
509
533
|
const nestedDiff = diff(originalProp, objToDiffProp, {}, opts)
|
|
510
534
|
if (nestedDiff && Object.keys(nestedDiff).length > 0) {
|
|
@@ -527,11 +551,23 @@ export const diffObjects = (original, objToDiff, cache, opts) => {
|
|
|
527
551
|
}
|
|
528
552
|
|
|
529
553
|
const diffArrays = (original, objToDiff, cache, opts) => {
|
|
554
|
+
// Different lengths means arrays are different
|
|
530
555
|
if (original.length !== objToDiff.length) {
|
|
531
556
|
return objToDiff
|
|
532
557
|
}
|
|
533
558
|
|
|
534
559
|
let hasDiff = false
|
|
560
|
+
|
|
561
|
+
// First check if any elements have changed position
|
|
562
|
+
// by doing a shallow comparison of elements
|
|
563
|
+
const originalStringified = original.map((item) => JSON.stringify(item))
|
|
564
|
+
const diffStringified = objToDiff.map((item) => JSON.stringify(item))
|
|
565
|
+
|
|
566
|
+
if (originalStringified.join(',') !== diffStringified.join(',')) {
|
|
567
|
+
hasDiff = true
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// Then do deep comparison of each element
|
|
535
571
|
for (let i = 0; i < original.length; i++) {
|
|
536
572
|
const diffObj = diff(original[i], objToDiff[i], {}, opts)
|
|
537
573
|
if (
|
|
@@ -543,7 +579,7 @@ const diffArrays = (original, objToDiff, cache, opts) => {
|
|
|
543
579
|
}
|
|
544
580
|
}
|
|
545
581
|
|
|
546
|
-
return hasDiff ?
|
|
582
|
+
return hasDiff ? objToDiff : undefined
|
|
547
583
|
}
|
|
548
584
|
|
|
549
585
|
export const diff = (original, objToDiff, cache = {}, opts = {}) => {
|
|
@@ -552,8 +588,8 @@ export const diff = (original, objToDiff, cache = {}, opts = {}) => {
|
|
|
552
588
|
objToDiff = deepClone(objToDiff)
|
|
553
589
|
}
|
|
554
590
|
|
|
555
|
-
original =
|
|
556
|
-
objToDiff =
|
|
591
|
+
original = deepStringifyFunctions(original)
|
|
592
|
+
objToDiff = deepStringifyFunctions(objToDiff)
|
|
557
593
|
|
|
558
594
|
if (isArray(original) && isArray(objToDiff)) {
|
|
559
595
|
const result = diffArrays(original, objToDiff, [], opts)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/utils",
|
|
3
|
-
"version": "2.31.
|
|
3
|
+
"version": "2.31.27",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"build": "npx rimraf -I dist; npm run build:cjs; npm run build:esm",
|
|
25
25
|
"prepublish": "npm run build; npm run copy:package:cjs"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "0edcd5aae5c16b6a53a7e9b6512bb2562e8eb8a1",
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@babel/core": "^7.27.1"
|
|
30
30
|
}
|