3.1.0 • Published 8 years ago

universal-reduce v3.1.0

Weekly downloads
137
License
ISC
Repository
github
Last release
8 years ago

Universal Reduce

Reduce over anything vaguely collection like. Objects, Arrays, Maps, Sets, Generators, iterables of any kind. Supports early reduction.

Works in very old JS environments, and does its best to get the same results.

var universalReduce = require('universal-reduce')

API

universalReduce(anything, fn, initial)

Similar to Array.prototype.reduce, except that it can be used on anything.

fn(accumulator, value, key) -> accumulator. fn is called for each item in anything, receiving the previous result of calling fn, the value of the item, and a reasonable key value. The first call is seeded with initial if provided.

For non-Map iterables 'key' is the string representation of the integer index based on the order it has been iterated to. This gives the most consistent results, Arrays and arguments objects behave the same if they are being iterated as objects or iterables.

When iterating over an Object, only looks at own properties.

universalReduce.reduced(value)

When you've arrived at the final value of your reduction, you can short circuit to avoid iterating over items you don't need to. Very important if you are searching through a large, or even potentially infinite list for example. From inside the reducer, return universalReduce.reduced(value) at any time to cut the reduction short, and return value as the final result.

3.1.0

8 years ago

3.0.0

8 years ago

2.0.0

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago