Skip to content

Commit

Permalink
Adds distinct (#81)
Browse files Browse the repository at this point in the history
Closes #8
  • Loading branch information
jreina authored Jan 25, 2019
1 parent 544bb93 commit 124db28
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ interface Array<T> {
Contains<T>(element: T): boolean;
Count<T>(): number;
Count<T>(predicate: (x: T) => boolean): number;
Distinct<number>(): Array<number>;
Distinct<string>(): Array<string>;
Distinct<T>(equalityComparer: IEqualityComparer<T>): Array<T>;
ElementAt<T>(index: number): T;
First<T>(): T;
First<T>(predicate: (x: T) => boolean): T;
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Average = require('./src/Average');
const Concat = require('./src/Concat');
const Contains = require('./src/Contains');
const Count = require('./src/Count');
const Distinct = require('./src/Distinct');
const ElementAt = require('./src/ElementAt');
const Empty = require('./src/Empty');
const First = require('./src/First');
Expand Down Expand Up @@ -42,6 +43,7 @@ const bindAll = function() {
Array.prototype.Concat = Concat;
Array.prototype.Contains = Contains;
Array.prototype.Count = Count;
Array.prototype.Distinct = Distinct;
Array.prototype.ElementAt = ElementAt;
Array.Empty = Empty;
Array.prototype.First = First;
Expand Down
24 changes: 24 additions & 0 deletions src/Distinct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const _defaultEqualityComparer = require('./.internal/_defaultEqualityComparer');

// Distinct<TSource>(IEnumerable<TSource>)
// Distinct<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>)

/**
* @template T
* @returns {Array<T>}
*/

/**
* @template T
* @param {(a: T, b: T) => boolean} equalityComparer
* @returns {Array<T>}
* @this {Array<T>}
*/
function Distinct(equalityComparer = _defaultEqualityComparer) {
return this.filter((val, idx, arr) => {
const foundIdx = this.findIndex(v => equalityComparer(v, val));
return foundIdx === idx;
});
}

module.exports = Distinct;
42 changes: 42 additions & 0 deletions test/Distinct.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const expect = require('chai').expect;
require('..')();

describe('Array#prototype#Distinct', function() {
it('Should return distinct elements for an array of numbers', function() {
const input = [1, 2, 3, 3, 3, 5, 5];
const expected = [1, 2, 3, 5];
const actual = input.Distinct();
expect(actual).to.eql(expected);
});

it('Should return distinct elements for an array of strings', function() {
const input = ['hello', 'hello', 'world', '!'];
const expected = ['hello', 'world', '!'];
const actual = input.Distinct();
expect(actual).to.eql(expected);
});

it('Should return distinct elements for an array of objects when comparing their references', function() {
const obj1 = { _id: 2, name: 'Bernie' };
const obj2 = { _id: 3, name: 'Susan' };
const input = [obj1, obj2, obj1];
const expected = [obj1, obj2];

const actual = input.Distinct();

expect(actual).to.eql(expected);
});

it('Should return distinct elements for an array of objects when comparing their _id values', function() {
const input = [
{ _id: 2, name: 'Bernie' },
{ _id: 3, name: 'Susan' },
{ _id: 2, name: 'Terry' }
];
const expected = [{ _id: 2, name: 'Bernie' }, { _id: 3, name: 'Susan' }];

const actual = input.Distinct(({ _id: id1 }, { _id: id2 }) => id1 === id2);

expect(actual).to.eql(expected);
});
});

0 comments on commit 124db28

Please sign in to comment.