Fork me on GitHub

predicate.jsv. 1.0.0

Adding clarity and conciseness to your JS through predicates

predicate provides a slew of functions that allow you to compare values and test types. This leads to more functional, concise, legible, and correct code.

This is extremely useful when filtering data or comparing against a set of data.

  ['venus', 'neptune', 1, []].filter(predicate.str); // ['venus', 'neptune']
  [1.2, 3, 5].every(predicate.int) // false

is also offers autocurrying for predicates of arity 2

var isInSet = predicate.contains([1, 2, 3]);
isInSet(1); // true
isInSet(5); // false

var hasKey = predicate.has({ name: 'Trevor', twitter: '@trevor_landau' });
hasKey('name'); // true
hasKey('myspace'); // false

Make room on your utility belt next to underscore

  _.filter([1, 2, 3], predicate.less(1)); // [2, 3]
  _.partition([1, -2, -3, 4], predicate.pos); // [[1, 4], [-2, -3]]

The project is hosted on GitHub. You can report bugs and discuss features on the issues page or send tweets to @trevor_landau.


Install/Downloads

Node npm install --save is-predicate

Development predicate.js

Production predicate.min.js

Predicates


notpredicate.not

A complemented version of the predicate predicate functions. All predicate functions are available in this namespace.


Example

predicate.null(null) // true
predicate.not.null(null) // false

ispredicate.is(a, b)

Returns true if Object.is(a, b)

curryable

Example

predicate.is(window, window) // true
predicate.is(1, 1) // true
predicate.is(NaN, 0/0) // true
predicate.is(0, -0) // false
predicate.is([], []) // false

existspredicate.exists(val)

Returns true if a value is not null or undefined


Example

predicate.exists(null) // false
predicate.exists(false) // true
predicate.exists(0) // true
predicate.exists('javascript') // true

truthypredicate.truthy(val)

Returns true if a value exists and isn't false


Example

predicate.truthy(0) // true
predicate.truthy([1, 2]) // true
predicate.truthy(false) // false
predicate.truthy(null) // false

falseypredicate.falsey(val)

Returns true if a value is false or doesn't exist


Example

predicate.falsey(0) // false
predicate.falsey([1, 2]) // false
predicate.falsey(false) // true
predicate.falsey(null) // true

nullpredicate.null(val)

Returns true if a value is null


Example

predicate.null(null) // true
predicate.null(false) // false
predicate.null(undefined) // false

undefpredicate.undef(val)

Returns true if a value is undefined


Example

predicate.undef(undefined) // true
predicate.undef(null) // false
predicate.undef(false) // false

equalpredicate.equal(a, b)

Returns true if a === b

curryable

Example

predicate.equal(1, 1) // true
predicate.equal('a', 'a') // true
predicate.equal(null, 1) // false
predicate.equal([1, 2], [1, 2]) // false

eqpredicate.eq(a, b)

Returns true if a == b

curryable

Example

predicate.eq('1', 1) // true
predicate.eq('a', 'b') // false

lesspredicate.less(a, b)

aliaspredicate.lt(a, b)

Returns true if a < b

curryable

Example

predicate.less(1, 5) // true
predicate.less(5, 5) // false
predicate.less(5, 1) // false

lessEqpredicate.lessEq(a, b)

aliaspredicate.ltEq(a, b) predicate.le(a, b)

Returns true if a <= b

curryable

Example

predicate.lessEq(1, 5) // true
predicate.lessEq(5, 5) // true
predicate.lessEq(5, 1) // false

greaterpredicate.greater(a, b)

aliaspredicate.gt(a, b)

Returns true if a > b

curryable

Example

predicate.greater(5, 1) // true
predicate.greater(5, 5) // false
predicate.greater(1, 5) // false

greaterEqpredicate.greaterEq(a, b)

aliaspredicate.gtEq(a, b) predicate.ge(a, b)

Returns true if a > b

curryable

Example

predicate.greaterEq(5, 1) // true
predicate.greaterEq(5, 5) // true
predicate.greaterEq(1, 5) // false

objectpredicate.object(val)

aliaspredicate.obj(val)

Returns true if val is an object


Example

predicate.object({}) // true
predicate.object([]) // true
predicate.object(1) // false

arraypredicate.array(val)

aliaspredicate.arr(val)

Returns true if val is an array

Defaults to native Array.isArray


Example

predicate.array([]) // true
predicate.array({}) // false
predicate.array(1) // false

datepredicate.date(val)

Returns true if val is a date


Example

predicate.date(new Date()) // true
predicate.date({}) // false
predicate.date(1) // false

rgxpredicate.rgx(val)

aliaspredicate.RegExp(val) predicate.regexp(val) predicate.regex(val)

