JSPM

  • Created
  • Published
  • Downloads 115
  • Score
    100M100P100Q67387F
  • License MIT

robust 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

utils-type progressed.io

build NPM version

Robust type checking.

Node Version >= 0.8

install

$ npm install utils-type

usage

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

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

// and more!
type( new Stream() );
// -> { object: true, eventemitter: true, stream: { domain: null, _events: {}, _maxListeners: 10 } }
type( 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:

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

making easy to compose

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

so, you know..., turtles all the way down!

type(
  type(
    type(
      type(
        type(
          type([[[/*...*/[[[Infinity]]]/*...*/]]]))
                                            .array[0])
                                              .array[0])
                                                .array[0])
                                                  .array[0])
                                                    .array[0])
                                                      .array[0]).Infinity
                                                            // => Infinity lol!

falsy values

Falsy values are made true when it makes sense based on their use

var arr = [false, 0, NaN, ''];   // yes, the empty string is falsy

type(type(arr).array[0]).boolean // -> true      (truthy)
type(type(arr).array[1]).number  // -> 0         (falsy)
type(type(arr).array[2]).number  // -> undefined (nope is "Not A Number")
type(type(arr).array[3]).string  // -> ' '       (truthy)

Why:

  • false is a boolean, returning it will be misleading.
  • 0 is a number yes, but if is changed to true you can't add to it after the function returns.
  • NaN is not a number :D
  • the empty string is kept as an space so checks and operations can be done.

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

$ npm test

todo

  • Make browser tests
  • Make more server tests (included stream, event emitter) function property.

license

LICENSE