-
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.
Closes #8
- Loading branch information
Showing
4 changed files
with
71 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,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; |
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,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); | ||
}); | ||
}); |