predicates

Set of various predicates for type checking, assertions, filtering etc

Predicates

Set of various predicates for type checking, assertions, filtering etc.

npm install predicates
bower install predicates.js

For list of all available functions see API reference

If you wonder why predicates is different, read API design principles

var is = require('predicates');
 
is.string(1); // false 
is.string('test'); // true 
 
is.undefinedOr(is.string, undefined); // true 
is.undefinedOr(is.string, 'timmy'); // true 
is.undefinedOr(is.string)(undefined); // true 
is.undefinedOr(is.string)('timmy'); // true 
 
var isPerson = is.structure({
    name: is.string,
    surname: is.undefinedOr(is.string),
    age: is.number
});
 
isPerson({name: 'Tom', age: 10}); // true 
isPerson({surname: 'Welling', age: 100}); // false, lack of name property 

Read API reference for list of all available functions

Assert-generator helps create assertions directly from predicates which is very useful for arguments assertions

var assertName = createAssert(is.string, 'Name must be a string'),
    assertSurname = createAssert(is.all(
        is.string, is.not(is.empty)
    ), 'Surname must be a string and cannot be empty'),
    assertAge = createAssert(is.undefinedOr(is.positive),
        'Age must be a number and greater than 0');
 
 
var Person = function(namesurnameage) {
    assertName(name);
    assertSurname(surname);
    assertAge(age);
}
 
new Person('Tom', 'Welling', 33); // OK! 
new Person('Tom', 'Welling'); // OK! 
new Person('Tom', '', 33); // Error: Surname must be a string and cannot be emptye 

Supported by all major browsers including IE >=9. IE8 is supported as well but you need to include https://github.com/es-shims/es5-shim and es5-sham.

  • clone repo
  • ensure your editor is decent and pick up the .editorconfig files
  • npm install
  • add tests, add features
  • gulp --> there should be no errors
  • Added bower support #16
  • Provided names for all the functions to make debugging easier #13
  • Added primitive predicate #12
  • Added plainObject predicate #4
  • Added divisible and divisibleWithRemainder predicates
  • Added property predicate #14
  • Added notBlank predicate
  • Improved performance
  • Fixed path to finite polyfill
  • Better support for is.NaN #11
  • Added notEmpty predicate #9
  • Improved is.false and is.true to accept new Boolean(true/false) as well
  • Fixed behavior of is.object to accept Object.create(null) #10
  • Fix: instanceof renamed to instanceOf
  • Added information how to use predicates for arguments assertions
  • Added documentation with examples and design rules
  • Added "startsWith" and "endsWith" functions
  • Added type checking for property names for hasOwnProperty and hasProperty predicate
  • Fixed error type declaration for @throws