From 44b77af622488327835c405066f3bad3cfbacb08 Mon Sep 17 00:00:00 2001 From: "Philip (flip) Kromer" Date: Fri, 5 Aug 2022 14:26:09 -0500 Subject: [PATCH 1/4] feat: #isEqual set helpers (wip); test coverage; alternate intersection impl --- CHANGELOG.md | 4 ++ set.js | 27 ++++++++ test/set.js | 190 ++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 203 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95d47fcc..6b591834 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.39.3 + +* Adding `#isEqual` to Set helpers. + ## 0.39.2 * Fixing typings of low-level structure consuming methods (@jerome-benoit). diff --git a/set.js b/set.js index e0d020bb..8188e01c 100644 --- a/set.js +++ b/set.js @@ -181,6 +181,33 @@ exports.isSubset = function(A, B) { return true; }; + +/** + * Function returning whether A equals B (same size and B has all elements of A) + * + * @param {Set} A - First set. + * @param {Set} B - Second set. + * @return {boolean} + */ +exports.isEqual = function(A, B) { + var iterator = A.values(), + step; + + // Shortcuts + if (A === B) + return true; + + if (A.size !== B.size) + return false; + + while ((step = iterator.next(), !step.done)) { + if (!B.has(step.value)) + return false; + } + + return true; +}; + /** * Function returning whether A is a superset of B. * diff --git a/test/set.js b/test/set.js index 8624e2cb..7f264f65 100644 --- a/test/set.js +++ b/test/set.js @@ -11,23 +11,93 @@ describe('Set functions', function() { describe('#.intersection', function() { it('should properly compute the intersection of two sets.', function() { - var A = new Set([1, 2, 3]), - B = new Set([2, 3, 4]); + var Exemplar = new Set([2, 3, 1]); + var Equiv = new Set([1, 2, 3]); + var Missing1 = new Set([2, 3]); + var Extra45 = new Set([1, 2, 3, 4, 5]); + var Disjoint = new Set([7, 8, 9]); + var Empty = new Set([]) + // + var WithItself = functions.intersection(Exemplar, Exemplar); + assert.deepStrictEqual(Array.from(WithItself), [2, 3, 1]); + var WithEquiv = functions.intersection(Exemplar, Equiv); + assert.deepStrictEqual(Array.from(WithEquiv), [2, 3, 1]); + var WithSubset = functions.intersection(Exemplar, Missing1); + assert.deepStrictEqual(Array.from(WithSubset), [2, 3]); + var WithExtras = functions.intersection(Exemplar, Extra45); + assert.deepStrictEqual(Array.from(WithExtras), [2, 3, 1]); + var WithDisjoint = functions.intersection(Exemplar, Disjoint); + assert.deepStrictEqual(Array.from(WithDisjoint), []); + var WithEmpty = functions.intersection(Exemplar, Empty); + assert.deepStrictEqual(Array.from(WithEmpty), []); + }); - var I = functions.intersection(A, B); + it('Makes no guarantees for element ordering.', function() { + // To be clear: it *currently* preserves the order of the earliest of + // sets with the smallest size, but do not depend on this behavior. + // + var Exemplar = new Set([2, 3, 99, 1, 10]); + var Equiv = new Set([1, 2, 3, 10, 99]); + var SubsetA = new Set([99, 3, 2]); + var SubsetB = new Set([3, 99, 2]); + // + var R1 = functions.intersection(Exemplar, Equiv); + assert.deepStrictEqual(Array.from(R1), [2, 3, 99, 1, 10]); + // + var R2 = functions.intersection(Equiv, Exemplar); + assert.deepStrictEqual(Array.from(R2), [1, 2, 3, 10, 99]); + // + var R3 = functions.intersection(Exemplar, Equiv, SubsetA); + assert.deepStrictEqual(Array.from(R3), [99, 3, 2]); + // + var R4 = functions.intersection(Exemplar, SubsetB, Equiv, SubsetA); + assert.deepStrictEqual(Array.from(R4), [3, 99, 2]); + }); - assert.deepStrictEqual(Array.from(I), [2, 3]); + it('compares identity, not equivalence.', function() { + var arrE1 = [] + var arrE2 = [] + var arrN1 = [1] + var arrN2 = [1] + var Exemplar = new Set([1, 2, 3, 'same', arrE1, arrN1]); + var NoneIdentical = new Set([2, 3, 'same', arrE2, arrN2]); + var SomeIdentical = new Set([arrN2, 2, 3, 'same', arrE1]); + // + var WithNoneI = functions.intersection(Exemplar, NoneIdentical); + assert.deepStrictEqual(Array.from(WithNoneI), [2, 3, 'same']); + var WithSomeI = functions.intersection(Exemplar, SomeIdentical); + assert.deepStrictEqual(Array.from(WithSomeI), [2, 3, 'same', arrE1]); }); - it('should be variadic.', function() { - var A = new Set([1, 2, 3, 4]), - B = new Set([2, 3, 4]), - C = new Set([1, 4]), - D = new Set([4, 5, 6]); + it('returns a new set, modifying none', function() { + var Exemplar = new Set([1, 2, 3]); + var Missing1 = new Set([2, 3]); + var Extra45 = new Set([1, 2, 3, 4, 5]); + var Disjoint = new Set([7, 8, 9]); + // + functions.intersection(Exemplar, Missing1, Extra45, Disjoint); + // + assert.deepStrictEqual(Array.from(Exemplar), [1, 2, 3]); + assert.deepStrictEqual(Array.from(Missing1), [2, 3]); + assert.deepStrictEqual(Array.from(Extra45), [1, 2, 3, 4, 5]); + assert.deepStrictEqual(Array.from(Disjoint), [7, 8, 9]); + }); + + it('should be variadic (work with multiple sets).', function() { + var Exemplar = new Set([1, 2, 3]); + var Equiv = new Set([1, 2, 3]); + var Missing1 = new Set([2, 3]); + var Extra45 = new Set([1, 2, 3, 4, 5]); + var Disjoint = new Set([7, 8, 9]); + var Empty = new Set([]) - var I = functions.intersection(A, B, C, D); + var I1 = functions.intersection(Exemplar, Missing1, Extra45); - assert.deepStrictEqual(Array.from(I), [4]); + assert.deepStrictEqual(Array.from(I1), [2, 3]); + + var I2 = functions.intersection(Exemplar, Missing1, Extra45, Disjoint); + + assert.deepStrictEqual(Array.from(I2), []); }); }); @@ -82,21 +152,52 @@ describe('Set functions', function() { it('should properly return if the first set is a subset of the second.', function() { var A = new Set([1, 2]), B = new Set([1, 2, 3]), - C = new Set([2, 4]); + C = new Set([2, 4]), + Empty = new Set([]); assert.strictEqual(functions.isSubset(A, B), true); assert.strictEqual(functions.isSubset(C, B), false); + assert.strictEqual(functions.isSubset(Empty, B), true); + assert.strictEqual(functions.isSubset(B, Empty), false); + assert.strictEqual(functions.isSubset(Empty, Empty), true); + }); + }); + + describe('#.isEqual', function() { + it('should properly return if the first set is equal to the second set.', function() { + var Exemplar = new Set([1, 2, 3]), + SameObj = Exemplar, + YepEqualsTo = new Set([1, 2, 3]), + MoreEls = new Set([1, 2, 3, 4]), + FewerEls = new Set([1]), + DifferentEls = new Set([1, 2, 4]), + Empty = new Set([]); + + assert.strictEqual(functions.isEqual(Exemplar, Exemplar), true); + assert.strictEqual(functions.isEqual(SameObj, Exemplar), true); + assert.strictEqual(functions.isEqual(YepEqualsTo, Exemplar), true); + assert.strictEqual(functions.isEqual(Empty, Empty), true); + // + assert.strictEqual(functions.isEqual(MoreEls, Exemplar), false); + assert.strictEqual(functions.isEqual(FewerEls, Exemplar), false); + assert.strictEqual(functions.isEqual(DifferentEls, Exemplar), false); + assert.strictEqual(functions.isEqual(Empty, Exemplar), false); + assert.strictEqual(functions.isEqual(Exemplar, Empty), false); }); }); describe('#.isSuperset', function() { - it('should properly return if the first set is a subset of the second.', function() { + it('should properly return if the first set is a superset of the second.', function() { var A = new Set([1, 2]), B = new Set([1, 2, 3]), - C = new Set([2, 4]); + C = new Set([2, 4]), + Empty = new Set([]); assert.strictEqual(functions.isSuperset(B, A), true); assert.strictEqual(functions.isSuperset(B, C), false); + assert.strictEqual(functions.isSuperset(B, Empty), true); + assert.strictEqual(functions.isSuperset(Empty, B), false); + assert.strictEqual(functions.isSuperset(Empty, Empty), true); }); }); @@ -107,6 +208,10 @@ describe('Set functions', function() { functions.add(A, new Set([2, 3])); assert.deepStrictEqual(Array.from(A), [1, 2, 3]); + + functions.add(A, new Set()); + + assert.deepStrictEqual(Array.from(A), [1, 2, 3]); }); }); @@ -117,16 +222,65 @@ describe('Set functions', function() { functions.subtract(A, new Set([2, 3])); assert.deepStrictEqual(Array.from(A), [1]); + + functions.subtract(A, new Set()); + + assert.deepStrictEqual(Array.from(A), [1]); + }); + + it('should properly subtract sets from an empty set.', function() { + var Empty = new Set([]); + + functions.subtract(Empty, new Set([2, 3])); + + assert.deepStrictEqual(Array.from(Empty), []); + + functions.subtract(Empty, new Set()); + + assert.deepStrictEqual(Array.from(Empty), []); }); }); describe('#.intersect', function() { - it('should properly intersect the second set to the first.', function() { - var A = new Set([1, 2]); + var Equiv = new Set([1, 2, 3]); + var Missing1 = new Set([2, 3]); + var Disjoint = new Set([7, 8, 9]); + var Empty = new Set([]) + + it('should properly intersect overlapping sets.', function() { + var Exemplar = new Set([1, 2, 3]); + functions.intersect(Exemplar, Missing1) + + assert.deepStrictEqual(Array.from(Exemplar), [2, 3]); + assert.deepStrictEqual(Array.from(Missing1), [2, 3]); + }); + + it('should properly intersect equivalent sets.', function() { + var Exemplar = new Set([1, 2, 3]); + functions.intersect(Exemplar, Exemplar) + + assert.deepStrictEqual(Array.from(Exemplar), [1, 2, 3]); + + functions.intersect(Exemplar, Equiv) + + assert.deepStrictEqual(Array.from(Exemplar), [1, 2, 3]); + assert.deepStrictEqual(Array.from(Equiv), [1, 2, 3]); + }); + + it('should properly intersect disjoint sets.', function() { + var Exemplar = new Set([1, 2, 3]); + + functions.intersect(Exemplar, Disjoint); + + assert.deepStrictEqual(Array.from(Exemplar), []); + assert.deepStrictEqual(Array.from(Disjoint), [7, 8, 9]); + + Exemplar = new Set([1, 2, 3]); - functions.intersect(A, new Set([2, 3])); + functions.intersect(Exemplar, Empty); - assert.deepStrictEqual(Array.from(A), [2]); + assert.deepStrictEqual(Array.from(Exemplar), []); + assert.deepStrictEqual(Array.from(Empty), []); }); }); From e0f10849e296ac97eef50463ec7813782e70ebaa Mon Sep 17 00:00:00 2001 From: "Philip (flip) Kromer" Date: Mon, 8 Aug 2022 17:48:49 -0500 Subject: [PATCH 2/4] chore: addressing code review style guidance --- CHANGELOG.md | 4 +- test/_utils.js | 8 +- test/set.js | 344 ++++++++++++++++++++++++------------------------- 3 files changed, 178 insertions(+), 178 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b591834..3b2a0c6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Changelog -## 0.39.3 +## 0.40.0 -* Adding `#isEqual` to Set helpers. +* Adding `#isEqual` to Set helpers (@mrflip). (provisional) ## 0.39.2 diff --git a/test/_utils.js b/test/_utils.js index 7e93043b..75d9ed19 100644 --- a/test/_utils.js +++ b/test/_utils.js @@ -14,14 +14,14 @@ describe('utils', function() { describe('typed-arrays', function() { describe('#.getPointerArray', function() { - var validatePointerArrayConstructor = function (min, max, expectedCtor) { - it(`returns ${expectedCtor} for ${min}`, () => { + var validatePointerArrayConstructor = function(min, max, expectedCtor) { + it(`returns ${expectedCtor} for ${min}`, function() { assert.strictEqual(typed.getPointerArray(min), expectedCtor); }); - it(`returns ${expectedCtor} for ${(max - min) / 2}`, () => { + it(`returns ${expectedCtor} for ${(max - min) / 2}`, function() { assert.strictEqual(typed.getPointerArray((max - min) / 2), expectedCtor); }); - it(`returns ${expectedCtor} for ${max}`, () => { + it(`returns ${expectedCtor} for ${max}`, function() { assert.strictEqual(typed.getPointerArray(max), expectedCtor); }); }; diff --git a/test/set.js b/test/set.js index 7f264f65..fbeea76b 100644 --- a/test/set.js +++ b/test/set.js @@ -11,47 +11,47 @@ describe('Set functions', function() { describe('#.intersection', function() { it('should properly compute the intersection of two sets.', function() { - var Exemplar = new Set([2, 3, 1]); - var Equiv = new Set([1, 2, 3]); - var Missing1 = new Set([2, 3]); - var Extra45 = new Set([1, 2, 3, 4, 5]); - var Disjoint = new Set([7, 8, 9]); - var Empty = new Set([]) + var exemplar = new Set([2, 3, 1]); + var equiv = new Set([1, 2, 3]); + var missing1 = new Set([2, 3]); + var extra45 = new Set([1, 2, 3, 4, 5]); + var disjoint = new Set([7, 8, 9]); + var empty = new Set([]) // - var WithItself = functions.intersection(Exemplar, Exemplar); - assert.deepStrictEqual(Array.from(WithItself), [2, 3, 1]); - var WithEquiv = functions.intersection(Exemplar, Equiv); - assert.deepStrictEqual(Array.from(WithEquiv), [2, 3, 1]); - var WithSubset = functions.intersection(Exemplar, Missing1); - assert.deepStrictEqual(Array.from(WithSubset), [2, 3]); - var WithExtras = functions.intersection(Exemplar, Extra45); - assert.deepStrictEqual(Array.from(WithExtras), [2, 3, 1]); - var WithDisjoint = functions.intersection(Exemplar, Disjoint); - assert.deepStrictEqual(Array.from(WithDisjoint), []); - var WithEmpty = functions.intersection(Exemplar, Empty); - assert.deepStrictEqual(Array.from(WithEmpty), []); + var withItself = functions.intersection(exemplar, exemplar); + assert.deepStrictEqual(Array.from(withItself), [2, 3, 1]); + var withEquiv = functions.intersection(exemplar, equiv); + assert.deepStrictEqual(Array.from(withEquiv), [2, 3, 1]); + var withSubset = functions.intersection(exemplar, missing1); + assert.deepStrictEqual(Array.from(withSubset), [2, 3]); + var withExtras = functions.intersection(exemplar, extra45); + assert.deepStrictEqual(Array.from(withExtras), [2, 3, 1]); + var withDisjoint = functions.intersection(exemplar, disjoint); + assert.deepStrictEqual(Array.from(withDisjoint), []); + var withEmpty = functions.intersection(exemplar, empty); + assert.deepStrictEqual(Array.from(withEmpty), []); }); - it('Makes no guarantees for element ordering.', function() { + it('makes no guarantees for element ordering.', function() { // To be clear: it *currently* preserves the order of the earliest of // sets with the smallest size, but do not depend on this behavior. // - var Exemplar = new Set([2, 3, 99, 1, 10]); - var Equiv = new Set([1, 2, 3, 10, 99]); - var SubsetA = new Set([99, 3, 2]); - var SubsetB = new Set([3, 99, 2]); + var exemplar = new Set([2, 3, 99, 1, 10]); + var equiv = new Set([1, 2, 3, 10, 99]); + var subsetA = new Set([99, 3, 2]); + var subsetB = new Set([3, 99, 2]); // - var R1 = functions.intersection(Exemplar, Equiv); - assert.deepStrictEqual(Array.from(R1), [2, 3, 99, 1, 10]); + var r1 = functions.intersection(exemplar, equiv); + assert.deepStrictEqual(Array.from(r1), [2, 3, 99, 1, 10]); // - var R2 = functions.intersection(Equiv, Exemplar); - assert.deepStrictEqual(Array.from(R2), [1, 2, 3, 10, 99]); + var r2 = functions.intersection(equiv, exemplar); + assert.deepStrictEqual(Array.from(r2), [1, 2, 3, 10, 99]); // - var R3 = functions.intersection(Exemplar, Equiv, SubsetA); - assert.deepStrictEqual(Array.from(R3), [99, 3, 2]); + var r3 = functions.intersection(exemplar, equiv, subsetA); + assert.deepStrictEqual(Array.from(r3), [99, 3, 2]); // - var R4 = functions.intersection(Exemplar, SubsetB, Equiv, SubsetA); - assert.deepStrictEqual(Array.from(R4), [3, 99, 2]); + var r4 = functions.intersection(exemplar, subsetB, equiv, subsetA); + assert.deepStrictEqual(Array.from(r4), [3, 99, 2]); }); it('compares identity, not equivalence.', function() { @@ -59,274 +59,274 @@ describe('Set functions', function() { var arrE2 = [] var arrN1 = [1] var arrN2 = [1] - var Exemplar = new Set([1, 2, 3, 'same', arrE1, arrN1]); - var NoneIdentical = new Set([2, 3, 'same', arrE2, arrN2]); - var SomeIdentical = new Set([arrN2, 2, 3, 'same', arrE1]); + var exemplar = new Set([1, 2, 3, 'same', arrE1, arrN1]); + var noneIdentical = new Set([2, 3, 'same', arrE2, arrN2]); + var someIdentical = new Set([arrN2, 2, 3, 'same', arrE1]); // - var WithNoneI = functions.intersection(Exemplar, NoneIdentical); - assert.deepStrictEqual(Array.from(WithNoneI), [2, 3, 'same']); - var WithSomeI = functions.intersection(Exemplar, SomeIdentical); - assert.deepStrictEqual(Array.from(WithSomeI), [2, 3, 'same', arrE1]); + var withNoneI = functions.intersection(exemplar, noneIdentical); + assert.deepStrictEqual(Array.from(withNoneI), [2, 3, 'same']); + var withSomeI = functions.intersection(exemplar, someIdentical); + assert.deepStrictEqual(Array.from(withSomeI), [2, 3, 'same', arrE1]); }); it('returns a new set, modifying none', function() { - var Exemplar = new Set([1, 2, 3]); - var Missing1 = new Set([2, 3]); - var Extra45 = new Set([1, 2, 3, 4, 5]); - var Disjoint = new Set([7, 8, 9]); + var exemplar = new Set([1, 2, 3]); + var missing1 = new Set([2, 3]); + var extra45 = new Set([1, 2, 3, 4, 5]); + var disjoint = new Set([7, 8, 9]); // - functions.intersection(Exemplar, Missing1, Extra45, Disjoint); + functions.intersection(exemplar, missing1, extra45, disjoint); // - assert.deepStrictEqual(Array.from(Exemplar), [1, 2, 3]); - assert.deepStrictEqual(Array.from(Missing1), [2, 3]); - assert.deepStrictEqual(Array.from(Extra45), [1, 2, 3, 4, 5]); - assert.deepStrictEqual(Array.from(Disjoint), [7, 8, 9]); + assert.deepStrictEqual(Array.from(exemplar), [1, 2, 3]); + assert.deepStrictEqual(Array.from(missing1), [2, 3]); + assert.deepStrictEqual(Array.from(extra45), [1, 2, 3, 4, 5]); + assert.deepStrictEqual(Array.from(disjoint), [7, 8, 9]); }); it('should be variadic (work with multiple sets).', function() { - var Exemplar = new Set([1, 2, 3]); - var Equiv = new Set([1, 2, 3]); - var Missing1 = new Set([2, 3]); - var Extra45 = new Set([1, 2, 3, 4, 5]); - var Disjoint = new Set([7, 8, 9]); - var Empty = new Set([]) + var exemplar = new Set([1, 2, 3]); + var equiv = new Set([1, 2, 3]); + var missing1 = new Set([2, 3]); + var extra45 = new Set([1, 2, 3, 4, 5]); + var disjoint = new Set([7, 8, 9]); + var empty = new Set([]) - var I1 = functions.intersection(Exemplar, Missing1, Extra45); + var intersectioned1 = functions.intersection(exemplar, missing1, extra45); - assert.deepStrictEqual(Array.from(I1), [2, 3]); + assert.deepStrictEqual(Array.from(intersectioned1), [2, 3]); - var I2 = functions.intersection(Exemplar, Missing1, Extra45, Disjoint); + var intersectioned2 = functions.intersection(exemplar, missing1, extra45, disjoint); - assert.deepStrictEqual(Array.from(I2), []); + assert.deepStrictEqual(Array.from(intersectioned2), []); }); }); describe('#.union', function() { it('should properly compute the union of two sets.', function() { - var A = new Set([1, 2, 3]), - B = new Set([2, 3, 4]); + var setA = new Set([1, 2, 3]), + setB = new Set([2, 3, 4]); - var U = functions.union(A, B); + var unioned = functions.union(setA, setB); - assert.deepStrictEqual(Array.from(U), [1, 2, 3, 4]); + assert.deepStrictEqual(Array.from(unioned), [1, 2, 3, 4]); }); it('should be variadic.', function() { - var A = new Set([1, 2, 3, 4]), - B = new Set([2, 3, 4]), - C = new Set([1, 4]), - D = new Set([4, 5, 6]); + var setA = new Set([1, 2, 3, 4]), + setB = new Set([2, 3, 4]), + setC = new Set([1, 4]), + setD = new Set([4, 5, 6]); - var U = functions.union(A, B, C, D); + var unioned = functions.union(setA, setB, setC, setD); - assert.deepStrictEqual(Array.from(U), [1, 2, 3, 4, 5, 6]); + assert.deepStrictEqual(Array.from(unioned), [1, 2, 3, 4, 5, 6]); }); }); describe('#.difference', function() { it('should properly compute the difference of two sets.', function() { - var A = new Set([1, 2, 3, 4, 5]), - B = new Set([2, 3]); + var setA = new Set([1, 2, 3, 4, 5]), + setB = new Set([2, 3]); - var D = functions.difference(A, B); + var differenced = functions.difference(setA, setB); - assert.deepStrictEqual(Array.from(D), [1, 4, 5]); + assert.deepStrictEqual(Array.from(differenced), [1, 4, 5]); }); }); describe('#.symmetricDifference', function() { it('should properly compute the symmetric difference of two sets.', function() { - var A = new Set([1, 2, 3]), - B = new Set([3, 4, 5]); + var setA = new Set([1, 2, 3]), + setB = new Set([3, 4, 5]); - var S = functions.symmetricDifference(A, B); + var symDiffed = functions.symmetricDifference(setA, setB); - assert.deepStrictEqual(Array.from(S), [1, 2, 4, 5]); + assert.deepStrictEqual(Array.from(symDiffed), [1, 2, 4, 5]); }); }); describe('#.isSubset', function() { it('should properly return if the first set is a subset of the second.', function() { - var A = new Set([1, 2]), - B = new Set([1, 2, 3]), - C = new Set([2, 4]), - Empty = new Set([]); - - assert.strictEqual(functions.isSubset(A, B), true); - assert.strictEqual(functions.isSubset(C, B), false); - assert.strictEqual(functions.isSubset(Empty, B), true); - assert.strictEqual(functions.isSubset(B, Empty), false); - assert.strictEqual(functions.isSubset(Empty, Empty), true); + var setA = new Set([1, 2]), + setB = new Set([1, 2, 3]), + setC = new Set([2, 4]), + empty = new Set([]); + + assert.strictEqual(functions.isSubset(setA, setB), true); + assert.strictEqual(functions.isSubset(setC, setB), false); + assert.strictEqual(functions.isSubset(empty, setB), true); + assert.strictEqual(functions.isSubset(setB, empty), false); + assert.strictEqual(functions.isSubset(empty, empty), true); }); }); describe('#.isEqual', function() { it('should properly return if the first set is equal to the second set.', function() { - var Exemplar = new Set([1, 2, 3]), - SameObj = Exemplar, - YepEqualsTo = new Set([1, 2, 3]), - MoreEls = new Set([1, 2, 3, 4]), - FewerEls = new Set([1]), - DifferentEls = new Set([1, 2, 4]), - Empty = new Set([]); - - assert.strictEqual(functions.isEqual(Exemplar, Exemplar), true); - assert.strictEqual(functions.isEqual(SameObj, Exemplar), true); - assert.strictEqual(functions.isEqual(YepEqualsTo, Exemplar), true); - assert.strictEqual(functions.isEqual(Empty, Empty), true); + var exemplar = new Set([1, 2, 3]), + sameObj = exemplar, + yepEqualsTo = new Set([1, 2, 3]), + moreEls = new Set([1, 2, 3, 4]), + fewerEls = new Set([1]), + differentEls = new Set([1, 2, 4]), + empty = new Set([]); + + assert.strictEqual(functions.isEqual(exemplar, exemplar), true); + assert.strictEqual(functions.isEqual(sameObj, exemplar), true); + assert.strictEqual(functions.isEqual(yepEqualsTo, exemplar), true); + assert.strictEqual(functions.isEqual(empty, empty), true); // - assert.strictEqual(functions.isEqual(MoreEls, Exemplar), false); - assert.strictEqual(functions.isEqual(FewerEls, Exemplar), false); - assert.strictEqual(functions.isEqual(DifferentEls, Exemplar), false); - assert.strictEqual(functions.isEqual(Empty, Exemplar), false); - assert.strictEqual(functions.isEqual(Exemplar, Empty), false); + assert.strictEqual(functions.isEqual(moreEls, exemplar), false); + assert.strictEqual(functions.isEqual(fewerEls, exemplar), false); + assert.strictEqual(functions.isEqual(differentEls, exemplar), false); + assert.strictEqual(functions.isEqual(empty, exemplar), false); + assert.strictEqual(functions.isEqual(exemplar, empty), false); }); }); describe('#.isSuperset', function() { it('should properly return if the first set is a superset of the second.', function() { - var A = new Set([1, 2]), - B = new Set([1, 2, 3]), - C = new Set([2, 4]), - Empty = new Set([]); - - assert.strictEqual(functions.isSuperset(B, A), true); - assert.strictEqual(functions.isSuperset(B, C), false); - assert.strictEqual(functions.isSuperset(B, Empty), true); - assert.strictEqual(functions.isSuperset(Empty, B), false); - assert.strictEqual(functions.isSuperset(Empty, Empty), true); + var setA = new Set([1, 2]), + setB = new Set([1, 2, 3]), + setC = new Set([2, 4]), + empty = new Set([]); + + assert.strictEqual(functions.isSuperset(setB, setA), true); + assert.strictEqual(functions.isSuperset(setB, setC), false); + assert.strictEqual(functions.isSuperset(setB, empty), true); + assert.strictEqual(functions.isSuperset(empty, setB), false); + assert.strictEqual(functions.isSuperset(empty, empty), true); }); }); describe('#.add', function() { it('should properly add the second set to the first.', function() { - var A = new Set([1, 2]); + var setA = new Set([1, 2]); - functions.add(A, new Set([2, 3])); + functions.add(setA, new Set([2, 3])); - assert.deepStrictEqual(Array.from(A), [1, 2, 3]); + assert.deepStrictEqual(Array.from(setA), [1, 2, 3]); - functions.add(A, new Set()); + functions.add(setA, new Set()); - assert.deepStrictEqual(Array.from(A), [1, 2, 3]); + assert.deepStrictEqual(Array.from(setA), [1, 2, 3]); }); }); describe('#.subtract', function() { it('should properly subtract the second set to the first.', function() { - var A = new Set([1, 2]); + var setA = new Set([1, 2]); - functions.subtract(A, new Set([2, 3])); + functions.subtract(setA, new Set([2, 3])); - assert.deepStrictEqual(Array.from(A), [1]); + assert.deepStrictEqual(Array.from(setA), [1]); - functions.subtract(A, new Set()); + functions.subtract(setA, new Set()); - assert.deepStrictEqual(Array.from(A), [1]); + assert.deepStrictEqual(Array.from(setA), [1]); }); it('should properly subtract sets from an empty set.', function() { - var Empty = new Set([]); + var empty = new Set([]); - functions.subtract(Empty, new Set([2, 3])); + functions.subtract(empty, new Set([2, 3])); - assert.deepStrictEqual(Array.from(Empty), []); + assert.deepStrictEqual(Array.from(empty), []); - functions.subtract(Empty, new Set()); + functions.subtract(empty, new Set()); - assert.deepStrictEqual(Array.from(Empty), []); + assert.deepStrictEqual(Array.from(empty), []); }); }); describe('#.intersect', function() { - var Equiv = new Set([1, 2, 3]); - var Missing1 = new Set([2, 3]); - var Disjoint = new Set([7, 8, 9]); - var Empty = new Set([]) + var equiv = new Set([1, 2, 3]); + var missing1 = new Set([2, 3]); + var disjoint = new Set([7, 8, 9]); + var empty = new Set([]) it('should properly intersect overlapping sets.', function() { - var Exemplar = new Set([1, 2, 3]); - functions.intersect(Exemplar, Missing1) + var exemplar = new Set([1, 2, 3]); + functions.intersect(exemplar, missing1) - assert.deepStrictEqual(Array.from(Exemplar), [2, 3]); - assert.deepStrictEqual(Array.from(Missing1), [2, 3]); + assert.deepStrictEqual(Array.from(exemplar), [2, 3]); + assert.deepStrictEqual(Array.from(missing1), [2, 3]); }); it('should properly intersect equivalent sets.', function() { - var Exemplar = new Set([1, 2, 3]); - functions.intersect(Exemplar, Exemplar) + var exemplar = new Set([1, 2, 3]); + functions.intersect(exemplar, exemplar) - assert.deepStrictEqual(Array.from(Exemplar), [1, 2, 3]); + assert.deepStrictEqual(Array.from(exemplar), [1, 2, 3]); - functions.intersect(Exemplar, Equiv) + functions.intersect(exemplar, equiv) - assert.deepStrictEqual(Array.from(Exemplar), [1, 2, 3]); - assert.deepStrictEqual(Array.from(Equiv), [1, 2, 3]); + assert.deepStrictEqual(Array.from(exemplar), [1, 2, 3]); + assert.deepStrictEqual(Array.from(equiv), [1, 2, 3]); }); it('should properly intersect disjoint sets.', function() { - var Exemplar = new Set([1, 2, 3]); + var exemplar = new Set([1, 2, 3]); - functions.intersect(Exemplar, Disjoint); + functions.intersect(exemplar, disjoint); - assert.deepStrictEqual(Array.from(Exemplar), []); - assert.deepStrictEqual(Array.from(Disjoint), [7, 8, 9]); + assert.deepStrictEqual(Array.from(exemplar), []); + assert.deepStrictEqual(Array.from(disjoint), [7, 8, 9]); - Exemplar = new Set([1, 2, 3]); + exemplar = new Set([1, 2, 3]); - functions.intersect(Exemplar, Empty); + functions.intersect(exemplar, empty); - assert.deepStrictEqual(Array.from(Exemplar), []); - assert.deepStrictEqual(Array.from(Empty), []); + assert.deepStrictEqual(Array.from(exemplar), []); + assert.deepStrictEqual(Array.from(empty), []); }); }); describe('#.disjunct', function() { it('should properly disjunct the second set to the first.', function() { - var A = new Set([1, 2]); + var setA = new Set([1, 2]); - functions.disjunct(A, new Set([2, 3])); + functions.disjunct(setA, new Set([2, 3])); - assert.deepStrictEqual(Array.from(A), [1, 3]); + assert.deepStrictEqual(Array.from(setA), [1, 3]); }); }); describe('#.intersectionSize', function() { it('should properly return the size of the intersection.', function() { - var A = new Set([1, 2, 3]), - B = new Set([2, 3, 4]); + var setA = new Set([1, 2, 3]), + setB = new Set([2, 3, 4]); - var N = new Set([]); + var emptySet = new Set([]); - assert.strictEqual(functions.intersectionSize(A, B), 2); - assert.strictEqual(functions.intersectionSize(A, N), 0); + assert.strictEqual(functions.intersectionSize(setA, setB), 2); + assert.strictEqual(functions.intersectionSize(setA, emptySet), 0); }); }); describe('#.unionSize', function() { it('should properly return the size of the union.', function() { - var A = new Set([1, 2, 3]), - B = new Set([2, 3, 4]); + var setA = new Set([1, 2, 3]), + setB = new Set([2, 3, 4]); - var N = new Set([]); + var emptySet = new Set([]); - assert.strictEqual(functions.unionSize(A, B), 4); - assert.strictEqual(functions.unionSize(A, N), 3); + assert.strictEqual(functions.unionSize(setA, setB), 4); + assert.strictEqual(functions.unionSize(setA, emptySet), 3); }); }); describe('#.jaccard', function() { it('should properly return the Jaccard similarity between two sets.', function() { - var A = new Set([1, 2, 3]), - B = new Set([2, 3, 4]); + var setA = new Set([1, 2, 3]), + setB = new Set([2, 3, 4]); - var N = new Set([]); + var emptySet = new Set([]); - assert.strictEqual(functions.jaccard(A, B), 2 / 4); - assert.strictEqual(functions.jaccard(A, N), 0); + assert.strictEqual(functions.jaccard(setA, setB), 2 / 4); + assert.strictEqual(functions.jaccard(setA, emptySet), 0); assert.strictEqual(functions.jaccard(new Set('contact'), new Set('context')), 4 / 7); }); @@ -334,13 +334,13 @@ describe('Set functions', function() { describe('#.overlap', function() { it('should properly return the overlap coefficient between two sets.', function() { - var A = new Set([1, 2, 3]), - B = new Set([2, 3, 4]); + var setA = new Set([1, 2, 3]), + setB = new Set([2, 3, 4]); - var N = new Set([]); + var emptySet = new Set([]); - assert.strictEqual(functions.overlap(A, B), 2 / 3); - assert.strictEqual(functions.overlap(A, N), 0); + assert.strictEqual(functions.overlap(setA, setB), 2 / 3); + assert.strictEqual(functions.overlap(setA, emptySet), 0); assert.strictEqual(functions.overlap(new Set('contact'), new Set('context')), 4 / 5); }); From e60b077c034c732b946b8f6cc9837a5ed9d4c951 Mon Sep 17 00:00:00 2001 From: "Philip (flip) Kromer" Date: Mon, 8 Aug 2022 18:14:17 -0500 Subject: [PATCH 3/4] chore: added mocha linter --- package-lock.json | 39 +++++++++++++++++++++++++++++++++++++++ package.json | 8 +++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 821609e9..c6995a47 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "asciitree": "^1.0.2", "damerau-levenshtein": "^1.0.7", "eslint": "^8.2.0", + "eslint-plugin-mocha": "^10.1.0", "leven": "^3.1.0", "lodash": "^4.17.21", "matcha": "^0.7.0", @@ -537,6 +538,22 @@ "url": "https://opencollective.com/eslint" } }, + "node_modules/eslint-plugin-mocha": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-mocha/-/eslint-plugin-mocha-10.1.0.tgz", + "integrity": "sha512-xLqqWUF17llsogVOC+8C6/jvQ+4IoOREbN7ZCHuOHuD6cT5cDD4h7f2LgsZuzMAiwswWE21tO7ExaknHVDrSkw==", + "dev": true, + "dependencies": { + "eslint-utils": "^3.0.0", + "rambda": "^7.1.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "eslint": ">=7.0.0" + } + }, "node_modules/eslint-scope": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-6.0.0.tgz", @@ -1410,6 +1427,12 @@ "node": ">=6" } }, + "node_modules/rambda": { + "version": "7.2.1", + "resolved": "https://registry.npmjs.org/rambda/-/rambda-7.2.1.tgz", + "integrity": "sha512-Wswj8ZvzdI3VhaGPkZAxaCTwuMmGtgWt7Zxsgyo4P+iTmVnkojvyWaOep5q3ZjMIecW0wtQa66GWxaKkZ24RAA==", + "dev": true + }, "node_modules/randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", @@ -2238,6 +2261,16 @@ "v8-compile-cache": "^2.0.3" } }, + "eslint-plugin-mocha": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-mocha/-/eslint-plugin-mocha-10.1.0.tgz", + "integrity": "sha512-xLqqWUF17llsogVOC+8C6/jvQ+4IoOREbN7ZCHuOHuD6cT5cDD4h7f2LgsZuzMAiwswWE21tO7ExaknHVDrSkw==", + "dev": true, + "requires": { + "eslint-utils": "^3.0.0", + "rambda": "^7.1.0" + } + }, "eslint-scope": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-6.0.0.tgz", @@ -2894,6 +2927,12 @@ "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==", "dev": true }, + "rambda": { + "version": "7.2.1", + "resolved": "https://registry.npmjs.org/rambda/-/rambda-7.2.1.tgz", + "integrity": "sha512-Wswj8ZvzdI3VhaGPkZAxaCTwuMmGtgWt7Zxsgyo4P+iTmVnkojvyWaOep5q3ZjMIecW0wtQa66GWxaKkZ24RAA==", + "dev": true + }, "randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", diff --git a/package.json b/package.json index 4efe74d4..1fb3a229 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "asciitree": "^1.0.2", "damerau-levenshtein": "^1.0.7", "eslint": "^8.2.0", + "eslint-plugin-mocha": "^10.1.0", "leven": "^3.1.0", "lodash": "^4.17.21", "matcha": "^0.7.0", @@ -90,7 +91,10 @@ "typescript": "^4.5.2" }, "eslintConfig": { - "extends": "@yomguithereal/eslint-config", + "extends": [ + "plugin:mocha/recommended", + "@yomguithereal/eslint-config" + ], "parserOptions": { "ecmaVersion": 6, "ecmaFeatures": { @@ -98,6 +102,8 @@ } }, "rules": { + "mocha/no-identical-title": 0, + "mocha/no-setup-in-describe": 0, "no-new": 0 } } From 343152d5c8e387119e8c9368288ca23ed5109078 Mon Sep 17 00:00:00 2001 From: "Philip (flip) Kromer" Date: Mon, 8 Aug 2022 18:15:43 -0500 Subject: [PATCH 4/4] chore: celebrate success of test suite --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1fb3a229..150dfb9e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "scripts": { "lint": "eslint --cache ./*.js ./utils ./test", "lint:fix": "eslint --cache --fix ./*.js ./utils ./test", - "prepublishOnly": "npm run lint && npm test && npm run test:types", + "prepublishOnly": "npm run lint && npm test && npm run test:types && echo 'Success! All checks passed!'", "test": "mocha", "test:types": "tsc --target es2015 --noEmit --noImplicitAny --noImplicitReturns ./test/types.ts" },