-
-
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.
feat(Remove): Array remove function developed
also dev packages updated Closes card-20235569
- Loading branch information
1 parent
9eb5a82
commit 91bec58
Showing
5 changed files
with
114 additions
and
6 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-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)); | ||
}); | ||
}); |
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,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); | ||
} |
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,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]); | ||
}); | ||
}); |
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,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; | ||
} |