shimIsPlainObject.js (1811B)
1 var baseForIn = require('./baseForIn'), 2 isObjectLike = require('./isObjectLike'); 3 4 /** `Object#toString` result references. */ 5 var objectTag = '[object Object]'; 6 7 /** Used for native method references. */ 8 var objectProto = Object.prototype; 9 10 /** Used to check objects for own properties. */ 11 var hasOwnProperty = objectProto.hasOwnProperty; 12 13 /** 14 * Used to resolve the `toStringTag` of values. 15 * See the [ES spec](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.prototype.tostring) 16 * for more details. 17 */ 18 var objToString = objectProto.toString; 19 20 /** 21 * A fallback implementation of `_.isPlainObject` which checks if `value` 22 * is an object created by the `Object` constructor or has a `[[Prototype]]` 23 * of `null`. 24 * 25 * @private 26 * @param {*} value The value to check. 27 * @returns {boolean} Returns `true` if `value` is a plain object, else `false`. 28 */ 29 function shimIsPlainObject(value) { 30 var Ctor; 31 32 // Exit early for non `Object` objects. 33 if (!(isObjectLike(value) && objToString.call(value) == objectTag) || 34 (!hasOwnProperty.call(value, 'constructor') && 35 (Ctor = value.constructor, typeof Ctor == 'function' && !(Ctor instanceof Ctor)))) { 36 return false; 37 } 38 // IE < 9 iterates inherited properties before own properties. If the first 39 // iterated property is an object's own property then there are no inherited 40 // enumerable properties. 41 var result; 42 // In most environments an object's own properties are iterated before 43 // its inherited properties. If the last iterated property is an object's 44 // own property then there are no inherited enumerable properties. 45 baseForIn(value, function(subValue, key) { 46 result = key; 47 }); 48 return typeof result == 'undefined' || hasOwnProperty.call(value, result); 49 } 50 51 module.exports = shimIsPlainObject;