Skip to content

Commit

Permalink
feat(Remove): Array remove function developed
Browse files Browse the repository at this point in the history
also dev packages updated

Closes card-20235569
  • Loading branch information
alisahinozcelik committed Sep 10, 2019
1 parent 9eb5a82 commit 91bec58
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 6 deletions.
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"math",
"min max",
"noop",
"unique id"
"unique id",
"replace",
"remove"
],
"repository": {
"type": "git",
Expand All @@ -29,7 +31,7 @@
"scripts": {
"document": "typedoc --out docs --excludeNotExported --excludePrivate --disableOutputCheck --gitRevision master --exclude \"**/+(as-proto|as-static)/**/*ts\"",
"prepare-github-pages": "npm run document && echo \"\" > ./docs/.nojekyll",
"test": "nyc mocha --recursive --compilers ts:ts-node/register src/**/*.spec.ts",
"test": "nyc mocha --recursive --require ts-node/register src/**/*.spec.ts",
"cover": "nyc report --reporter=text-lcov | codecov",
"publish:config-npm": "replace '<EMAIL>' $NPM_EMAIL .npmrc && replace '<TOKEN>' $NPM_TOKEN .npmrc",
"publish:copy-files": "cpy 'LICENSE' 'README.md' '.npmrc' '.npmignore' 'package.json' dist",
Expand All @@ -50,12 +52,12 @@
"chai": "^4.2.0",
"codecov.io": "^0.1.6",
"cpy-cli": "^2.0.0",
"mocha": "^5.2.0",
"nyc": "^13.1.0",
"mocha": "^6.2.0",
"nyc": "^14.1.1",
"replace": "^1.0.0",
"ts-node": "^7.0.1",
"ts-node": "^8.3.0",
"typedoc": "^0.13.0",
"typescript": "^3.1.3",
"typescript": "^3.6.2",
"update-json": "^1.0.0"
},
"dependencies": {
Expand Down
15 changes: 15 additions & 0 deletions src/as-proto/array-remove.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from 'chai';
import 'mocha';

import "./array-remove";
import { remove } from "../remove";

describe('Array Remove Proto Function', () => {
it('should work as same', () => {
const foo = [1, 2, 3, 1, 2, 3];

expect(foo.remove(2)).to.eql(remove(foo, 2));
expect(foo.remove(4)).to.eql(remove(foo, 4));
expect(foo.remove(3, true)).to.eql(remove(foo, 3, true));
});
});
31 changes: 31 additions & 0 deletions src/as-proto/array-remove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { remove } from "../remove";

declare global {
export interface Array<T> {
/**
* #### Remove
*
* Removes an item from the array
*
* Removes all item references if multi is set to `true`
*
* * * *
* Example:
* ```typescript
* import "@thalesrc/js-utils/dist/as-proto/array-remove";
*
* const array = ["a", "b", "c", "a", "b", "c"];
*
* array.remove("b", true); // ["a", "c", "a", "c"]
* ```
* * * *
* @param itemToRemove Item to remove
* @return New array
*/
remove(itemToRemove: T, multi?: boolean): T[];
}
}

Array.prototype.remove = <any>function<T>(this: T[], itemToRemove: T, multi: boolean = false): T[] {
return remove.call(null, this, ...arguments);
}
24 changes: 24 additions & 0 deletions src/remove.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from 'chai';
import 'mocha';

import { remove } from "./remove";

describe('Remove Function', () => {
let foo: number[];

beforeEach(() => {
foo = [1, 2, 3, 1, 2, 3];
});

it('should remove one item successfully', () => {
expect(remove(foo, 2)).to.eql([1, 3, 1, 2, 3]);
});

it('shouldn\'t remove nonfound item', () => {
expect(remove(foo, 4)).to.eql([1, 2, 3, 1, 2, 3]);
});

it('should remove multiply', () => {
expect(remove(foo, 3, true)).to.eql([1, 2, 1, 2]);
});
});
36 changes: 36 additions & 0 deletions src/remove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* #### Remove
*
* Removes an item from the array
*
* Removes all item references if multi is set to `true`
*
* * * *
* Example:
* ```typescript
* import { remove } from "@thalesrc/js-utils";
*
* const array = ["a", "b", "c", "a", "b", "c"];
*
* remove(array, "b"); // ["a", "c", "a", "b", "c"]
* remove(array, "b", true); // ["a", "c", "a", "c"]
* ```
* * * *
* @param array: Array to remove the item
* @param itemToRemove Item to remove
* @return New array
*/
export function remove<T = any>(array: T[], item: T, multi = false): T[] {
let index: number = array.indexOf(item);

if (index < 0) {
return [...array];
}

do {
array = [...array.slice(0, index), ...array.slice(index + 1)];
index = array.indexOf(item);
} while (multi && index > -1)

return array;
}

0 comments on commit 91bec58

Please sign in to comment.