diff --git a/README.md b/README.md index 1c159ec..f9fc343 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/as-proto/array-uniquify.spec.ts b/src/as-proto/array-uniquify.spec.ts new file mode 100644 index 0000000..71188e3 --- /dev/null +++ b/src/as-proto/array-uniquify.spec.ts @@ -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)); + }); +}); diff --git a/src/as-proto/array-uniquify.ts b/src/as-proto/array-uniquify.ts new file mode 100644 index 0000000..4d90c46 --- /dev/null +++ b/src/as-proto/array-uniquify.ts @@ -0,0 +1,28 @@ +import { uniquify } from "../uniquify"; + +declare global { + export interface Array { + /** + * #### 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 = function(this: T[]): T[] { + return uniquify(this); +} diff --git a/src/index.ts b/src/index.ts index 8a35354..88e9c64 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,3 +13,4 @@ export * from "./promise-timeout"; export * from "./replace"; export { revertPromise } from "./revert-promise"; export * from "./unique-id"; +export * from "./uniquify"; diff --git a/src/uniquify.spec.ts b/src/uniquify.spec.ts new file mode 100644 index 0000000..4229a6c --- /dev/null +++ b/src/uniquify.spec.ts @@ -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]); + }); +}); diff --git a/src/uniquify.ts b/src/uniquify.ts new file mode 100644 index 0000000..2807dce --- /dev/null +++ b/src/uniquify.ts @@ -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(array: T[]): T[] { + return [...new Set(array)]; +}