A utility library for working with sets.
Features
- Flow and TypeScript declarations included.
- CommonJS, UMD and ESM modules provided.
- Zero dependencies.
Installation
Yarn:
yarn add set-fns
NPM:
npm install set-fns
Usage
; const alphabet = ; const sentence = ; console; console; if ! console;
API
set
declare ;
Takes an iterable and returns a set of all items in that iterable. If the iterable is already a set it is returned unchanged.
const a = ; // Set { 1, 2, 3 }const b = ; // Set { 1, 2, 3 }const c = ; // Set { 1, 2, 3 }a === b; // truea === c; // false
and / intersection
declare ;
Takes two iterables and returns a set of all items that appear in both.
const a = 1 2 3 4;const b = 6 5 4 3;; // Set { 3, 4 }; // Set { 3, 4 }
or / union
declare ;
Takes two iterables and returns a set of all items that appear in either.
const a = 1 2 3 4;const b = 6 5 4 3;; // Set { 1, 2, 3, 4, 5, 6 }; // Set { 1, 2, 3, 4, 5, 6 }
not / subtract / difference
declare ;
Takes two iterables and returns a set of all items that appear the first, but not the second.
const a = 1 2 3 4;const b = 6 5 4 3;; // Set { 1, 2 }; // Set { 1, 2 }; // Set { 1, 2 }
xor
declare ;
Takes two iterables and returns a set of all items that appear exclusively in the first or the second (do not appear in both iterables).
const a = 1 2 3 4;const b = 6 5 4 3;; // Set { 1, 2, 5, 6 }
equal
declare ;
Takes two iterables and returns true if both contain the exactly the same items.
const a = 1 2 3;const b = 3 2 1;const c = 3 3 2 2 1 1;; // true; // true
subset
declare ;
Takes two iterables and returns true if the first is a subset of the second.
const a = 1 2;const b = 1 2 3;const c = 1 2;; // true; // true
strictSubset
declare ;
Takes two iterables and returns true if the first is a strict subset of the second.
const a = 1 2;const b = 1 2 3;const c = 1 2;; // true; // false
superset
declare ;
Takes two iterables and returns true if the first is a superset of the second.
const a = 1 2 3;const b = 1 2;const c = 1 2 3;; // true; // true
strictSuperset
declare ;
Takes two iterables and returns true if the first is a strict superset of the second.
const a = 1 2 3;const b = 1 2;const c = 1 2 3;; // true; // false
intersects
declare ;
Takes two iterables and returns true if the two intersect.
const a = 1 2;const b = 2 3;const c = 3 4;; // true; // false