@domql/utils 2.4.12 → 2.5.14

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.
@@ -19,6 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var function_exports = {};
20
20
  __export(function_exports, {
21
21
  debounce: () => debounce,
22
+ isStringFunction: () => isStringFunction,
22
23
  memoize: () => memoize
23
24
  });
24
25
  module.exports = __toCommonJS(function_exports);
@@ -44,3 +45,7 @@ const memoize = (fn) => {
44
45
  }
45
46
  };
46
47
  };
48
+ const isStringFunction = (inputString) => {
49
+ const functionRegex = /^((function\s*\([^)]*\)\s*\{[^}]*\})|(\([^)]*\)\s*=>))/;
50
+ return functionRegex.test(inputString);
51
+ };
@@ -21,6 +21,7 @@ __export(object_exports, {
21
21
  clone: () => clone,
22
22
  deepClone: () => deepClone,
23
23
  deepCloneExclude: () => deepCloneExclude,
24
+ deepContains: () => deepContains,
24
25
  deepDestringify: () => deepDestringify,
25
26
  deepMerge: () => deepMerge,
26
27
  deepStringify: () => deepStringify,
@@ -380,23 +381,56 @@ const flattenRecursive = (param, prop, stack = []) => {
380
381
  delete objectized[prop];
381
382
  return stack;
382
383
  };
383
- const isEqualDeep = (param, element) => {
384
- if (param === element)
384
+ const isEqualDeep = (param, element, visited = /* @__PURE__ */ new Set()) => {
385
+ if (typeof param !== "object" || typeof element !== "object" || param === null || element === null) {
386
+ return param === element;
387
+ }
388
+ if (visited.has(param) || visited.has(element)) {
385
389
  return true;
386
- if (!param || !element)
390
+ }
391
+ visited.add(param);
392
+ visited.add(element);
393
+ const keysParam = Object.keys(param);
394
+ const keysElement = Object.keys(element);
395
+ if (keysParam.length !== keysElement.length) {
387
396
  return false;
388
- for (const prop in param) {
389
- const paramProp = param[prop];
390
- const elementProp = element[prop];
391
- if ((0, import_types.isObjectLike)(paramProp)) {
392
- const isEqual = isEqualDeep(paramProp, elementProp);
393
- if (!isEqual)
394
- return false;
395
- } else {
396
- const isEqual = paramProp === elementProp;
397
- if (!isEqual)
397
+ }
398
+ for (const key of keysParam) {
399
+ if (!keysElement.includes(key)) {
400
+ return false;
401
+ }
402
+ const paramProp = param[key];
403
+ const elementProp = element[key];
404
+ if (!isEqualDeep(paramProp, elementProp, visited)) {
405
+ return false;
406
+ }
407
+ }
408
+ return true;
409
+ };
410
+ const deepContains = (obj1, obj2) => {
411
+ if (typeof obj1 !== typeof obj2) {
412
+ return false;
413
+ }
414
+ if ((0, import_types.isObjectLike)(obj1)) {
415
+ if (Array.isArray(obj1) && Array.isArray(obj2)) {
416
+ if (obj1.length !== obj2.length) {
398
417
  return false;
418
+ }
419
+ for (let i = 0; i < obj1.length; i++) {
420
+ if (!deepContains(obj1[i], obj2[i])) {
421
+ return false;
422
+ }
423
+ }
424
+ } else if ((0, import_types.isObjectLike)(obj1) && obj2 !== null) {
425
+ for (const key in obj1) {
426
+ const hasOwnProperty = Object.prototype.hasOwnProperty.call(obj2, key);
427
+ if (!hasOwnProperty || !deepContains(obj1[key], obj2[key])) {
428
+ return false;
429
+ }
430
+ }
399
431
  }
432
+ } else {
433
+ return obj2 === obj1;
400
434
  }
401
435
  return true;
402
436
  };
package/function.js CHANGED
@@ -21,3 +21,11 @@ export const memoize = (fn) => {
21
21
  }
22
22
  }
23
23
  }
24
+
25
+ export const isStringFunction = inputString => {
26
+ // Regular expression to match both regular and arrow function declarations
27
+ const functionRegex = /^((function\s*\([^)]*\)\s*\{[^}]*\})|(\([^)]*\)\s*=>))/
28
+
29
+ // Use the regex to test if the inputString matches the function pattern
30
+ return functionRegex.test(inputString)
31
+ }
package/object.js CHANGED
@@ -375,20 +375,107 @@ export const flattenRecursive = (param, prop, stack = []) => {
375
375
  return stack
376
376
  }
377
377
 
