|
| 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