Skip to content

micro-js/reduce-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

reduce-filter

Build status Git tag NPM version Code style

Generates a reducer that filters its accumulator by reducing each of its items to a boolean value. That is somewhat complicated to state, but easy to read as code:

function reduceFilter (fn) {
  return function (state, value) {
    return filter(predicate, state)

    function predicate (item, key) {
      return fn(item, value, key)
    }
  }
}

Installation

$ npm install @f/reduce-filter

Usage

var reduceFilter = require('@f/reduce-filter')

reduceFilter(isDivisbleBy)([1, 2, 3, 4, 5], 2) // -> [2, 4]

function isDivisbleBy (m, n) {
  return m % n === 0
}

API

reduceFilter(fn)

  • fn - Accepts (item, value, key) and returns a bool, indicating whether or not to retain item.

Returns: Returns a reducing function that accepts (state, value) and returns a new container of type state filtered by fn.

License

MIT