@mojule/is
Duck typing utility
Last updated 2 years ago by nrkn .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @mojule/is 
SYNC missed versions from official npm registry.

is

A type checking facade that doesn't care how you check types - use duck typing, use JSON schema, whatever.

We needed a consistent interface for type checking, sometimes we use duck typing, sometimes we use JSON schema, regardless of which, we tend to follow the same general pattern, is codifies this.

is.string( '' ) // true
is.string( 42 ) // false

usage

npm install @mojule/is

Using default predicates:

const { is } = require( '@mojule/is' )

// These all return true
is.number( 1.1 )
is.integer( 1.0 )
is.string( '' )
is.boolean( true )
is.array( [] )
is.null( null )
is.undefined( undefined )
is.function( i => i + 1 )
is.object({})
is.empty({})

The default predicates are moderately opinionated, e.g. they don't consider null or [] to be is.object

With custom predicates

Here we define additional predicates which extend the default predicates.

const { is, extendDefaults } = require( '@mojule/is' )

const predicates = {
  domNode: subject =>
    is.object( subject ) && is.string( subject.nodeName ),
  div: subject =>
    predicates.domNode( subject ) && subject.nodeName.toLowerCase() === 'div'
}

const domIs = extendDefaults( predicates )

// using the default
domIs.string( '' )

const div = document.querySelector( 'div' )

// These return true
domIs.domNode( div )
domIs.div( div )

Additional functions

In addition to testing against single predicates by name, as above, we provide these additional utility functions, isType, isOnly, some, every, of, allOf.

const { is, utils, Utils } = require( '@mojule/is' )

// utils uses the default predicates
console.log( utils.isOnly( 'abc', 'string' ) )

const predicates = {
  domNode: subject =>
    Is.is.object( subject ) && Is.is.string( subject.nodeName ),
  div: subject =>
    predicates.domNode( subject ) && subject.nodeName.toLowerCase() === 'div'
}

// the Utils factory function can create utility functions for custom predicates
const domUtils = Utils( predicates )

const span = document.querySelector( 'span' )
const div = document.querySelector( 'div' )

// Returns true as predicate for span not in predicates above.
domUtils.isOnly( span, 'domNode' )

// Returns false as div is also 'div'
domUtils.isOnly( div, 'domNode' )

// Returns true, as span matches domNode
domUtils.some( span, 'domNode', 'div' )

// Returns true as a div is both a domNode and a div
domUtils.every( div, 'domNode', 'div' )

// Returns 'domNode' i.e. The first predicate that matches div
domUtils.of( div )

// Returns [ 'domNode', 'div' ] i.e. An array of names of all predicates matching div
domUtils.allOf( div )

Note: Predicates are checked in the order that the keys are declared i.e. in the above example utils.of( div ) returns the predicate name 'domNode' and not 'div' because 'domNode' is declared first in the passed custom predicates.

License

MIT

Current Tags

  • 0.3.9                                ...           latest (2 years ago)

28 Versions

  • 0.3.9                                ...           2 years ago
  • 0.3.8                                ...           2 years ago
  • 0.3.7                                ...           2 years ago
  • 0.3.6                                ...           2 years ago
  • 0.3.5                                ...           2 years ago
  • 0.3.4                                ...           2 years ago
  • 0.3.3                                ...           2 years ago
  • 0.3.2                                ...           2 years ago
  • 0.3.1                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.2.5                                ...           2 years ago
  • 0.2.4                                ...           2 years ago
  • 0.2.3                                ...           2 years ago
  • 0.2.2                                ...           2 years ago
  • 0.2.1                                ...           3 years ago
  • 0.2.0                                ...           3 years ago
  • 0.1.11                                ...           3 years ago
  • 0.1.10                                ...           3 years ago
  • 0.1.9                                ...           3 years ago
  • 0.1.8                                ...           3 years ago
  • 0.1.7                                ...           3 years ago
  • 0.1.6                                ...           3 years ago
  • 0.1.5                                ...           3 years ago
  • 0.1.4                                ...           3 years ago
  • 0.1.3                                ...           3 years ago
  • 0.1.2                                ...           3 years ago
  • 0.1.1                                ...           3 years ago
  • 0.1.0                                ...           3 years ago

Copyright 2014 - 2016 © taobao.org |