Skip to content

dylang/reduction

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

reduction

_.reduce with syntactic sugar

Example

var reduction = require('reduction');

// pass in a method name to be called on the accumulator
var concatted = reduction([['foo', 'bar'], ['baz'], ['asdf']], 'concat');
// => ['foo', 'bar', 'baz', 'asdf']

// pass an operator name to be used as the reducing function
var sum = reduction([1, 2, 3], '+');
// => 6

// works just like lodash woah
var mapped = reduction({ a: 1, b: 2, c: 3 }, function(result, num, key) {
  result[key] = num * 3;
  return result;
}, {});
// → { 'a': 3, 'b': 6, 'c': 9 }

Functions just like lodash's reduce function, but now you can pass a method name or an operator. If you pass a method name, that method will be called on the accumulator. If you pass an operator, the equivalent method of that operator will be used as the reducing method.

If you have a method with the same name as an operator, you're gonna have a bad time. This decision was made for convenience in the 99% of cases when your object doesn't have a callable property with the same name as an operator. If this turns out to be a big deal for people, I'll revisit it.

Contributing

This module was made because of a rejected PR. PRs welcome.

About

_.reduce with syntactic sugar

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published