JSPM

  • Created
  • Published
  • Downloads 135
  • Score
    100M100P100Q67288F
  • License MIT

a posteriory simple type checking for js

Package Exports

  • utils-type

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (utils-type) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm downloads NPM version build build

utils-type

a posteriory simple type checking for js progressed.io

Heavily inspired by api design of ianstorm's is.

Implementation state : missing browser tests.

install

$ npm install utils-type

usage

var is = require('utils-type');

is( 42 );              // -> { number: 42 }
is( NaN );             // -> { number: 'NaN', nan: true }
is( null );            // -> { null: true }
is( true );            // -> { boolean: true }
is( false );           // -> { boolean: 'false' }
is( Infinity );        // -> { number: Infinity, infinity: true }
is( undefined );       // -> { undefined: true }
is( 'a string');       // -> { string: 'a string' }
is( /a regex/ );       // -> { object: true, regexp: /a regex/ } }
is( function(){ } );   // -> { object: true, function: [Function] }
is( { type : 'toy' }); // -> { object: { type: 'toy' } }
is( new Date() );      // -> { object: true,  date: Mon Sep 08 2014 19:10:32 GMT+0200 (CEST) }
is( new Error() );     // -> { object: true, error: [Error] }

// and more!
is( new Stream() );
// -> { object: true, eventemitter: true, stream: { domain: null, _events: {}, _maxListeners: 10 } }
is( new EventEmitter() );
// -> { object: true, eventemitter: { domain: null, _events: {}, _maxListeners: 10 } }

composing

The function returns an object. The type mached by what type returns itself. That is:

is(1).number      // -> 1 (that is truthy)
is([1,2,3]).array // -> [1,2,3] (truthy)
is([1,2,3])       // -> { object : true, array : [1,2,3] }

so its very easy to compose

is(is([1,2,3]).array[1]).number // -> 1 (truthy)

and continue as much as possible

is(
  is(
    is([1,Infinity,3]).array[1] ).number ).Infinity

Something I don't know if I should change is how falsy values are handled (false, 0 or NaN). At the moment they are concatenated with a string so they become truthy. Though this might have some drawbacks I will have to investigate.

var arr = [false,0,NaN];

is(is(arr).array[0]).boolean // -> 'false'       (truthy)
is(is(arr).array[1]).number  // -> '0'           (truthy)
is(is(arr).array[1]).object  // -> undefined     (nope)
is(is(arr).array[2]).number  // 'NaN'     (truthy)

philosophy

No specific method for each class is implemented. Instead use constructor.name and if is not present ({}).toString to get the [[Class]]. This makes possible to find types/instances names all over the map with very little code.

No booleans for the return value. An object is returned. True/false is achieved just by checking the existence of properties.

test

$ make test
or
$ npm test

todo

  • Make browser tests
  • Make more server tests (included stream, event emitter)
  • Provide a map so to rename types. Either because:
    • You like things your way, sorter, different prop names.
    • It seems to be an issue for some environments to have a function property.
  • Maybe chain everything ?
    • that is is([1,2,3]).array[1].number > 0
    • instead of what's the case now is(is([1,2,3]).array[1]).number > 0

license

LICENSE