time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

isPlainObject.js (1650B)


      1 var baseGetTag = require('./_baseGetTag'),
      2     getPrototype = require('./_getPrototype'),
      3     isObjectLike = require('./isObjectLike');
      4 
      5 /** `Object#toString` result references. */
      6 var objectTag = '[object Object]';
      7 
      8 /** Used for built-in method references. */
      9 var funcProto = Function.prototype,
     10     objectProto = Object.prototype;
     11 
     12 /** Used to resolve the decompiled source of functions. */
     13 var funcToString = funcProto.toString;
     14 
     15 /** Used to check objects for own properties. */
     16 var hasOwnProperty = objectProto.hasOwnProperty;
     17 
     18 /** Used to infer the `Object` constructor. */
     19 var objectCtorString = funcToString.call(Object);
     20 
     21 /**
     22  * Checks if `value` is a plain object, that is, an object created by the
     23  * `Object` constructor or one with a `[[Prototype]]` of `null`.
     24  *
     25  * @static
     26  * @memberOf _
     27  * @since 0.8.0
     28  * @category Lang
     29  * @param {*} value The value to check.
     30  * @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
     31  * @example
     32  *
     33  * function Foo() {
     34  *   this.a = 1;
     35  * }
     36  *
     37  * _.isPlainObject(new Foo);
     38  * // => false
     39  *
     40  * _.isPlainObject([1, 2, 3]);
     41  * // => false
     42  *
     43  * _.isPlainObject({ 'x': 0, 'y': 0 });
     44  * // => true
     45  *
     46  * _.isPlainObject(Object.create(null));
     47  * // => true
     48  */
     49 function isPlainObject(value) {
     50   if (!isObjectLike(value) || baseGetTag(value) != objectTag) {
     51     return false;
     52   }
     53   var proto = getPrototype(value);
     54   if (proto === null) {
     55     return true;
     56   }
     57   var Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor;
     58   return typeof Ctor == 'function' && Ctor instanceof Ctor &&
     59     funcToString.call(Ctor) == objectCtorString;
     60 }
     61 
     62 module.exports = isPlainObject;