Expose _.isPlainObject.

Former-commit-id: 884bc87df7773ef3cb5e52725d5a37f07812385a
This commit is contained in:
John-David Dalton
2012-09-23 21:30:14 -07:00
parent 5d2d86bffc
commit 77242bfb95
2 changed files with 69 additions and 48 deletions

View File

@@ -906,7 +906,7 @@
* @returns {Boolean} Returns `true` if the `value` is a function, else `false`.
* @example
*
* _.isFunction(''.concat);
* _.isFunction(_);
* // => true
*/
function isFunction(value) {
@@ -919,6 +919,44 @@
};
}
/**
* Checks if a given `value` is an object created by the `Object` constructor.
*
* @static
* @memberOf _
* @category Objects
* @param {Mixed} value The value to check.
* @param {Boolean} [skipArgsCheck=false] Internally used to skip checks for
* `arguments` objects.
* @returns {Boolean} Returns `true` if `value` is a plain object, else `false`.
* @example
*
* function Stooge(name, age) {
* this.name = name;
* this.age = age;
* }
*
* _.isPlainObject(new Stooge('moe', 40));
* // false
*
* _.isPlainObject([1, 2, 3]);
* // false
*
* _.isPlainObject({ 'name': 'moe', 'age': 40 });
* // => true
*/
var isPlainObject = objectTypes.__proto__ != ObjectProto ? isPlainFallback : function(value, skipArgsCheck) {
if (!value) {
return false;
}
var valueOf = value.valueOf,
objProto = typeof valueOf == 'function' && (objProto = valueOf.__proto__) && objProto.__proto__;
return objProto
? value == objProto || (value.__proto__ == objProto && (skipArgsCheck || !isArguments(value)))
: isPlainFallback(value);
};
/**
* A fallback implementation of `isPlainObject` that checks if a given `value`
* is an object created by the `Object` constructor, assuming objects created
@@ -964,27 +1002,6 @@
return result;
}
/**
* Checks if a given `value` is an object created by the `Object` constructor.
*
* @private
* @param {Mixed} value The value to check.
* @param {Boolean} [skipArgsCheck=false] Internally used to skip checks for
* `arguments` objects.
* @returns {Boolean} Returns `true` if `value` is a plain object, else `false`.
*/
var isPlainObject = objectTypes.__proto__ != ObjectProto ? isPlainFallback : function(value, skipArgsCheck) {
if (!value) {
return false;
}
var valueOf = value.valueOf,
objProto = typeof valueOf == 'function' && (objProto = valueOf.__proto__) && objProto.__proto__;
return objProto
? value == objProto || (value.__proto__ == objProto && (skipArgsCheck || !isArguments(value)))
: isPlainFallback(value);
};
/**
* A shim implementation of `Object.keys` that produces an array of the given
* object's own enumerable property names.
@@ -1553,6 +1570,9 @@
* _.isObject({});
* // => true
*
* _.isObject([1, 2, 3]);
* // => true
*
* _.isObject(1);
* // => false
*/
@@ -4160,6 +4180,7 @@
lodash.isNull = isNull;
lodash.isNumber = isNumber;
lodash.isObject = isObject;
lodash.isPlainObject = isPlainObject;
lodash.isRegExp = isRegExp;
lodash.isString = isString;
lodash.isUndefined = isUndefined;