1.0.0 • Published 6 years ago

hypercore-reduce v1.0.0

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

hypercore-reduce

Build Status

Reduce from an initial state as a function of a hypercore

Usage

var reduce = require('hypercore-reduce')

var initial = {}
// Feed is a hypercore instance
reduce(feed, reducer, initial, done)

function reducer (prevState, curState, next) {
  return next(null, Object.assign(prevState, curState))
}

function done (err, finalState) {
  if (err) throw err

  console.log(finalState)
}

API

var destroy = reduce(feed, [readOpts], reducer, initial, done)

Reduce a hypercore feed from an initial state initial, calling reducer(state, currentItem, callback), where callback(err, state) will update the state. When the feed is completely consumed, done(err, state) will be invoked with the final state. If an error occurs, done is also invoked. Optionally readOpts can be passed as argument to feed.createReadStream.

A destroy(err) function is returned, which can be used to destroy the underlying stream.

Install

npm install hypercore-reduce

License

ISC