Created
May 25, 2017 10:30
-
-
Save asvae/1d3b62f2708bba2249bbdd8970bafef3 to your computer and use it in GitHub Desktop.
array delete function with tests
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
/** | |
* Remove item from array via identity check. | |
* | |
* @param array | |
* @param item | |
* @returns {[*]} | |
*/ | |
static remove (array: Array, item) { | |
const result = [...array] | |
const foundItemIndex = array.indexOf(item) | |
if (foundItemIndex !== -1) { | |
result.splice(foundItemIndex, 1) | |
} | |
return result | |
} | |
// Tests | |
it('removes element in array', () => { | |
const object1 = { id: 1 } | |
const object2 = { id: 2 } | |
const array = [object1, object2] | |
expect(ArrayHelpers.remove(array, object1).length).toBe(1) | |
expect(ArrayHelpers.remove(array, { id: 1 }).length).toBe(2) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment