diff --git a/README.md b/README.md index a823fc0..a55dbae 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ npm install set-fns ## Usage ```js -import { set, or as union, not as subtract, subset } from "set-fns"; +import { set, union, intersection, difference, subset } from "set-fns"; const alphabet = set("abcdefghijklmnopqrstuvwxyz".split("")); @@ -42,13 +42,13 @@ console.log("All characters", union(alphabet, sentence)); console.log( "Characters from the alphabet used in the sentence", - intersect(alphabet, sentence) + intersection(alphabet, sentence) ); if (!subset(sentence, alphabet)) { console.log( "Extra character(s) found in sentence!", - subtract(sentence, alphabet) + difference(sentence, alphabet) ); } ``` @@ -71,7 +71,7 @@ a === b; // true a === c; // false ``` -### and +### and / intersection @@ -85,9 +85,10 @@ 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]; and(a, b); // Set { 3, 4 } +intersection(a, b); // Set { 3, 4 } ``` -### or +### or / union @@ -101,9 +102,10 @@ 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]; or(a, b); // Set { 1, 2, 3, 4, 5, 6 } +union(a, b); // Set { 1, 2, 3, 4, 5, 6 } ``` -### not +### not / subtract / difference @@ -117,6 +119,8 @@ Takes two iterables and returns a set of all items that appear the first, but no const a = [1, 2, 3, 4]; const b = [6, 5, 4, 3]; not(a, b); // Set { 1, 2 } +subtract(a, b); // Set { 1, 2 } +difference(a, b); // Set { 1, 2 } ``` ### xor diff --git a/index.ts b/index.ts index ed44758..8382cb1 100644 --- a/index.ts +++ b/index.ts @@ -44,8 +44,12 @@ const strictSuperset = (a: Iterable, b: Iterable): boolean => export { set, and, + and as intersection, or, + or as union, not, + not as subtract, + not as difference, xor, equal, subset, diff --git a/test.ts b/test.ts index 833e858..d68840c 100644 --- a/test.ts +++ b/test.ts @@ -2,8 +2,12 @@ import { strict as assert } from "assert"; import { set, and, + intersection, or, + union, not, + subtract, + difference, xor, equal, subset, @@ -22,10 +26,14 @@ assert.deepEqual(set([1, 2, 3]), new Set([1, 2, 3])); })(); assert.deepEqual(and([1, 2, 3], [2, 3, 4]), set([2, 3])); +assert.deepEqual(intersection([1, 2, 3], [2, 3, 4]), set([2, 3])); assert.deepEqual(or([1, 2, 3], [2, 3, 4]), set([1, 2, 3, 4])); +assert.deepEqual(union([1, 2, 3], [2, 3, 4]), set([1, 2, 3, 4])); assert.deepEqual(not([1, 2, 3], [2, 3, 4]), set([1])); +assert.deepEqual(subtract([1, 2, 3], [2, 3, 4]), set([1])); +assert.deepEqual(difference([1, 2, 3], [2, 3, 4]), set([1])); assert.deepEqual(xor([1, 2, 3], [2, 3, 4]), set([1, 4]));