Revision fc0627cda4f3fafad3b8b212ce7912c53771e78e authored by Irakli Gozalishvili on 22 January 2013, 17:02:00 UTC, committed by Irakli Gozalishvili on 22 January 2013, 17:02:00 UTC
1 parent bac1edf
Raw File
drop.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 drop(source, n) {
  /**
  Returns sequence of all `source`'s items after `n`-th one. If source contains
  less then `n` items empty sequence is returned.

  ## Example

  print(drop([ 1, 2, 3, 4 ], 2))  // => <stream 3 4 />
  print(drop([ 1, 2, 3 ], 5))     // => <stream />
  **/

  // If drop `<= 0` then optimize by returning source itself. If `Infinity`
  // return empty.
  if (n <= 0) return source
  if (n === Infinity) return void(0)
  return reducible(function reduceDrop(next, initial) {
    var count = n
    reduce(source, function reduceDropSource(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 count of items has reached `0` just keep on passing values.
      if (count === 0) return next(value, result)
      // Otherwise just decrement count and return `result`.
      count = count - 1
      return result
    }, initial)
  })
}

module.exports = drop
back to top