map-filter
When you need to run map
and filter
together (aka elegant reduce).
Why
I've seen code that looks like:
let list =name: 'alex' age: 28name: 'frank' age: 28name: 'joe' age: 30name: 'ayham' age: 28name: 'solomon' age: 29list // youngsters!
Whereas you can achieve much better performance with a simple
reduce
:
list
No trust? Check yourself:
~/projects/js-map-filter (master ✔) ᐅ node bench.js 1s
filter + map: 19.378ms
reduce: 6.254ms
map-filter module: 7.116ms
So what does this guy do?
This utility lets you do the same things you'd do with a reduce, without having to keep track of the accumulator:
const mapFilter =
Just return undefined
(aka don't return) for the values you
want to exclude from the final list.
Tests
Just mocha
it:
~/projects/js-map-filter (master ✘)✹✭ ᐅ mocha 1s
mapFilter
✓ should be able to return all original values if no fn is specified
✓ should be able to exclude values
✓ should be able to map values
✓ should be able to handle falsey values
4 passing (9ms)