From 6d930d6920feb0fe118c1f6a9f99e8f1657779d2 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 8 Aug 2016 15:44:07 -0700 Subject: [PATCH] Simplify `_.isEmpty`. --- lodash.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lodash.js b/lodash.js index 47e584660..909d6ebef 100644 --- a/lodash.js +++ b/lodash.js @@ -2208,7 +2208,7 @@ * @returns {Array} Returns the array of property names. */ function arrayLikeKeys(value, inherited) { - var result = (isArray(value) || isString(value) || isArguments(value)) + var result = isArray(value) ? baseTimes(value.length, String) : []; @@ -5748,7 +5748,7 @@ } var length = object ? object.length : 0; return !!length && isLength(length) && isIndex(key, length) && - (isArray(object) || isString(object) || isArguments(object)); + (isArray(object) || isArguments(object)); } /** @@ -9401,8 +9401,7 @@ return 0; } if (isArrayLike(collection)) { - var result = collection.length; - return (result && isString(collection)) ? stringSize(collection) : result; + return isString(collection) ? stringSize(collection) : collection.length; } if (isObjectLike(collection)) { var tag = getTag(collection); @@ -11040,24 +11039,23 @@ */ function isEmpty(value) { if (isArrayLike(value) && - (isArray(value) || isString(value) || isFunction(value.splice) || - isArguments(value) || isBuffer(value))) { + (isArray(value) || typeof value == 'string' || + typeof value.splice == 'function' || isBuffer(value) || isArguments(value))) { return !value.length; } - if (isObjectLike(value)) { - var tag = getTag(value); - if (tag == mapTag || tag == setTag) { - return !value.size; - } + var tag = getTag(value); + if (tag == mapTag || tag == setTag) { + return !value.size; + } + if (nonEnumShadows || isPrototype(value)) { + return !nativeKeys(value).length; } - var isProto = isPrototype(value); for (var key in value) { - if (hasOwnProperty.call(value, key) && - !(isProto && key == 'constructor')) { + if (hasOwnProperty.call(value, key)) { return false; } } - return !(nonEnumShadows && nativeKeys(value).length); + return true; } /**