UNPKG

@newdash/newdash

Version:

javascript/typescript utility library

96 lines (95 loc) 2.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isEmpty = void 0; const baseKeys_1 = __importDefault(require("./.internal/baseKeys")); const getTag_1 = __importDefault(require("./.internal/getTag")); const isPrototype_1 = __importDefault(require("./.internal/isPrototype")); const isArguments_1 = __importDefault(require("./isArguments")); const isArrayLike_1 = __importDefault(require("./isArrayLike")); const isBuffer_1 = __importDefault(require("./isBuffer")); const isTypedArray_1 = __importDefault(require("./isTypedArray")); /** * Used to check objects for own properties. * @ignore */ const hasOwnProperty = Object.prototype.hasOwnProperty; /** * @ignore */ const mapTag = "[object Map]"; /** * @ignore */ const setTag = "[object Set]"; /** * @ignore * @internal * @private */ const isArray = Array.isArray; /** * 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`. * * heavy operation * * @since 5.5.0 * @category Lang * @param value The value to check. * @returns Returns `true` if `value` is empty, else `false`. * @example * * ```js * 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 ((0, isArrayLike_1.default)(value) && (isArray(value) || typeof value == "string" || typeof value["splice"] == "function" || (0, isBuffer_1.default)(value) || (0, isTypedArray_1.default)(value) || (0, isArguments_1.default)(value))) { return !value.length; } const tag = (0, getTag_1.default)(value); if (tag == mapTag || tag == setTag) { return !value.size; } if ((0, isPrototype_1.default)(value)) { return !(0, baseKeys_1.default)(value).length; } for (const key in value) { if (hasOwnProperty.call(value, key)) { return false; } } return true; } exports.isEmpty = isEmpty; exports.default = isEmpty;