Skip to content

Commit

Permalink
fix(Util): make arraysEqual avoid mutating the input arrays (#3506)
Browse files Browse the repository at this point in the history
  • Loading branch information
thproxy authored and SpaceEEC committed Oct 4, 2019
1 parent a8e3657 commit 8ddd061
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,20 @@ class Util {
}

/**
* Checks whether the arrays are equal, also removes duplicated entries from b.
* @param {Array<*>} a Array which will not be modified.
* @param {Array<*>} b Array to remove duplicated entries from.
* Checks whether two arrays are equal or have the same elements.
* @param {Array<*>} a The first array.
* @param {Array<*>} b The second array.
* @returns {boolean} Whether the arrays are equal.
* @private
*/
static arraysEqual(a, b) {
if (a === b) return true;
if (a.length !== b.length) return false;

for (const item of a) {
const ind = b.indexOf(item);
if (ind !== -1) b.splice(ind, 1);
}
const setA = new Set(a);
const setB = new Set(b);

return b.length === 0;
return a.every(e => setB.has(e)) && b.every(e => setA.has(e));
}

/**
Expand Down

0 comments on commit 8ddd061

Please sign in to comment.