-
Notifications
You must be signed in to change notification settings - Fork 8
/
drop-while.js
39 lines (32 loc) · 1.24 KB
/
drop-while.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
33
34
35
36
37
38
39
"use strict";
var reducible = require("reducible/reducible")
var reduce = require("reducible/reduce")
var isError = require("reducible/is-error")
var end = require("reducible/end")
function dropWhile(source, predicate) {
/**
Returns a sequence of the items in `source` starting from the first
item for which `predicate(item)` returns `false`.
## Example
var numbers = dropWhile([ 2, 7, 10, 23 ], function(value) {
return value < 10
})
print(numbers) // => < 10 23 >
**/
return reducible(function reduceDropWhile(next, initial) {
var dropped = false
reduce(source, function reduceDropWhileSource(value, result) {
// If value is end of collection or is an error (which also includes
// end of collection) just pass it through, `reducible` will take care
// of everything.
if (value === end) return next(value, result)
if (isError(value)) return next(value, result)
// If already dropped all the intended items (if `dropped` is already
// being set to `true` or if current predicate returns `false`). Then
// just keep on passing values.
if (dropped || (dropped = !predicate(value))) return next(value, result)
return result
}, initial)
})
}
module.exports = dropWhile