@domql/utils 2.3.90 → 2.3.92

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/array.js CHANGED
@@ -20,3 +20,7 @@ export const removeFromArray = (arr, index) => {
20
20
  }
21
21
  return arr
22
22
  }
23
+
24
+ export const swapItemsInArray = (arr, i, j) => {
25
+ [arr[i], arr[j]] = [arr[j], arr[i]]
26
+ }
package/dist/cjs/array.js CHANGED
@@ -19,7 +19,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var array_exports = {};
20
20
  __export(array_exports, {
21
21
  arrayContainsOtherArray: () => arrayContainsOtherArray,
22
- removeFromArray: () => removeFromArray
22
+ removeFromArray: () => removeFromArray,
23
+ swapItemsInArray: () => swapItemsInArray
23
24
  });
24
25
  module.exports = __toCommonJS(array_exports);
25
26
  var import_types = require("./types");
@@ -41,3 +42,6 @@ const removeFromArray = (arr, index) => {
41
42
  }
42
43
  return arr;
43
44
  };
45
+ const swapItemsInArray = (arr, i, j) => {
46
+ [arr[i], arr[j]] = [arr[j], arr[i]];
47
+ };
@@ -26,6 +26,8 @@ __export(object_exports, {
26
26
  deepStringify: () => deepStringify,
27
27
  detachFunctionsFromObject: () => detachFunctionsFromObject,
28
28
  diff: () => diff,
29
+ diffArrays: () => diffArrays,
30
+ diffObjects: () => diffObjects,
29
31
  exec: () => exec,
30
32
  flattenRecursive: () => flattenRecursive,
31
33
  isEqualDeep: () => isEqualDeep,
@@ -245,21 +247,46 @@ const overwrite = (element, params, options) => {
245
247
  }
246
248
  return changes;
247
249
  };
248
- const diff = (obj, original, cache) => {
249
- const changes = cache || {};
250
- for (const e in obj) {
250
+ const diffObjects = (original, objToDiff, cache) => {
251
+ for (const e in objToDiff) {
251
252
  if (e === "ref")
252
253
  continue;
253
254
  const originalProp = original[e];
254
- const objProp = obj[e];
255
- if ((0, import_types.isObjectLike)(originalProp) && (0, import_types.isObjectLike)(objProp)) {
256
- changes[e] = {};
257
- diff(originalProp, objProp, changes[e]);
258
- } else if (objProp !== void 0) {
259
- changes[e] = objProp;
255
+ const objToDiffProp = objToDiff[e];
256
+ if ((0, import_types.isObject)(originalProp) && (0, import_types.isObject)(objToDiffProp)) {
257
+ cache[e] = {};
258
+ diff(originalProp, objToDiffProp, cache[e]);
259
+ } else if (objToDiffProp !== void 0) {
260
+ cache[e] = objToDiffProp;
260
261
  }
261
262
  }
262
- return changes;
263
+ return cache;
264
+ };
265
+ const diffArrays = (original, objToDiff, cache) => {
266
+ if (original.length !== objToDiff.length) {
267
+ cache = objToDiff;
268
+ } else {
269
+ const diffArr = [];
270
+ for (let i = 0; i < original.length; i++) {
271
+ const diffObj = diff(original[i], objToDiff[i]);
272
+ if (Object.keys(diffObj).length > 0) {
273
+ diffArr.push(diffObj);
274
+ }
275
+ }
276
+ if (diffArr.length > 0) {
277
+ cache = diffArr;
278
+ }
279
+ }
280
+ return cache;
281
+ };
282
+ const diff = (original, objToDiff, cache = {}) => {
283
+ if ((0, import_types.isArray)(original) && (0, import_types.isArray)(objToDiff)) {
284
+ cache = [];
285
+ diffArrays(original, objToDiff, cache);
286
+ } else {
287
+ diffObjects(original, objToDiff, cache);
288
+ }
289
+ return cache;
263
290
  };
264
291
  const overwriteObj = (params, obj) => {
265
292
  const changes = {};
package/object.js CHANGED
@@ -229,20 +229,50 @@ export const overwrite = (element, params, options) => {
229
229
  return changes
230
230
  }
231
231
 
232
- export const diff = (obj, original, cache) => {
233
- const changes = cache || {}
234
- for (const e in obj) {
232
+ export const diffObjects = (original, objToDiff, cache) => {
233
+ for (const e in objToDiff) {
235
234
  if (e === 'ref') continue
235
+
236
236
  const originalProp = original[e]
237
- const objProp = obj[e]
238
- if (isObjectLike(originalProp) && isObjectLike(objProp)) {
239
- changes[e] = {}
240
- diff(originalProp, objProp, changes[e])
241
- } else if (objProp !== undefined) {
242
- changes[e] = objProp
237
+ const objToDiffProp = objToDiff[e]
238
+
239
+ if (isObject(originalProp) && isObject(objToDiffProp)) {
240
+ cache[e] = {}
241
+ diff(originalProp, objToDiffProp, cache[e])
242
+ } else if (objToDiffProp !== undefined) {
243
+ cache[e] = objToDiffProp
243
244
  }
244
245
  }
245
- return changes
246
+ return cache
247
+ }
248
+
249
+ export const diffArrays = (original, objToDiff, cache) => {
250
+ if (original.length !== objToDiff.length) {
251
+ cache = objToDiff
252
+ } else {
253
+ const diffArr = []
254
+ for (let i = 0; i < original.length; i++) {
255
+ const diffObj = diff(original[i], objToDiff[i])
256
+ if (Object.keys(diffObj).length > 0) {
257
+ diffArr.push(diffObj)
258
+ }
259
+ }
260
+ if (diffArr.length > 0) {
261
+ cache = diffArr
262
+ }
263
+ }
264
+ return cache
265
+ }
266
+
267
+ export const diff = (original, objToDiff, cache = {}) => {
268
+ if (isArray(original) && isArray(objToDiff)) {
269
+ cache = []
270
+ diffArrays(original, objToDiff, cache)
271
+ } else {
272
+ diffObjects(original, objToDiff, cache)
273
+ }
274
+
275
+ return cache
246
276
  }
247
277
 
248
278
  /**
@@ -264,7 +294,7 @@ export const overwriteObj = (params, obj) => {
264
294
  }
265
295
 
266
296
  /**
267
- * Overwrites DEEPly object properties with another
297
+ * Overwrites DEEPLY object properties with another
268
298
  */
269
299
  export const overwriteDeep = (params, obj) => {
270
300
  for (const e in params) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/utils",
3
- "version": "2.3.90",
3
+ "version": "2.3.92",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "index.js",
@@ -22,5 +22,5 @@
22
22
  "@domql/globals": "latest",
23
23
  "@domql/tags": "latest"
24
24
  },
25
- "gitHead": "ab1a0f74fc9a7cc67cd5e732c7a8577d98fbb9b0"
25
+ "gitHead": "dc86022df601a498fca8c9af5be9e40dcf36ddbf"
26
26
  }