fp-and-or
v1.0.2
Published
Simple `and` and `or` functional programming predicates
Downloads
998,149
Readme
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
// un-fancy
items.filter(x => isEven(x) || isPositive(x))
// fancy
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) // true
return result of single predicate
and(isEven)(1) // false
and(isEven)(2) // true
return logical AND of two predicates
and(isEven, isPositive)(-2) // false
and(isEven, isPositive)(-1) // false
and(isEven, isPositive)(1) // false
and(isEven, isPositive)(2) // true
return logical AND of three predicates
and(isEven, isPositive, isBig)(-2) // false
and(isEven, isPositive, isBig)(-1) // false
and(isEven, isPositive, isBig)(1) // false
and(isEven, isPositive, isBig)(2) // false
and(isEven, isPositive, isBig)(-1002) // false
and(isEven, isPositive, isBig)(-1001) // false
and(isEven, isPositive, isBig)(1001) // false
and(isEven, isPositive, isBig)(1002) // true
return value of single boolean
and(true)(1) // true
and(false)(1) // false
return value of two booleans
and(false, false)(1) // false
and(false, true)(1) // false
and(true, false)(1) // false
and(true, true)(1) // true
return value of multiple booleans
and(false, false, false)(1) // false
and(false, true, false)(1) // false
and(true, false, false)(1) // false
and(true, true, false)(1) // false
and(false, false, true)(1) // false
and(false, true, true)(1) // false
and(true, false, true)(1) // false
and(true, true, true)(1) // true
return value of mixed booleans and functions
and(false, isEven)(1) // false
and(false, isEven)(2) // false
and(true, isEven)(1) // false
and(true, isEven)(2) // true
and(isEven, false)(1) // false
and(isEven, false)(2) // false
and(isEven, true)(1) // false
and(isEven, true)(2) // true
or
return false with no predicates
or()(1) // false
return result of single predicate
or(isEven)(1) // false
or(isEven)(2) // true
return logical OR of two predicates
or(isEven, isPositive)(-2) // true
or(isEven, isPositive)(-1) // false
or(isEven, isPositive)(1) // true
or(isEven, isPositive)(2) // true
return logical OR of three predicates
or(isEven, isPositive, isBig)(-2) // true
or(isEven, isPositive, isBig)(-1) // false
or(isEven, isPositive, isBig)(0) // true
or(isEven, isPositive, isBig)(1) // true
or(isEven, isPositive, isBig)(2) // true
or(isEven, isPositive, isBig)(-1002) // true
or(isEven, isPositive, isBig)(-1001) // true
or(isEven, isPositive, isBig)(1001) // true
or(isEven, isPositive, isBig)(1002) // true
return value of single boolean
or(true)(1) // true
or(false)(1) // false
return value of two booleans
or(false, false)(1) // false
or(false, true)(1) // true
or(true, false)(1) // true
or(true, true)(1) // true
return value of multiple booleans
or(false, false, false)(1) // false
or(false, true, false)(1) // true
or(true, false, false)(1) // true
or(true, true, false)(1) // true
or(false, false, true)(1) // true
or(false, true, true)(1) // true
or(true, false, true)(1) // true
or(true, true, true)(1) // true
return value of mixed booleans and functions
or(false, isEven)(1) // false
or(false, isEven)(2) // true
or(true, isEven)(1) // true
or(true, isEven)(2) // true
or(isEven, false)(1) // false
or(isEven, false)(2) // true
or(isEven, true)(1) // true
or(isEven, true)(2) // true