-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #13
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const _defaultEqualityComparer = require('./.internal/_defaultEqualityComparer'); | ||
|
||
/** | ||
* Produces the set intersection of two sequences by using the | ||
* specified equality comparer, or the default equality comparer | ||
* if none is supplied, function to compare values. | ||
* @param {Array<T>} second | ||
* @param {(a: T, b: T) => boolean} comparer | ||
* @returns | ||
*/ | ||
function Intersect(second, comparer = _defaultEqualityComparer) { | ||
if (!this || !second) { | ||
throw new Error('Value cannot be null or undefined.'); | ||
} | ||
return this.filter(item => second.find(sndItem => comparer(item, sndItem))); | ||
} | ||
|
||
module.exports = Intersect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const expect = require('chai').expect; | ||
require('..')(); | ||
|
||
describe('Array#prototype#Intersect', function() { | ||
describe('Without equality comparer', () => { | ||
const input = [1, 3, 5]; | ||
|
||
it('Should return an empty array', function() { | ||
const expected = []; | ||
|
||
const actual = input.Intersect([2, 4, 6]); | ||
expect(actual).to.eql(expected); | ||
}); | ||
|
||
it('Should return all numbers when second set is same as the first set', function() { | ||
const expected = [1, 3, 5]; | ||
|
||
const actual = input.Intersect([1, 3, 5]); | ||
expect(actual).to.eql(expected); | ||
}); | ||
|
||
it('Should throw an error when second is null', function() { | ||
expect(() => input.Intersect(null)).to.throw(); | ||
}); | ||
|
||
it('Should throw an error when second is undefined', function() { | ||
expect(() => input.Intersect()).to.throw(); | ||
}); | ||
}); | ||
|
||
// | ||
describe('With equality comparer', () => { | ||
const first = [{ x: 0 }, { x: 1 }, { x: 2 }]; | ||
const equalityComparer = (a, b) => a.x === b.x; | ||
|
||
it('Should return an empty array', function() { | ||
const second = [{ x: 3 }, { x: 4 }, { x: 5 }]; | ||
const expected = []; | ||
|
||
const actual = first.Intersect(second, equalityComparer); | ||
expect(actual).to.eql(expected); | ||
}); | ||
|
||
it('Should return the set intersection', function() { | ||
const second = [{ x: 2 }, { x: 4 }, { x: 6 }]; | ||
const expected = [{ x: 2 }]; | ||
|
||
const actual = first.Intersect(second, equalityComparer); | ||
expect(actual).to.eql(expected); | ||
}); | ||
|
||
it('Should return all numbers when second set is same as the first set', function() { | ||
const second = [...first]; | ||
const expected = [...first]; | ||
|
||
const actual = first.Intersect(second, equalityComparer); | ||
expect(actual).to.eql(expected); | ||
}); | ||
|
||
it('Should throw an error when second is null', function() { | ||
expect(() => first.Intersect(null, equalityComparer)).to.throw(); | ||
}); | ||
|
||
it('Should throw an error when second is undefined', function() { | ||
expect(() => first.Intersect(undefined, equalityComparer)).to.throw(); | ||
}); | ||
}); | ||
}); |