Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
69 lines (66 sloc)
1.73 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import getTag from './.internal/getTag.js' | |
import isArguments from './isArguments.js' | |
import isArrayLike from './isArrayLike.js' | |
import isBuffer from './isBuffer.js' | |
import isPrototype from './.internal/isPrototype.js' | |
import isTypedArray from './isTypedArray.js' | |
/** Used to check objects for own properties. */ | |
const hasOwnProperty = Object.prototype.hasOwnProperty | |
/** | |
* Checks if `value` is an empty object, collection, map, or set. | |
* | |
* Objects are considered empty if they have no own enumerable string keyed | |
* properties. | |
* | |
* Array-like values such as `arguments` objects, arrays, buffers, strings, or | |
* jQuery-like collections are considered empty if they have a `length` of `0`. | |
* Similarly, maps and sets are considered empty if they have a `size` of `0`. | |
* | |
* @since 0.1.0 | |
* @category Lang | |
* @param {*} value The value to check. | |
* @returns {boolean} Returns `true` if `value` is empty, else `false`. | |
* @example | |
* | |
* isEmpty(null) | |
* // => true | |
* | |
* isEmpty(true) | |
* // => true | |
* | |
* isEmpty(1) | |
* // => true | |
* | |
* isEmpty([1, 2, 3]) | |
* // => false | |
* | |
* isEmpty('abc') | |
* // => false | |
* | |
* isEmpty({ 'a': 1 }) | |
* // => false | |
*/ | |
function isEmpty(value) { | |
if (value == null) { | |
return true | |
} | |
if (isArrayLike(value) && | |
(Array.isArray(value) || typeof value === 'string' || typeof value.splice === 'function' || | |
isBuffer(value) || isTypedArray(value) || isArguments(value))) { | |
return !value.length | |
} | |
const tag = getTag(value) | |
if (tag == '[object Map]' || tag == '[object Set]') { | |
return !value.size | |
} | |
if (isPrototype(value)) { | |
return !Object.keys(value).length | |
} | |
for (const key in value) { | |
if (hasOwnProperty.call(value, key)) { | |
return false | |
} | |
} | |
return true | |
} | |
export default isEmpty |