Skip to content

Commit ce57c7c

Browse files
committed
✨ remove
1 parent b8106b8 commit ce57c7c

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/array/__tests__/remove.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { remove } from '..'
2+
3+
test('remove', () => {
4+
let arr = [1, 2, 3, 4]
5+
const fn = n => n % 2 === 0
6+
const removed = remove(arr, fn)
7+
expect(arr).toEqual([1, 3])
8+
expect(removed).toEqual([2, 4])
9+
})
10+
11+
test('remove', () => {
12+
let arr = [1, 2, 3, 4, 4, 1]
13+
const fn = n => n === 4
14+
const removed = remove(arr, fn)
15+
expect(arr).toEqual([1, 2, 3, 1])
16+
expect(removed).toEqual([4, 4])
17+
})
18+
19+
test('remove', () => {
20+
let arr = [1, 2, 3, 4]
21+
const fn = n => n > 0
22+
const removed = remove(arr, fn)
23+
expect(arr).toEqual([])
24+
expect(removed).toEqual([1, 2, 3, 4])
25+
})
26+
27+
test('remove', () => {
28+
let arr = [1, 2, 3, 4]
29+
const fn = n => n < 2
30+
const removed = remove(arr, fn)
31+
expect(arr).toEqual([2, 3, 4])
32+
expect(removed).toEqual([1])
33+
})

src/array/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ export * from './concat'
2626
export * from './flattenDepth'
2727
export * from './fromPairs'
2828
export * from './pullAt'
29+
export * from './remove'

src/array/remove.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const remove = (arr, fn) => {
2+
const removed = []
3+
let i = 0
4+
5+
while (i < arr.length) {
6+
if (fn(arr[i])) removed.push(...arr.splice(i, 1))
7+
else i++
8+
}
9+
10+
return removed
11+
}

0 commit comments

Comments
 (0)