UNPKG

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