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
documentation - install - todo
simple
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: Infinity }
type( undefined ); // -> { undefined: true }
type( 'a string'); // -> { string: 'a string' }
type( /a regex/ ); // -> { object: /a regex/, regexp: /a regex/ } }
type( function(){ } ); // -> { object: [Function], function: [Function] }
type({ type : 'toy' }); // -> { object: { type: 'toy' } }
type( new Date() ); // -> { object: Mon Sep 08 2014 19:10:32, date: Mon Sep 08 2014 19:10:32 GMT+0200 (CEST) }
type( new Error() ); // -> { object: [Error], error: [Error] }
type( new Stream() ); // ->
// {
// object: { domain: null, _events: {}, _maxListeners: 10 },
// stream: { domain: null, _events: {}, _maxListeners: 10 },
// eventemitter: { domain: null, _events: {}, _maxListeners: 10 }
// }
type( new EventEmitter() ); // ->
// {
// object: { domain: null, _events: {}, _maxListeners: 10 },
// eventemitter: { domain: null, _events: {}, _maxListeners: 10 }
// }one to many
type(function one(){}).match(/function|object/);
// => [Function: one]composition
The function returns an object. The type matched by that type returns itself. That is:
type(1).number // -> 1 (that is truthy)
type([1,2,3]).array // -> [1,2,3] (truthy)
type(type([1,2,3]).array[1]).number // -> 1 (truthy)comprehensive
type(-1).number // -> -1
type(-1).integer // -> -1
type(NaN).nan // -> true
type(0.4).float // -> 0.4
type(Infinity).infinity // -> Infinityfalsy values maintain the value if it makes sense
type(0).number // -> true
type(0).integer // -> 0
type('').string // -> ' '
type(null).null // -> true
type(NaN).number // -> undefined
type(false).boolean // -> true
type(undefined).undefined // -> trueWhy:
NaNis not a numberfalseis a boolean, returning it will be misleading0is a number, but forintegerits value its maintainedthe empty stringis changed to an space so is truthy and operations can be made on itnullandundefinedare self explanatory
--- ### Documentation
The module.exports a function
var type = require('utils-type');that takes one argument.
type
function type(value)arguments
valuetype any, from where types will be obtained
returns
- an object with as many properties as types the argument has
var items = type([1,2,3]);
Object.keys(items); // =>
// ['object', 'array']type(value).match
function type(value).match(RegExp pattern)The exported function has an additional match method giving a one to many relationship.
arguments
valuetype any, from where types will be obtainedpatterntype regexp, regular expression to match types to
returns
- null if
valuetypes did not match the givenpattern value, true or space if the types ofvaluematchedpatternvaluewhenvalueis truthy- true when
valueis null, NaN, 0, undefined or the empty string
That is, it always returns something truthy when there was a match.
Useful for checks that are not so strict.
var items = type([1,2,3]);
// so you can do this
if(items.match(/object|array/)){
// do something
}
// instead of this
if(items.array || items.object){
// do something
}install
With npm
$ npm install utils-typetest
$ npm testtodo
- Include browser tests