Skip to content

Commit

Permalink
Adds Intersect method (#85)
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
jreina authored Oct 20, 2021
1 parent 449cd32 commit 98a6479
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ interface Array<T> {
resultSelector: ((x: T, y: U) => V),
equalityComparer: IEqualityComparer<T>
): Array<V>;
Intersect<T>(second: Array<T>): Array<T>;
Intersect<T>(second: Array<T>, equalityComparer: IEqualityComparer<T>): Array<T>;
Last<T>(): T;
Join<TOuter, TInner, TKey, TResult>(
inner: TInner[],
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const First = require('./src/First');
const FirstOrDefault = require('./src/FirstOrDefault');
const GroupBy = require('./src/GroupBy');
const GroupJoin = require('./src/GroupJoin');
const Intersect = require('./src/Intersect');
const Join = require('./src/Join');
const Last = require('./src/Last');
const Min = require('./src/Min');
Expand Down Expand Up @@ -53,6 +54,7 @@ const bindAll = function() {
Array.prototype.FirstOrDefault = FirstOrDefault;
Array.prototype.GroupBy = GroupBy;
Array.prototype.GroupJoin = GroupJoin;
Array.prototype.Intersect = Intersect;
Array.prototype.Join = Join;
Array.prototype.Last = Last;
Array.prototype.Min = Min;
Expand Down
18 changes: 18 additions & 0 deletions src/Intersect.js
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;
68 changes: 68 additions & 0 deletions test/Intersect.spec.js
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();
});
});
});

0 comments on commit 98a6479

Please sign in to comment.