0.0.2 • Published 6 years ago

xstream-reduce v0.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

xstream-reduce

Reduces an entire stream into a single value.

Takes a function which applies the every value to the accumulating result and returns the new result. Returns a promise for the result that will be fulfilled when the stream ends, or rejected if the stream errors.

Note that this function will start consuming the stream, and the resulting value will not be a stream.

Conceptually similar to the reduce method on arrays.

Lifted from the most.js reduce operator.

Example

import reduce from "xstream-reduce"

// ...

const goalsByTeam = goalsStream.compose(
  reduce((accumulator, goal) => {
    if (!accumulator[goal.team]) accumulator[goal.team] = []
    accumulator[goal.team].push(goal)
    return accumulator
  }, {})
)

goalsByTeam.then(obj =>
  // Will be called when match ends
  console.log(`The Red team scored ${obj["Red"].length} goals. You go, Reds!`)
)