Skip to content

Commit

Permalink
Merge branch 'array-remove'
Browse files Browse the repository at this point in the history
  • Loading branch information
alisahinozcelik committed Oct 23, 2019
2 parents 94e6118 + a68f477 commit 9bb220e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Javascript utility functions for web development
* [replace](https://thalesrc.github.io/js-utils/modules/_replace_.html)
* [revert-promise](https://thalesrc.github.io/js-utils/modules/_revert_promise_.html)
* [unique-id](https://thalesrc.github.io/js-utils/modules/_unique_id_.html)
* [uniquify](https://thalesrc.github.io/js-utils/modules/_uniquify_.html)

#### Utility Classes
* [open-promise](https://thalesrc.github.io/js-utils/modules/_open_promise_.html)
Expand Down
15 changes: 15 additions & 0 deletions src/as-proto/array-uniquify.spec.ts
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));
});
});
28 changes: 28 additions & 0 deletions src/as-proto/array-uniquify.ts
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);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from "./remove";
export * from "./replace";
export { revertPromise } from "./revert-promise";
export * from "./unique-id";
export * from "./uniquify";
14 changes: 14 additions & 0 deletions src/uniquify.spec.ts
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]);
});
});
20 changes: 20 additions & 0 deletions src/uniquify.ts
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)];
}

0 comments on commit 9bb220e

Please sign in to comment.