Skip to content

Commit

Permalink
feat(Array): created uniquifyByKey
Browse files Browse the repository at this point in the history
  • Loading branch information
alisahinozcelik committed Aug 28, 2020
1 parent 8c9cc7e commit 3d5cfdf
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ const array = ["a", "b", "c", "a", "b", "c"];
uniquify(array); // ["a", "b", "c"]
```
#### [Uniquify By Key](https://thalesrc.github.io/js-utils/modules/_array_uniquify_by_key_.html)
Removes objects from the array which the value of its specifed key included before by another
```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}]
```
### Function
#### [Debounce](https://thalesrc.github.io/js-utils/modules/_function_debounce_.html)
Expand Down
1 change: 1 addition & 0 deletions src/array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './limit';
export * from './remove';
export * from './replace';
export * from './uniquify';
export * from './uniquify-by-key';
18 changes: 18 additions & 0 deletions src/array/proto/uniquify-by-key.spec.ts
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]);
});
});
29 changes: 29 additions & 0 deletions src/array/proto/uniquify-by-key.ts
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);
};
17 changes: 17 additions & 0 deletions src/array/uniquify-by-key.spec.ts
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);
});
});
41 changes: 41 additions & 0 deletions src/array/uniquify-by-key.ts
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;
});
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { ReplaceByMapOptions, ReplaceItemsOptions, TInclusion, TSubstraction, asyncMap, difference, findByKey, intersection, remove, replace, uniquify } from './array';
export { ReplaceByMapOptions, ReplaceItemsOptions, TInclusion, TSubstraction, asyncMap, difference, findByKey, intersection, remove, replace, uniquify, uniquifyByKey } from './array';
export * from './function';
export * from './map';
export * from './math';
Expand Down

0 comments on commit 3d5cfdf

Please sign in to comment.