https://github.com/Gozala/reducers
Raw File
Tip revision: fc0627cda4f3fafad3b8b212ce7912c53771e78e authored by Irakli Gozalishvili on 22 January 2013, 17:02:00 UTC
Add IE10 into test matrix.
Tip revision: fc0627c
drop-while.js
"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
back to top