Returns true if val is a rgx


Example

predicate.rgx(/\d{1,4}/) // true
predicate.rgx(new RegExp("Cosmos", "gi")) // true
predicate.rgx({}) // false
predicate.rgx("Dark Star") // false

finitepredicate.finite(val)

Returns true if val is a number and isFinite


Example

predicate.finite(1) // true
predicate.finite(2e64) // true
predicate.finite(NaN) // false
predicate.finite('1') // false
predicate.finite(Infinity) // false

nanpredicate.nan(val)

aliaspredicate.NaN(val)

Returns true if val is NaN


Example

predicate.nan(nan) // true
predicate.nan("titan") // false
predicate.nan(1) // false

argumentspredicate.arguments(val)

Returns true if val is an arguments object


Example

predicate.arguments(arguments) // true
predicate.arguments("titan") // false

errorpredicate.error(val)

Returns true if val is an error object


Example

predicate.error(new Error()) // true
predicate.error("titan") // false

fnpredicate.fn(val)

aliaspredicate.function(val)

Returns true if typeof val === 'function'


Example

predicate.fn(predicate.fn) // true
predicate.fn(Math.max) // true
predicate.fn({}) // false
predicate.fn("Dark Star") // false

numpredicate.num(val)

aliaspredicate.number(val)

Returns true if typeof val === 'number'


Example

predicate.num(1) // true
predicate.num(NaN) // false
predicate.num({}) // false
predicate.num("Dark Star") // false

strpredicate.str(val)

aliaspredicate.string(val)

Returns true if typeof val === 'string'


Example

predicate.str("Dark Star") // true
predicate.str(1) // false
predicate.str({}) // false

boolpredicate.bool(val)

aliaspredicate.boolean(val)

Returns true if typeof val === 'boolean'


Example

predicate.bool(true) // false
predicate.bool(false) // false
predicate.bool("Dark Star") // false
predicate.bool(1) // false

intpredicate.int(val)

Returns true if val is an integer


Example

predicate.int(1) // true
predicate.int(1.5) // false

primitivepredicate.primitive(val)

Returns true if val is a primitive value


Example

predicate.primitive(1) // true
predicate.primitive('a') // true
predicate.primitive(null) // true
predicate.primitive(NaN) // true
predicate.primitive([]) // false
predicate.primitive({}) // false

pospredicate.pos(val)

Returns true if val > 0 and is a number


Example

predicate.pos(1) // true
predicate.pos(-2) // false
predicate.pos(0) // false

negpredicate.neg(val)

Returns true if val < 0 and is a number


Example

predicate.neg(-2) // true
predicate.neg(1) // false
predicate.neg(0) // false

zeropredicate.zero(val)

Returns true if val === 0 and is a number


Example

predicate.zero(0) // true
predicate.zero(-2) // false
predicate.zero(1) // false

evenpredicate.even(val)

Returns true if val is a number, not zero, and it's modulus of 2 is zero


Example

predicate.even(2) // true
predicate.even(0) // false
predicate.even(1) // false

oddpredicate.odd(val)

Returns true if val is a number, not zero, and it's modulus of 2 is not zero


Example

predicate.odd(1) // true
predicate.odd(2) // false
predicate.odd(0) // false

includespredicate.includes(arr, val)

aliaspredicate.contains(arr, val)

Returns true if val exists in arr

curryable

Example

var arr = [1, 2, 3, 4];
predicate.includes(arr, 2) // true
predicate.includes(arr, -5) // false

emptypredicate.empty(o)

Returns true if o is an object and has no owned props

Returns true if o is an array or string of length zero

Throws TypeError if o is not an Array, Object, or String


Example

predicate.empty([]) // true
predicate.empty(['a']) // false
predicate.empty({}) // true
predicate.empty({foo: 'bar'}) // false

haspredicate.has(o, key)

Returns true if string key exists on object o

curryable

Example

var o = { planet: 'Earth' };
predicate.has(o, 'planet') // true
predicate.has(o, 'moon') // false

instancepredicate.instance(Cls, [inst])

Returns true if inst instanceof Cls is true.

curryable

Example

function Planet() {}
var p = new Planet();

var isPlanet = predicate.instance(Planet);
isPlanet(p); // true
predicate.instance(Planet, p); // true

matchespredicate.matches(rgx, val)

Returns rgx.test(val)

curryable

Example

var rgx = /\d{3}/;
predicate.matches(rgx, 200) // true
predicate.matches(rgx, 50) // false

ternarypredicate.ternary(pred, [a [b]])

If a pred is given:

Returns a function(a, b) that evaluates pred(a, b)

If pred and a and is given:

Returns a function(b) that evaluates pred(a, b)

