set-manipulator
ES6 set manipulation operations based on https://gist.github.com/jabney/d9d5c13ad7f871ddf03f
Set operations take two arrays and return the result of the operation as an array. Supported operations are union
, intersection
, difference
, complement
, and equals
. difference
is the symmetric difference and complement
is the relative complement. The set operations are fast, even for large arrays.
Installation
npm install set-manipulator
Usage
;const a = 1 1 2 3 3; // [1, 2, 3]const b = 3 4 4 5 5; // [3, 4, 5] // Join two sets together. A ∪ B; // => [1, 2, 3, 4, 5] // The intersection of two sets. A ∩ B; // => [3] // The symmetric difference of two sets. A Δ B; // => [1, 2, 4, 5] // The relative complement, or a minus b. A\B; // => [1] // Set equality. A = B; // => false; // => true
Using Objects
Arrays of objects can be used in set operations as long as they have some type of unique identifier. A custom identity extractor method can be specified as a third argument to any of the set manipulation functions.
;const a = id: a number: 1 id: b number: 2 ;const b = id: c number: 5; // Join two sets together. A ∪ B; // => [{ id: a, number: 1 }, { id: b, number: 2 }, { id: c, number: 5 }];