fp-and-or
Simple and
and or
functional programming predicates.
and(...fs): (...args): boolean
- Returns a predicate that returns true if all arguments are true or evaluate to true for the given input.or(...fs): (...args): boolean
- Returns a predicate that returns true if at least one argument is true or evaluates to true for the given input.
A predicate is a function that returns a boolean
, commonly used in Array.prototype.filter
.
e.g.
const isEven = n => n%2 === 0
const isPositive = n => n > 0
items.filter(x => isEven(x) || isPositive(x))
items.filter(or(isEven, isPositive))
Install
npm install --save fp-and-or
Usage
const { and, or } = require('fp-and-or')
const isEven = n => n % 2 === 0
const isPositive = n => n > 0
const isBig = n => Math.abs(n) >= 1000
and
return true with no predicates
and()(1)
return result of single predicate
and(isEven)(1)
and(isEven)(2)
return logical AND of two predicates
and(isEven, isPositive)(-2)
and(isEven, isPositive)(-1)
and(isEven, isPositive)(1)
and(isEven, isPositive)(2)
return logical AND of three predicates
and(isEven, isPositive, isBig)(-2)
and(isEven, isPositive, isBig)(-1)
and(isEven, isPositive, isBig)(1)
and(isEven, isPositive, isBig)(2)
and(isEven, isPositive, isBig)(-1002)
and(isEven, isPositive, isBig)(-1001)
and(isEven, isPositive, isBig)(1001)
and(isEven, isPositive, isBig)(1002)
return value of single boolean
and(true)(1)
and(false)(1)
return value of two booleans
and(false, false)(1)
and(false, true)(1)
and(true, false)(1)
and(true, true)(1)
return value of multiple booleans
and(false, false, false)(1)
and(false, true, false)(1)
and(true, false, false)(1)
and(true, true, false)(1)
and(false, false, true)(1)
and(false, true, true)(1)
and(true, false, true)(1)
and(true, true, true)(1)
return value of mixed booleans and functions
and(false, isEven)(1)
and(false, isEven)(2)
and(true, isEven)(1)
and(true, isEven)(2)
and(isEven, false)(1)
and(isEven, false)(2)
and(isEven, true)(1)
and(isEven, true)(2)
or
return false with no predicates
or()(1)
return result of single predicate
or(isEven)(1)
or(isEven)(2)
return logical OR of two predicates
or(isEven, isPositive)(-2)
or(isEven, isPositive)(-1)
or(isEven, isPositive)(1)
or(isEven, isPositive)(2)
return logical OR of three predicates
or(isEven, isPositive, isBig)(-2)
or(isEven, isPositive, isBig)(-1)
or(isEven, isPositive, isBig)(0)
or(isEven, isPositive, isBig)(1)
or(isEven, isPositive, isBig)(2)
or(isEven, isPositive, isBig)(-1002)
or(isEven, isPositive, isBig)(-1001)
or(isEven, isPositive, isBig)(1001)
or(isEven, isPositive, isBig)(1002)
return value of single boolean
or(true)(1)
or(false)(1)
return value of two booleans
or(false, false)(1)
or(false, true)(1)
or(true, false)(1)
or(true, true)(1)
return value of multiple booleans
or(false, false, false)(1)
or(false, true, false)(1)
or(true, false, false)(1)
or(true, true, false)(1)
or(false, false, true)(1)
or(false, true, true)(1)
or(true, false, true)(1)
or(true, true, true)(1)
return value of mixed booleans and functions
or(false, isEven)(1)
or(false, isEven)(2)
or(true, isEven)(1)
or(true, isEven)(2)
or(isEven, false)(1)
or(isEven, false)(2)
or(isEven, true)(1)
or(isEven, true)(2)