If pred and a and b is given:

Returns the evaluation of pred(a, b)

If pred is of type boolean, a, and b are given

Returns bool ? a : b


Example

var pred1 = predicate.ternary(predicate.less);
pred1(1, 2); // 1

var pred2 = predicate.ternary(predicate.less, 1);
pred2(2); // 1

predicate.ternary(predicate.less, 1, 2); // 1
predicate.ternary(1 < 2, 'a', 'b'); // 'a'

andpredicate.and([p1, p2, ...pN])

Returns a function in which, when evaluated with val, returns true if all given predicates return true.


Example

var fn = predicate.and(predicate.less(3), predicate.greater(1));
fn(2); // true

orpredicate.or([p1, p2, ...pN])

Returns a function in which, when evaluated with val, returns true if at least one of the given predicates return true.


Example

var fn = predicate.or(predicate.less(3), predicate.greater(1));
fn(1); // true

Chaining


everypredicate.every()

aliaspredicate.all()

Returns a chainable interface for lazily executing predicates.

Invoke .val() which returns true if all predicates evaluate to true.


Example

var chain = predicate.every();
chain = chain.less(1, 2).fn(predicate.exists);
chain.val(); // true
chain.NaN(1).val(); // false

predicate.every().exists('mars').str('asteroid').val(); //true

somepredicate.some()

aliaspredicate.any()

Returns a chainable interface for lazily executing predicates.

Invoke .val() which returns true if at least 1 predicate evaluate to true.


Example

var chain = predicate.some();
chain = chain.equal(1, 2).fn(predicate.exists);
chain.val(); // true
chain.NaN(1).val(); // true

predicate.some().exists(null).str('asteroid').val(); // true
predicate.some().exists(null).str(1).val(); // false

Utils


currypredicate.curry(fn)

Returns a new function that curries a given fn.

Used internally

Example

var fn = predicate.curry(function add(a, b) {
  return a + b;
});
var fn2 = add(1);

fn2(2); // 3
fn2(5); // 6
fn(2, 2); // 4

partialpredicate.partial(fn, ...)

Returns a partially applied function based on fn.

Used internally

Example

var fn = predicate.partial(predicate.less, 1);
fn(2); // true

complementpredicate.complement(fn)

aliaspredicate.invert(fn)

Returns a function a that receives the same args as fn and returns the opposite truth value.

Used internally

Example

var fn = predicate.complement(predicate.fn);
fn(predicate.fn); // false

modpredicate.mod(a, b)

Returns the value of a % b

Used internally

Example

predicate.mod(6, 5); // 1
predicate.mod(6, 6); // 0


changelog

1.2.0

  • Add support for searching strings using includes
  • Use native Object.assign when available

1.1.2

  • Add includes as alias to contains

1.1.1

  • Improve perf around chaining methods

1.1.0

  • add and and or functions

1.0.0

  • Convert to nodejs 4.x es6 support

0.12.0

  • Add predicate.matches

0.11.0

  • Add alias predicate.nan
  • Add alias predicate.regex and predicate.regexp
  • Add alias predicate.le
  • Add alias predicate.ge

0.10.2

  • Changed from is.js to predicate.js

0.10.0

0.9.0

  • Added is.is based off polyfill from MDN
  • Added alias for is.object and is.arr
  • is.num(NaN) now returns false

0.8.2

  • Correctly test for NaN in is.contains

0.8.1

  • Fix currying issue with is.not

0.8.0

  • Added is.curry which is used internally
  • Added autocurrying for all functions that are arity 2
  • is.complement can now return a function instead of just booleans
  • is.truthy and is.falsey now respect js falsey values (eg 0, '', etc)

0.7.3

  • Refactored file structure

0.7.2

  • Update docs

0.7.1

  • Converted module to node style package
  • build process uses browserify standalone
  • Tests now in JS instead of CS (run much faster!)
  • Internal changes around chaining
  • Dropped gulp

0.7.0

  • Added even, odd
  • Exposed mod
  • Even more improved docs!

0.6.0

  • Removed cmp
  • Added is.empty
  • Alias is.invert as is.complement
  • Expose is.partial from internals
  • Improved docs

0.5.0

  • Added is.zero
  • Fix is.object and is.error
  • is.ternary now supports partial application
  • is.gt, is.gtEq, is.lt, is.ltEq aliases added

0.4.0

  • Support for lazy chain evaluation

0.3.0

  • Expose `is.invert
  • Added `is.contains
  • Added `is.has
  • Remove bower support

0.2.0

  • Added is.pos
  • Added is.neg
  • Added is.ternary
  • Added is.not, which inverses all boolean returning predicate methods

0.1.1

  • Added is.bool/boolean method

0.1.0

  • Release