Sha256: 851f68a08b0a532057cfccc51f1949d278df861800c3c602b3963525560c7542

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

"use strict";

var reducible = require("reducible/reducible")
var reduce = require("reducible/reduce")
var end = require("reducible/end")

function take(source, n) {
  /**
  Returns sequence of first `n` items of the given `source`. If `source`
  contains less items than `n` then that's how  much items return sequence
  will contain.

  ## Example

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

  // If take `0` then optimize by returning an empty if less then `0`
  // then just return `source` back.
  if (n === 0) return void(0)
  if (n < 0) return source
  return reducible(function reduceTake(next, initial) {
    // Capture `n` into count, since modifying `n` directly will have side
    // effects on subsequent calls.
    var count = n
    reduce(source, function reduceTakeSource(value, result) {
      count = count - 1
      result = next(value, result)

      // If we have not taken `n` items yet just pass result back. Otherwise
      // pass `end` of stream to a consumer. Note `reducible` will return
      // `reduced(result)` back signaling source it should stop.
      return count > 0 ? result :
             next(end)
    }, initial)
  })
}

module.exports = take

Version data entries

5 entries across 2 versions & 1 rubygems

Version Path
ruby-wisp-source-0.8.0 vendor/interactivate/node_modules/interactivate/node_modules/reducers/take.js
ruby-wisp-source-0.8.0 vendor/node_modules/wisp/interactivate/node_modules/interactivate/node_modules/reducers/take.js
ruby-wisp-source-0.7.0 vendor/interactivate/node_modules/interactivate/node_modules/reducers/take.js
ruby-wisp-source-0.7.0 vendor/node_modules/wisp/interactivate/node_modules/interactivate/node_modules/reducers/take.js
ruby-wisp-source-0.7.0 vendor/try/node_modules/~wisp/interactivate/node_modules/interactivate/node_modules/reducers/take.js