378
- export const isEqualDeep = (param, element) => {
379
- if (param === element) return true
380
- if (!param || !element) return false
381
- for (const prop in param) {
382
- const paramProp = param[prop]
383
- const elementProp = element[prop]
384
- if (isObjectLike(paramProp)) {
385
- const isEqual = isEqualDeep(paramProp, elementProp)
386
- if (!isEqual) return false
387
- } else {
388
- const isEqual = paramProp === elementProp
389
- if (!isEqual) return false
378
+ /**
379
+ * Recursively compares two values to determine if they are deeply equal.
380
+ *
381
+ * This function checks for deep equality between two values, including
382
+ * objects, arrays, and nested structures. It handles circular references to
383
+ * prevent infinite loops.
384
+ *
385
+ * @param {*} param - The first value to compare.
386
+ * @param {*} element - The second value to compare.
387
+ * @param {Set} [visited] - (Optional) A set to track visited objects during recursion
388
+ * to handle circular references. You can omit this parameter when calling
389
+ * the function; it is used internally for tracking visited objects.
390
+ *
391
+ * @returns {boolean} Returns `true` if the values are deeply equal, `false` otherwise.
392
+ *
393
+ * @example
394
+ * // Comparing primitive values
395
+ * isEqualDeep(42, 42); // true
396
+ * isEqualDeep('hello', 'hello'); // true
397
+ * isEqualDeep(true, true); // true
398
+ * isEqualDeep(42, '42'); // false
399
+ *
400
+ * // Comparing simple objects
401
+ * const obj1 = { a: 1, b: { c: 2 } };
402
+ * const obj2 = { a: 1, b: { c: 2 } };
403
+ * isEqualDeep(obj1, obj2); // true
404
+ *
405
+ * // Handling circular references
406
+ * const circularObj = { prop: null };
407
+ * circularObj.prop = circularObj;
408
+ * const anotherObj = { prop: null };
409
+ * anotherObj.prop = anotherObj;
410
+ * isEqualDeep(circularObj, anotherObj); // true
411
+ */
412
+ export const isEqualDeep = (param, element, visited = new Set()) => {
413
+ // Check if both values are non-null objects
414
+ if (typeof param !== 'object' || typeof element !== 'object' || param === null || element === null) {
415
+ return param === element // Compare non-object values directly
416
+ }
417
+
418
+ // Check for circular references
419
+ if (visited.has(param) || visited.has(element)) {
420
+ return true // Assume equality to break the circular reference
421
+ }
422
+
423
+ visited.add(param)
424
+ visited.add(element)
425
+
426
+ const keysParam = Object.keys(param)
427
+ const keysElement = Object.keys(element)
428
+
429
+ // Check if both objects have the same number of properties
430
+ if (keysParam.length !== keysElement.length) {
431
+ return false
432
+ }
433
+
434
+ // Check if all properties in param also exist in element
435
+ for (const key of keysParam) {
436
+ if (!keysElement.includes(key)) {
437
+ return false
438
+ }
439
+
440
+ const paramProp = param[key]
441
+ const elementProp = element[key]
442
+
443
+ // Recursively check property values
444
+ if (!isEqualDeep(paramProp, elementProp, visited)) {
445
+ return false
390
446
  }
391
447
  }
448
+
449
+ return true
450
+ }
451
+
452
+ export const deepContains = (obj1, obj2) => {
453
+ if (typeof obj1 !== typeof obj2) {
454
+ return false
455
+ }
456
+
457
+ if (isObjectLike(obj1)) {
458
+ if (Array.isArray(obj1) && Array.isArray(obj2)) {
459
+ if (obj1.length !== obj2.length) {
460
+ return false
461
+ }
462
+ for (let i = 0; i < obj1.length; i++) {
463
+ if (!deepContains(obj1[i], obj2[i])) {
464
+ return false
465
+ }
466
+ }
467
+ } else if (isObjectLike(obj1) && obj2 !== null) {
468
+ for (const key in obj1) {
469
+ const hasOwnProperty = Object.prototype.hasOwnProperty.call(obj2, key)
470
+ if (!hasOwnProperty || !deepContains(obj1[key], obj2[key])) {
471
+ return false
472
+ }
473
+ }
474
+ }
475
+ } else {
476
+ return obj2 === obj1
477
+ }
478
+
392
479
  return true
393
480
  }
394
481
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/utils",
3
- "version": "2.4.12",
3
+ "version": "2.5.14",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "index.js",
@@ -23,7 +23,7 @@
23
23
  "build": "yarn build:cjs",
24
24
  "prepublish": "rimraf -I dist && yarn build && yarn copy:package:cjs"
25
25
  },
26
- "gitHead": "fb655eb1dbbde28cb1b843f13aad21afcfcece40",
26
+ "gitHead": "ab852cb3a435b154f6b32a9a664f65ec56a9c49f",
27
27
  "devDependencies": {
28
28
  "@babel/core": "^7.12.0"
29
29
  }