-
-
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
Showing
6 changed files
with
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { expect } from 'chai'; | ||
import 'mocha'; | ||
|
||
import "./array-uniquify"; | ||
import { uniquify } from "../uniquify"; | ||
|
||
describe('Array Uniquify Proto Function', () => { | ||
it('should work as same', () => { | ||
const foo = [1, 2, 3, 1, 2, 3]; | ||
const bar = [1, 2]; | ||
|
||
expect(foo.uniquify()).to.eql(uniquify(foo)); | ||
expect(bar.uniquify()).to.eql(uniquify(bar)); | ||
}); | ||
}); |
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,28 @@ | ||
import { uniquify } from "../uniquify"; | ||
|
||
declare global { | ||
export interface Array<T> { | ||
/** | ||
* #### Uniquify | ||
* | ||
* Removes repeated items from the array | ||
* | ||
* * * * | ||
* Example: | ||
* ```typescript | ||
* import "@thalesrc/js-utils/dist/as-proto/array-uniquify"; | ||
* | ||
* const array = ["a", "b", "c", "a", "b", "c"]; | ||
* | ||
* array.uniquify(); // ["a", "b", "c"] | ||
* ``` | ||
* * * * | ||
* @return The new uniquified array | ||
*/ | ||
uniquify(): T[]; | ||
} | ||
} | ||
|
||
Array.prototype.uniquify = <any>function<T>(this: T[]): T[] { | ||
return uniquify(this); | ||
} |
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,14 @@ | ||
import { expect } from 'chai'; | ||
import 'mocha'; | ||
|
||
import { uniquify } from "./uniquify"; | ||
|
||
describe('Uniquify Function', () => { | ||
it('should remove repeated items from array', () => { | ||
const a1 = [1, 2, 3]; | ||
const a2 = [1, 1, 2, 2, 3, 3]; | ||
|
||
expect(uniquify(a1)).to.eql([1,2,3]); | ||
expect(uniquify(a2)).to.eql([1,2,3]); | ||
}); | ||
}); |
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,20 @@ | ||
/** | ||
* #### Uniquify | ||
* | ||
* Removes repeated items from the array | ||
* | ||
* * * * | ||
* Example: | ||
* ```typescript | ||
* import { uniquify } "@thalesrc/js-utils"; | ||
* | ||
* const array = ["a", "b", "c", "a", "b", "c"]; | ||
* | ||
* uniquify(array); // ["a", "b", "c"] | ||
* ``` | ||
* * * * | ||
* @return The new uniquified array | ||
*/ | ||
export function uniquify<T>(array: T[]): T[] { | ||
return [...new Set(array)]; | ||
} |