You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.6 KiB
JavaScript
140 lines
3.6 KiB
JavaScript
10 months ago
|
/**
|
||
|
* lodash (Custom Build) <https://lodash.com/>
|
||
|
* Build: `lodash modularize exports="npm" -o ./`
|
||
|
* Copyright jQuery Foundation and other contributors <https://jquery.org/>
|
||
|
* Released under MIT license <https://lodash.com/license>
|
||
|
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
|
||
|
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
||
|
*/
|
||
|
|
||
|
/** `Object#toString` result references. */
|
||
|
var objectTag = '[object Object]';
|
||
|
|
||
|
/**
|
||
|
* Checks if `value` is a host object in IE < 9.
|
||
|
*
|
||
|
* @private
|
||
|
* @param {*} value The value to check.
|
||
|
* @returns {boolean} Returns `true` if `value` is a host object, else `false`.
|
||
|
*/
|
||
|
function isHostObject(value) {
|
||
|
// Many host objects are `Object` objects that can coerce to strings
|
||
|
// despite having improperly defined `toString` methods.
|
||
|
var result = false;
|
||
|
if (value != null && typeof value.toString != 'function') {
|
||
|
try {
|
||
|
result = !!(value + '');
|
||
|
} catch (e) {}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Creates a unary function that invokes `func` with its argument transformed.
|
||
|
*
|
||
|
* @private
|
||
|
* @param {Function} func The function to wrap.
|
||
|
* @param {Function} transform The argument transform.
|
||
|
* @returns {Function} Returns the new function.
|
||
|
*/
|
||
|
function overArg(func, transform) {
|
||
|
return function(arg) {
|
||
|
return func(transform(arg));
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/** Used for built-in method references. */
|
||
|
var funcProto = Function.prototype,
|
||
|
objectProto = Object.prototype;
|
||
|
|
||
|
/** Used to resolve the decompiled source of functions. */
|
||
|
var funcToString = funcProto.toString;
|
||
|
|
||
|
/** Used to check objects for own properties. */
|
||
|
var hasOwnProperty = objectProto.hasOwnProperty;
|
||
|
|
||
|
/** Used to infer the `Object` constructor. */
|
||
|
var objectCtorString = funcToString.call(Object);
|
||
|
|
||
|
/**
|
||
|
* Used to resolve the
|
||
|
* [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
|
||
|
* of values.
|
||
|
*/
|
||
|
var objectToString = objectProto.toString;
|
||
|
|
||
|
/** Built-in value references. */
|
||
|
var getPrototype = overArg(Object.getPrototypeOf, Object);
|
||
|
|
||
|
/**
|
||
|
* Checks if `value` is object-like. A value is object-like if it's not `null`
|
||
|
* and has a `typeof` result of "object".
|
||
|
*
|
||
|
* @static
|
||
|
* @memberOf _
|
||
|
* @since 4.0.0
|
||
|
* @category Lang
|
||
|
* @param {*} value The value to check.
|
||
|
* @returns {boolean} Returns `true` if `value` is object-like, else `false`.
|
||
|
* @example
|
||
|
*
|
||
|
* _.isObjectLike({});
|
||
|
* // => true
|
||
|
*
|
||
|
* _.isObjectLike([1, 2, 3]);
|
||
|
* // => true
|
||
|
*
|
||
|
* _.isObjectLike(_.noop);
|
||
|
* // => false
|
||
|
*
|
||
|
* _.isObjectLike(null);
|
||
|
* // => false
|
||
|
*/
|
||
|
function isObjectLike(value) {
|
||
|
return !!value && typeof value == 'object';
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Checks if `value` is a plain object, that is, an object created by the
|
||
|
* `Object` constructor or one with a `[[Prototype]]` of `null`.
|
||
|
*
|
||
|
* @static
|
||
|
* @memberOf _
|
||
|
* @since 0.8.0
|
||
|
* @category Lang
|
||
|
* @param {*} value The value to check.
|
||
|
* @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
|
||
|
* @example
|
||
|
*
|
||
|
* function Foo() {
|
||
|
* this.a = 1;
|
||
|
* }
|
||
|
*
|
||
|
* _.isPlainObject(new Foo);
|
||
|
* // => false
|
||
|
*
|
||
|
* _.isPlainObject([1, 2, 3]);
|
||
|
* // => false
|
||
|
*
|
||
|
* _.isPlainObject({ 'x': 0, 'y': 0 });
|
||
|
* // => true
|
||
|
*
|
||
|
* _.isPlainObject(Object.create(null));
|
||
|
* // => true
|
||
|
*/
|
||
|
function isPlainObject(value) {
|
||
|
if (!isObjectLike(value) ||
|
||
|
objectToString.call(value) != objectTag || isHostObject(value)) {
|
||
|
return false;
|
||
|
}
|
||
|
var proto = getPrototype(value);
|
||
|
if (proto === null) {
|
||
|
return true;
|
||
|
}
|
||
|
var Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor;
|
||
|
return (typeof Ctor == 'function' &&
|
||
|
Ctor instanceof Ctor && funcToString.call(Ctor) == objectCtorString);
|
||
|
}
|
||
|
|
||
|
module.exports = isPlainObject;
|