-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4873da5
commit b27079b
Showing
2 changed files
with
42 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Determines whether two arrays are deeply equal. Two arrays are considered equal if they have the | ||
* same length and corresponding elements are equal. This function also supports nested arrays, | ||
* comparing them recursively. | ||
* | ||
* @param {Array} a - The first array to compare. | ||
* @param {Array} b - The second array to compare. | ||
* @returns {boolean} - Returns `true` if the arrays are deeply equal, otherwise `false`. | ||
* | ||
* @example | ||
* arrayEquals([1, 2, 3], [1, 2, 3]); // true | ||
* arrayEquals([1, 2, 3], [3, 2, 1]); // false | ||
* arrayEquals([1, [2, 3]], [1, [2, 3]]); // true | ||
* arrayEquals([1, [2, 3]], [1, [3, 2]]); // false | ||
* arrayEquals([1, 2, 3], null); // false | ||
* arrayEquals(null, null); // false | ||
*/ | ||
const arrayEquals = (a, b) => { | ||
if (!a || !b) { | ||
return false; | ||
} | ||
|
||
const l = a.length; | ||
|
||
if (l !== b.length) { | ||
return false; | ||
} | ||
|
||
for (let i = 0; i < l; i++) { | ||
if (a[i] instanceof Array && b[i] instanceof Array) { | ||
if (!arrayEquals(a[i], b[i])) { | ||
return false; | ||
} | ||
} else if (a[i] !== b[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
export { arrayEquals }; |