fp-and-or
Simple `and` and `or` functional programming predicates
Last updated 4 years ago by raine .
ISC · Original npm · Tarball · package.json
$ npm install fp-and-or 
SYNC missed versions from official npm registry.

fp-and-or

npm version

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

Current Tags

  • 1.0.2                                ...           latest (a year ago)

7 Versions

  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
  • 0.1.4                                ...           a year ago
  • 0.1.3                                ...           4 years ago
  • 0.1.2                                ...           4 years ago
  • 0.1.1                                ...           4 years ago
Maintainers (1)
Downloads
Total 2
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (9)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE