chai
Version:
BDD/TDD assertion library for node.js and the browser. Test framework agnostic.
46 lines (41 loc) • 1.09 kB
JavaScript
/*!
* Chai - type utility
* Copyright(c) 2012-2013 Jake Luer <jake@alogicalparadox.com>
* MIT Licensed
*/
/*!
* Detectable javascript natives
*/
var natives = {
'[object Arguments]': 'arguments'
, '[object Array]': 'array'
, '[object Date]': 'date'
, '[object Function]': 'function'
, '[object Number]': 'number'
, '[object RegExp]': 'regexp'
, '[object String]': 'string'
};
/**
* ### type(object)
*
* Better implementation of `typeof` detection that can
* be used cross-browser. Handles the inconsistencies of
* Array, `null`, and `undefined` detection.
*
* utils.type({}) // 'object'
* utils.type(null) // `null'
* utils.type(undefined) // `undefined`
* utils.type([]) // `array`
*
* @param {Mixed} object to detect type of
* @name type
* @api private
*/
module.exports = function (obj) {
var str = Object.prototype.toString.call(obj);
if (natives[str]) return natives[str];
if (obj === null) return 'null';
if (obj === undefined) return 'undefined';
if (obj === Object(obj)) return 'object';
return typeof obj;
};