-
Notifications
You must be signed in to change notification settings - Fork 87
/
dropLastWhile.js
32 lines (27 loc) · 901 Bytes
/
dropLastWhile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import {_isArray} from './_internals/_isArray'
export function dropLastWhile(predicate, iterable) {
if (arguments.length === 1) {
return _iterable => dropLastWhile(predicate, _iterable)
}
if (iterable.length === 0) return iterable
const isArray = _isArray(iterable)
if (typeof predicate !== 'function') {
throw new Error(`'predicate' is from wrong type ${typeof predicate}`)
}
if (!isArray && typeof iterable !== 'string') {
throw new Error(`'iterable' is from wrong type ${typeof iterable}`)
}
let found = false
const toReturn = []
let counter = iterable.length
while (counter > 0) {
counter--
if (!found && predicate(iterable[counter]) === false) {
found = true
toReturn.push(iterable[counter])
} else if (found) {
toReturn.push(iterable[counter])
}
}
return isArray ? toReturn.reverse() : toReturn.reverse().join('')
}