Skip to content

Commit 1c61438

Browse files
committed
Added remove-all module
1 parent d09acf4 commit 1c61438

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ const reduce = require('./src/reduce')
141141
const reduceRight = require('./src/reduce-right')
142142
const reject = require('./src/reject')
143143
const remove = require('./src/remove')
144+
const removeAll = require('./src/remove-all')
144145
const removeRight = require('./src/remove-right')
145146
const replace = require('./src/replace')
146147
const replaceAll = require('./src/replace-all')
@@ -338,6 +339,7 @@ module.exports = {
338339
reduceRight,
339340
reject,
340341
remove,
342+
removeAll,
341343
removeRight,
342344
replace,
343345
replaceAll,

src/remove-all.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict'
2+
const and = require('./and')
3+
const append = require('./append')
4+
const curry = require('./curry')
5+
const drop = require('./drop')
6+
const empty = require('./empty')
7+
const eq = require('./eq')
8+
const head = require('./head')
9+
const isEmpty = require('./is-empty')
10+
const len = require('./len')
11+
const notArr = require('./not-arr')
12+
const notStr = require('./not-str')
13+
const tail = require('./tail')
14+
const take = require('./take')
15+
16+
function _removeAllArr(x, xs, acc) {
17+
if (isEmpty(xs))
18+
return acc
19+
20+
if (eq(x, head(xs)))
21+
return _removeAllArr(x, tail(xs), acc)
22+
23+
return _removeAllArr(x, tail(xs), append(head(xs), acc))
24+
}
25+
26+
function _removeAllStr(x, xs, acc) {
27+
if (isEmpty(xs))
28+
return acc
29+
30+
if (eq(take(len(x), xs), x))
31+
return _removeAllStr(x, drop(len(x), xs), acc)
32+
33+
return _removeAllStr(x, tail(xs), append(head(xs), acc))
34+
}
35+
36+
function removeAll(x, xs) {
37+
if (and(notArr(xs), notStr(xs)))
38+
throw new TypeError('[remove] Last argument must be an array or a string')
39+
40+
if (notArr(xs))
41+
return _removeAllStr(x, xs, empty(xs))
42+
43+
return _removeAllArr(x, xs, empty(xs))
44+
}
45+
46+
module.exports = curry(removeAll)

0 commit comments

Comments
 (0)