diff --git a/src/setIntersection.js b/src/setIntersection.js new file mode 100644 index 0000000..2b3f3b4 --- /dev/null +++ b/src/setIntersection.js @@ -0,0 +1,8 @@ +export default function setIntersecion(firstSet, secondSet) { + + const intersection = new Set([...firstSet].filter(x => secondSet.includes(x))) + + if (!firstSet || !secondSet) { + return undefined + } return Array.from(intersection) +} diff --git a/test/setIntersection_test.js b/test/setIntersection_test.js new file mode 100644 index 0000000..4c3e5d6 --- /dev/null +++ b/test/setIntersection_test.js @@ -0,0 +1,24 @@ +import { expect } from 'chai'; +import setIntersection from '../src/setIntersection'; + +describe('setIntersection()', () => { + it('should be a function', () => { + expect(setIntersection).to.be.a('function'); + }); + + it('Return the intersection of two sets.', () => { + const firstSet = [1, 2, 3, 4]; + const secondSet = [2, 4, 6, 8]; + expect(setIntersection(firstSet, secondSet)).to.deep.equal([2, 4]); + }); + + it('Return the intersection of two sets.', () => { + const firstSet = [3, 6, 9, 12]; + const secondSet = [6, 7, 8, 9]; + expect(setIntersection(firstSet, secondSet)).to.deep.equal([6, 9]); + }); + + it('Will not try to evaluate empty things', () => { + expect(setIntersection('', '')).to.equal(undefined); + }); +});