Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 53137c5

Browse files
Stanisław JarosKent C. Dodds
Stanisław Jaros
authored and
Kent C. Dodds
committed
feat(removeElementByIndex): add removeElementByIndex function (#233)
Closes #232
1 parent 42d1c23 commit 53137c5

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

Diff for: src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import timeSince from './timeSince'
8888
import toPower from './to-power'
8989
import truncate from './truncate'
9090
import validateEmail from './validateEmail'
91+
import removeElementByIndex from './removeElementByIndex'
9192

9293
export {
9394
reverseArrayInPlace,
@@ -180,4 +181,5 @@ export {
180181
numberToString,
181182
largest,
182183
hex2hsl,
184+
removeElementByIndex,
183185
}

Diff for: src/removeElementByIndex.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default removeElementByIndex
2+
3+
/**
4+
* Original source: https://stackoverflow.com/a/5767335/10309455
5+
* This method removes the element from an array by index and returns edited array
6+
* @param {Array} array - base array
7+
* @param {Number} index - index to delete
8+
* @returns {Array} - array without deleted item
9+
*/
10+
11+
function removeElementByIndex(array, index) {
12+
const updatedArray = [...array]
13+
updatedArray.splice(index, 1)
14+
return updatedArray
15+
}

Diff for: test/removeElementByIndex.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava'
2+
import {removeElementByIndex} from '../src'
3+
4+
test('Removes index from an array', t => {
5+
const array = [1, 2, 3]
6+
const expected = [1, 3]
7+
const actual = removeElementByIndex(array, 1)
8+
9+
t.deepEqual(actual, expected)
10+
})

0 commit comments

Comments
 (0)