diff --git a/snippets/frozenSet.md b/snippets/frozenSet.md new file mode 100644 index 000000000..2568c9deb --- /dev/null +++ b/snippets/frozenSet.md @@ -0,0 +1,24 @@ +--- +title: frozenSet +tags: array,intermediate +--- + +Creates a frozen `Set` object. + +- Use the `new Set()` constructor to create a new `Set` object from `iterable`. +- Set the `add`, `delete` and `clear` methods of the newly created object to `undefined`, so that they cannot be used, practically freezing the object. + +```js +const frozenSet = iterable => { + const s = new Set(iterable); + s.add = undefined; + s.delete = undefined; + s.clear = undefined; + return s; +}; +``` + +```js +frozenSet([1, 2, 3, 1, 2]); +// Set { 1, 2, 3, add: undefined, delete: undefined, clear: undefined } +``` diff --git a/snippets/isDisjoint.md b/snippets/isDisjoint.md new file mode 100644 index 000000000..37e06010e --- /dev/null +++ b/snippets/isDisjoint.md @@ -0,0 +1,21 @@ +--- +title: isDisjoint +tags: array,intermediate +--- + +Checks if the two iterables are disjointed (have no common values). + +- Use the `new Set()` constructor to create a new `Set` object from each iterable. +- Use `Array.prototype.every()` and `Set.prototype.has()` to check that the two iterables have no common values. + +```js +const isDisjoint = (a, b) => { + const sA = new Set(a), sB = new Set(b); + return [...sA].every(v => !sB.has(v)); +}; +``` + +```js +isDisjoint(new Set([1, 2]), new Set([3, 4])); // true +isDisjoint(new Set([1, 2]), new Set([1, 3])); // false +``` diff --git a/snippets/subSet.md b/snippets/subSet.md new file mode 100644 index 000000000..fd8b2fb26 --- /dev/null +++ b/snippets/subSet.md @@ -0,0 +1,21 @@ +--- +title: subSet +tags: array,intermediate +--- + +Checks if the first iterable is a subset of the second one. + +- Use the `new Set()` constructor to create a new `Set` object from each iterable. +- Use `Array.prototype.every()` and `Set.prototype.has()` to check that each value in the first iterable is contained in the second one. + +```js +const subSet = (a, b) => { + const sA = new Set(a), sB = new Set(b); + return [...sA].every(v => sB.has(v)); +}; +``` + +```js +subSet(new Set([1, 2]), new Set([1, 2, 3, 4])); // true +subSet(new Set([1, 5]), new Set([1, 2, 3, 4])); // false +``` diff --git a/snippets/superSet.md b/snippets/superSet.md new file mode 100644 index 000000000..38cc17ccd --- /dev/null +++ b/snippets/superSet.md @@ -0,0 +1,21 @@ +--- +title: superSet +tags: array,intermediate +--- + +Checks if the first iterable is a superset of the second one. + +- Use the `new Set()` constructor to create a new `Set` object from each iterable. +- Use `Array.prototype.every()` and `Set.prototype.has()` to check that each value in the second iterable is contained in the first one. + +```js +const superSet = (a, b) => { + const sA = new Set(a), sB = new Set(b); + return [...sB].every(v => sA.has(v)); +}; +``` + +```js +superSet(new Set([1, 2, 3, 4]), new Set([1, 2])); // true +superSet(new Set([1, 2, 3, 4]), new Set([1, 5])); // false +```