mout-lang-type
v0.6.0
Published
An extension to the `mout/lang` utilities
Downloads
755
Maintainers
Readme
mout-lang-type
An extension to the mout/lang utilities for more reliable type testing
mout-lang-type is an extension to MOUT’s lang utilities. It provides two major modules: A typeOf
and an isType
function to be used for more reliable type checks where the included kindOf
and isKind
functions would fail. The difference is that those included functions always use the string description tags of objects (historically known as the [[Class]]
internal slot tags) expressed in UpperCamelCase for testing, whereas these additional functions use only the limited set of built-in object tags expressed in lowerCamelCase and yield precedence to the results of the native typeof
operator whenever possible:
kindOf( navigator ); // 'Navigator'
typeOf( navigator ); // 'object'
typeof []; // 'object'
typeOf( [] ); // 'array'
This extension also provides two additional modules: An instanceOf
function that improves the native instanceof
operator by supporting comparisons with primitive values and fixing a memory leak in legacy Internet Explorer versions; and an isComplex
function that tests whether values are of complex (non-primitive) data types or not:
1 instanceof Number; // false
instanceOf( 1, Number ); // true
// No memory leaks in IE < 9 on COM objects:
instanceOf( window, Object ); // false
isComplex( 1 ); // false
isComplex( new Number ); // true
Lastly, this extension provides a bunch of replacement modules: An isPrimitive
function that adds support for ES2015 symbols and finally the isFunction
, isObject
and isRegExp
functions that internally use isType
instead of isKind
for their specific type checks:
isFunction( alert ); // true, even in IE7
isObject( window ); // true, same here
isPrimitive( Symbol() ); // true
isRegExp( /^regExp$/i ); // true
Getting started
Installation
Install this extension as a dependency to your project using Bower:
$ bower install --save mout-lang-type
Alternatively, it is also available using npm:
$ npm install --save mout-lang-type
Usage
You can load and use this extension as a module using the AMD or CommonJS API:
// Load the overall module:
var
moutLangType = require('mout-lang-type');
// Use the overall module:
moutLangType.lang.instanceOf( [], Array ); // true
moutLangType.lang.typeOf( null ) === 'null'; // true
// Load all lang utilities:
var
lang = require('mout-lang-type/lang');
// Use the lang utilities:
lang.isPrimitive( false ); // true
lang.isType( new String('foo'), 'string'); // true
// Load the individual utilities:
var
isComplex = require('mout-lang-type/lang/isComplex'),
isFunction = require('mout-lang-type/lang/isFunction');
// Use the individual utilities:
isComplex( new String('bar') ); // true
isFunction( Array ); // true
Keep in mind that the module’s name mout-lang-type
in AMD is actually just the module’s root directory and can differ from the example above depending on your deployed scripts directory structure.
Policy
This is communist software. It is crafted with heart and soul to the best of the author’s knowledge and belief: Not for profit but to satisfy the concrete needs. Do whatever you want with it (as long as you keep the author’s copyright notice in all copies or substantial portions of it included) for free. Imagine how the world could be if others would produce and distribute their products for the same benefits and ask yourself why they’re actually not.
License
This software is licensed under MIT License.
Copyright © 2015 Christian Grete