Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Super simple js type checking library.

 _           _
:_;         :_;
.-. .--.    .-. .--.
: :`._-.' _ : :`._-.'
:_;`.__.':_;: :`.__.'
          .-. :
          `._.'

Usage #1 (single argument call):

// someObj {anything}: Object that you would like to know the type of.

is(someObject);

// Will return the type of the object as a lower case string.

is('mystring'); // returns 'string'
is([1, 2, 3]); // returns 'array'
is(true); //returns 'boolean'

Usage #2 (double argument call):

// someObj  {anything}: Object that you would like to check the type of.
// someType {string}: The name of the type you are expecting.

is(someObject, someType);

// Will return true if someObj matches someType.

is('mystring', 'string'); // returns true
is(3.14, 'number'); // returns true
is(42, 'string'); // returns false

Custom types:

If you have your own special objects that are identifiable by certain fields, you can add your own types to check against:

var larry = {
    dressesInWomensClothing: true,
    hangsAroundInBars: true,
    chopTree: function(){}
};

is.add('Lumberjacks', function(obj){
    // return true if obj is a lumberjack, otherwise false.
    return obj.dressesInWomensClothing && obj.hangsAroundInBars;
});

is(larry, 'Lumberjack'); // returns true

And if for some reason you want to remove an added type at a later time:

is.remove('Lumberjack');

is(larry); // return 'object'

Contribute:

Any ideas on how to better detect types, submit a pull request (must include covering jasmine tests), or shoot me a short email.

About

Simple type checking micro library

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages