Created
August 6, 2015 21:25
-
-
Save rsms/2d01d7f9f5159a7a7d1f to your computer and use it in GitHub Desktop.
ES6 Set equality test
This file contains hidden or 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
function seteq(a, b) { | |
if (a != b) { | |
if (!a || !b) { return false; } | |
for (var e of a) { | |
if (!b.has(e)) { return false; } | |
} | |
} | |
return true; | |
} | |
var assert = require('assert'); | |
var s1 = Symbol(), s2 = Symbol(); | |
var a = new Set([s1]), b = new Set([s2]); | |
assert(seteq(a, b) === false); | |
assert(seteq(a, null) === false); | |
assert(seteq(null, b) === false); | |
assert(seteq(null, null) === true); | |
assert(seteq(null, undefined) === true); | |
a.add(s2); | |
assert(seteq(a, b) === false); | |
b.add(s1); | |
assert(seteq(a, b) === true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A check that the sets are of same size would greatly help your comparison's accuracy without needing to loop over them in the case they have different numbers of items.