-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c9cc7e
commit 3d5cfdf
Showing
7 changed files
with
118 additions
and
1 deletion.
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 @@ | ||
import 'jest'; | ||
|
||
import './uniquify-by-key'; | ||
import { uniquifyByKey } from '../uniquify-by-key'; | ||
|
||
describe('Array Uniquify By Key Proto Function', () => { | ||
it('should work as same', () => { | ||
const foo = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}]; | ||
const bar = [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 1}]; | ||
|
||
const firstObj = {a: 1}; | ||
const baz = [firstObj, {a: 2}, {a: 3}, {a: 1}, {a: 1}]; | ||
|
||
expect(foo.uniquifyByKey('a')).toEqual(uniquifyByKey(foo, 'a')); | ||
expect(bar.uniquifyByKey('a')).toEqual(uniquifyByKey(bar, 'a')); | ||
expect(baz.uniquifyByKey('a')[0]).toBe(uniquifyByKey(baz, 'a')[0]); | ||
}); | ||
}); |
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,29 @@ | ||
import { uniquifyByKey } from '../uniquify-by-key'; | ||
|
||
declare global { | ||
export interface Array<T> { | ||
/** | ||
* #### Uniquify By Key | ||
* | ||
* Removes objects from the array which the value of its specifed key included before by another | ||
* | ||
* * * * | ||
* Example: | ||
* ```typescript | ||
* import "@thalesrc/js-utils/array/proto/uniquify-by-key"; | ||
* | ||
* const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}]; | ||
* | ||
* array.uniquifyByKey('a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}] | ||
* ``` | ||
* * * * | ||
* @param key Key to search the value on | ||
* @return The new uniquified array | ||
*/ | ||
uniquifyByKey(key: keyof T): typeof uniquifyByKey; | ||
} | ||
} | ||
|
||
Array.prototype.uniquifyByKey = <any>function<T>(this: T[], key: keyof T): T[] { | ||
return uniquifyByKey(this, key); | ||
}; |
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,17 @@ | ||
import 'jest'; | ||
|
||
import { uniquifyByKey } from './uniquify-by-key'; | ||
|
||
describe('Uniquify By Key Function', () => { | ||
it('should remove repeated items from array', () => { | ||
const a1 = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}]; | ||
const a2 = [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 1}]; | ||
|
||
const firstObj = {a: 1}; | ||
const a3 = [firstObj, {a: 2}, {a: 3}, {a: 1}, {a: 1}]; | ||
|
||
expect(uniquifyByKey(a1, 'a')).toEqual([{a: 1}, {a: 2}, {a: 3}, {a: 4}]); | ||
expect(uniquifyByKey(a2, 'a')).toEqual([{a: 1}, {a: 2}, {a: 3}, {a: 4}]); | ||
expect(uniquifyByKey(a3, 'a')[0]).toBe(firstObj); | ||
}); | ||
}); |
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,41 @@ | ||
/** | ||
* #### Uniquify By Key | ||
* | ||
* Removes objects from the array which the value of its specifed key included before by another | ||
* | ||
* * * * | ||
* Example: | ||
* ```typescript | ||
* import { uniquifyByKey } "@thalesrc/js-utils/array"; | ||
* | ||
* const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}]; | ||
* | ||
* uniquifyByKey(array, 'a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}] | ||
* ``` | ||
* | ||
* Prototype Example: | ||
* ```typescript | ||
* import "@thalesrc/js-utils/array/proto/uniquify-by-key"; | ||
* | ||
* const array = [{a: 1}, {a: 1}, {a: 2}, {a: 3}, {a: 3}, {a: 4}]; | ||
* | ||
* array.uniquifyByKey('a'); // [{a: 1}, {a: 2}, {a: 3}, {a: 4}] | ||
* ``` | ||
* * * * | ||
* @param array Collection of the objects | ||
* @param key Key to search the value on | ||
* @return The new uniquified array | ||
*/ | ||
export function uniquifyByKey<T extends Object, P extends keyof T>(array: T[], key: P): T[] { | ||
const cache = new Set<T[P]>(); | ||
|
||
return array.filter(item => { | ||
if (cache.has(item[key])) { | ||
return false; | ||
} | ||
|
||
cache.add(item[key]); | ||
|
||
return true; | ||
}); | ||
} |
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