modulex-util
Version:
common utilities from modulex
151 lines (140 loc) • 3.87 kB
JavaScript
/**
* @ignore
* type judgement
* @author yiminghe@gmail.com, lifesinger@gmail.com
*/
var util = require('./base');
// [[Class]] -> type pairs
var class2type = {},
FALSE = false,
undef,
noop = util.noop,
OP = Object.prototype,
toString = OP.toString;
function hasOwnProperty(o, p) {
return OP.hasOwnProperty.call(o, p);
}
util.mix(util, {
/**
* Determine the internal JavaScript [[Class]] of an object.
* @member util
*/
type: function (o) {
return o == null ?
String(o) :
class2type[toString.call(o)] || 'object';
},
/**
* Checks to see if an object is a plain object (created using '{}'
* or 'new Object()' but not 'new FunctionClass()').
* @member util
*/
isPlainObject: function (obj) {
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that Dom nodes and window objects don't pass through, as well
if (!obj || util.type(obj) !== 'object' || obj.nodeType ||
/*jshint eqeqeq:false*/
// must == for ie8
obj.window == obj) {
return FALSE;
}
var key, objConstructor;
try {
// Not own constructor property must be Object
if ((objConstructor = obj.constructor) && !hasOwnProperty(obj, 'constructor') && !hasOwnProperty(objConstructor.prototype, 'isPrototypeOf')) {
return FALSE;
}
} catch (e) {
// IE8,9 Will throw exceptions on certain host objects
return FALSE;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
/*jshint noempty:false*/
for (key in obj) {
}
return ((key === undef) || hasOwnProperty(obj, key));
}
});
if ('@DEBUG@') {
util.mix(util, {
/**
* test whether o is boolean
* @method
* @param o
* @return {Boolean}
* @member util
*/
isBoolean: noop,
/**
* test whether o is number
* @method
* @param o
* @return {Boolean}
* @member util
*/
isNumber: noop,
/**
* test whether o is String
* @method
* @param o
* @return {Boolean}
* @member util
*/
isString: noop,
/**
* test whether o is function
* @method
* @param o
* @return {Boolean}
* @member util
*/
isFunction: noop,
/**
* test whether o is Array
* @method
* @param o
* @return {Boolean}
* @member util
*/
isArray: noop,
/**
* test whether o is Date
* @method
* @param o
* @return {Boolean}
* @member util
*/
isDate: noop,
/**
* test whether o is RegExp
* @method
* @param o
* @return {Boolean}
* @member util
*/
isRegExp: noop,
/**
* test whether o is Object
* @method
* @param o
* @return {Boolean}
* @member util
*/
isObject: noop
});
}
var types = 'Boolean Number String Function Date RegExp Object Array'.split(' ');
for (var i = 0; i < types.length; i++) {
/*jshint loopfunc:true*/
(function (name, lc) {
// populate the class2type map
class2type['[object ' + name + ']'] = (lc = name.toLowerCase());
// add isBoolean/isNumber/...
util['is' + name] = function (o) {
return util.type(o) === lc;
};
})(types[i], i);
}
util.isArray = Array.isArray || util.isArray;