UNPKG

2 kBJavaScriptView Raw
1import baseKeys from './_baseKeys.js';
2import getTag from './_getTag.js';
3import isArguments from './isArguments.js';
4import isArray from './isArray.js';
5import isArrayLike from './isArrayLike.js';
6import isBuffer from './isBuffer.js';
7import isPrototype from './_isPrototype.js';
8import isTypedArray from './isTypedArray.js';
9
10/** `Object#toString` result references. */
11var mapTag = '[object Map]',
12 setTag = '[object Set]';
13
14/** Used for built-in method references. */
15var objectProto = Object.prototype;
16
17/** Used to check objects for own properties. */
18var hasOwnProperty = objectProto.hasOwnProperty;
19
20/**
21 * Checks if `value` is an empty object, collection, map, or set.
22 *
23 * Objects are considered empty if they have no own enumerable string keyed
24 * properties.
25 *
26 * Array-like values such as `arguments` objects, arrays, buffers, strings, or
27 * jQuery-like collections are considered empty if they have a `length` of `0`.
28 * Similarly, maps and sets are considered empty if they have a `size` of `0`.
29 *
30 * @static
31 * @memberOf _
32 * @since 0.1.0
33 * @category Lang
34 * @param {*} value The value to check.
35 * @returns {boolean} Returns `true` if `value` is empty, else `false`.
36 * @example
37 *
38 * _.isEmpty(null);
39 * // => true
40 *
41 * _.isEmpty(true);
42 * // => true
43 *
44 * _.isEmpty(1);
45 * // => true
46 *
47 * _.isEmpty([1, 2, 3]);
48 * // => false
49 *
50 * _.isEmpty({ 'a': 1 });
51 * // => false
52 */
53function isEmpty(value) {
54 if (value == null) {
55 return true;
56 }
57 if (isArrayLike(value) &&
58 (isArray(value) || typeof value == 'string' || typeof value.splice == 'function' ||
59 isBuffer(value) || isTypedArray(value) || isArguments(value))) {
60 return !value.length;
61 }
62 var tag = getTag(value);
63 if (tag == mapTag || tag == setTag) {
64 return !value.size;
65 }
66 if (isPrototype(value)) {
67 return !baseKeys(value).length;
68 }
69 for (var key in value) {
70 if (hasOwnProperty.call(value, key)) {
71 return false;
72 }
73 }
74 return true;
75}
76
77export default isEmpty;