@domql/utils
Advanced tools
Comparing version 2.4.12 to 2.5.13
@@ -22,2 +22,3 @@ "use strict"; | ||
debounce: () => debounce, | ||
isStringFunction: () => isStringFunction, | ||
memoize: () => memoize | ||
@@ -48,1 +49,5 @@ }); | ||
}; | ||
const isStringFunction = (inputString) => { | ||
const functionRegex = /^((function\s*\([^)]*\)\s*\{[^}]*\})|(\([^)]*\)\s*=>))/; | ||
return functionRegex.test(inputString); | ||
}; |
@@ -383,19 +383,25 @@ "use strict"; | ||
}; | ||
const isEqualDeep = (param, element) => { | ||
if (param === element) | ||
const isEqualDeep = (param, element, visited = /* @__PURE__ */ new Set()) => { | ||
if (typeof param !== "object" || typeof element !== "object" || param === null || element === null) { | ||
return param === element; | ||
} | ||
if (visited.has(param) || visited.has(element)) { | ||
return true; | ||
if (!param || !element) | ||
} | ||
visited.add(param); | ||
visited.add(element); | ||
const keysParam = Object.keys(param); | ||
const keysElement = Object.keys(element); | ||
if (keysParam.length !== keysElement.length) { | ||
return false; | ||
for (const prop in param) { | ||
const paramProp = param[prop]; | ||
const elementProp = element[prop]; | ||
if ((0, import_types.isObjectLike)(paramProp)) { | ||
const isEqual = isEqualDeep(paramProp, elementProp); | ||
if (!isEqual) | ||
return false; | ||
} else { | ||
const isEqual = paramProp === elementProp; | ||
if (!isEqual) | ||
return false; | ||
} | ||
for (const key of keysParam) { | ||
if (!keysElement.includes(key)) { | ||
return false; | ||
} | ||
const paramProp = param[key]; | ||
const elementProp = element[key]; | ||
if (!isEqualDeep(paramProp, elementProp, visited)) { | ||
return false; | ||
} | ||
} | ||
@@ -402,0 +408,0 @@ return true; |
@@ -24,1 +24,9 @@ 'use strict' | ||
} | ||
export const isStringFunction = inputString => { | ||
// Regular expression to match both regular and arrow function declarations | ||
const functionRegex = /^((function\s*\([^)]*\)\s*\{[^}]*\})|(\([^)]*\)\s*=>))/ | ||
// Use the regex to test if the inputString matches the function pattern | ||
return functionRegex.test(inputString) | ||
} |
@@ -378,16 +378,73 @@ 'use strict' | ||
export const isEqualDeep = (param, element) => { | ||
if (param === element) return true | ||
if (!param || !element) return false | ||
for (const prop in param) { | ||
const paramProp = param[prop] | ||
const elementProp = element[prop] | ||
if (isObjectLike(paramProp)) { | ||
const isEqual = isEqualDeep(paramProp, elementProp) | ||
if (!isEqual) return false | ||
} else { | ||
const isEqual = paramProp === elementProp | ||
if (!isEqual) return false | ||
/** | ||
* Recursively compares two values to determine if they are deeply equal. | ||
* | ||
* This function checks for deep equality between two values, including | ||
* objects, arrays, and nested structures. It handles circular references to | ||
* prevent infinite loops. | ||
* | ||
* @param {*} param - The first value to compare. | ||
* @param {*} element - The second value to compare. | ||
* @param {Set} [visited] - (Optional) A set to track visited objects during recursion | ||
* to handle circular references. You can omit this parameter when calling | ||
* the function; it is used internally for tracking visited objects. | ||
* | ||
* @returns {boolean} Returns `true` if the values are deeply equal, `false` otherwise. | ||
* | ||
* @example | ||
* // Comparing primitive values | ||
* isEqualDeep(42, 42); // true | ||
* isEqualDeep('hello', 'hello'); // true | ||
* isEqualDeep(true, true); // true | ||
* isEqualDeep(42, '42'); // false | ||
* | ||
* // Comparing simple objects | ||
* const obj1 = { a: 1, b: { c: 2 } }; | ||
* const obj2 = { a: 1, b: { c: 2 } }; | ||
* isEqualDeep(obj1, obj2); // true | ||
* | ||
* // Handling circular references | ||
* const circularObj = { prop: null }; | ||
* circularObj.prop = circularObj; | ||
* const anotherObj = { prop: null }; | ||
* anotherObj.prop = anotherObj; | ||
* isEqualDeep(circularObj, anotherObj); // true | ||
*/ | ||
export const isEqualDeep = (param, element, visited = new Set()) => { | ||
// Check if both values are non-null objects | ||
if (typeof param !== 'object' || typeof element !== 'object' || param === null || element === null) { | ||
return param === element // Compare non-object values directly | ||
} | ||
// Check for circular references | ||
if (visited.has(param) || visited.has(element)) { | ||
return true // Assume equality to break the circular reference | ||
} | ||
visited.add(param) | ||
visited.add(element) | ||
const keysParam = Object.keys(param) | ||
const keysElement = Object.keys(element) | ||
// Check if both objects have the same number of properties | ||
if (keysParam.length !== keysElement.length) { | ||
return false | ||
} | ||
// Check if all properties in param also exist in element | ||
for (const key of keysParam) { | ||
if (!keysElement.includes(key)) { | ||
return false | ||
} | ||
const paramProp = param[key] | ||
const elementProp = element[key] | ||
// Recursively check property values | ||
if (!isEqualDeep(paramProp, elementProp, visited)) { | ||
return false | ||
} | ||
} | ||
return true | ||
@@ -394,0 +451,0 @@ } |
{ | ||
"name": "@domql/utils", | ||
"version": "2.4.12", | ||
"version": "2.5.13", | ||
"license": "MIT", | ||
@@ -26,3 +26,3 @@ "type": "module", | ||
}, | ||
"gitHead": "fb655eb1dbbde28cb1b843f13aad21afcfcece40", | ||
"gitHead": "3bbff7f4e2ced6f104572489a2779a5cf0dbb450", | ||
"devDependencies": { | ||
@@ -29,0 +29,0 @@ "@babel/core": "^7.12.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60320
1916