Skip to content

Instantly share code, notes, and snippets.

@asvae
Created May 25, 2017 10:30
Show Gist options
  • Save asvae/1d3b62f2708bba2249bbdd8970bafef3 to your computer and use it in GitHub Desktop.
Save asvae/1d3b62f2708bba2249bbdd8970bafef3 to your computer and use it in GitHub Desktop.
array delete function with tests
/**
* 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