UNPKG

super-utils-plus

Version:

A superior alternative to Lodash with improved performance, TypeScript support, and developer experience

143 lines (142 loc) 4.79 kB
"use strict"; /** * Type checking functions with improved TypeScript support */ Object.defineProperty(exports, "__esModule", { value: true }); exports.isEqual = exports.isEmpty = exports.isNaN = exports.isInteger = exports.isFinite = exports.isRegExp = exports.isDate = exports.isPlainObject = exports.isObject = exports.isArray = exports.isFunction = exports.isBoolean = exports.isString = exports.isNumber = exports.isNull = exports.isUndefined = exports.isNil = void 0; /** * Checks if value is null or undefined */ const isNil = (value) => value === null || value === undefined; exports.isNil = isNil; /** * Checks if value is undefined */ const isUndefined = (value) => value === undefined; exports.isUndefined = isUndefined; /** * Checks if value is null */ const isNull = (value) => value === null; exports.isNull = isNull; /** * Checks if value is a number * Unlike Lodash, this correctly returns false for NaN */ const isNumber = (value) => typeof value === 'number' && !Number.isNaN(value); exports.isNumber = isNumber; /** * Checks if value is a string */ const isString = (value) => typeof value === 'string'; exports.isString = isString; /** * Checks if value is a boolean */ const isBoolean = (value) => typeof value === 'boolean'; exports.isBoolean = isBoolean; /** * Checks if value is a function */ // eslint-disable-next-line @typescript-eslint/ban-types const isFunction = (value) => typeof value === 'function'; exports.isFunction = isFunction; /** * Checks if value is an array */ exports.isArray = Array.isArray; /** * Checks if value is an object (excluding null) * This is different from Lodash as it doesn't consider arrays as objects */ const isObject = (value) => value !== null && typeof value === 'object' && !(0, exports.isArray)(value); exports.isObject = isObject; /** * Checks if value is a plain object (created by Object constructor or object literal) */ const isPlainObject = (value) => { if (value === null || typeof value !== 'object') return false; const proto = Object.getPrototypeOf(value); return proto === null || proto === Object.prototype; }; exports.isPlainObject = isPlainObject; /** * Checks if value is a Date object */ const isDate = (value) => value instanceof Date; exports.isDate = isDate; /** * Checks if value is a RegExp object */ const isRegExp = (value) => value instanceof RegExp; exports.isRegExp = isRegExp; /** * Checks if value is a valid finite number */ const isFinite = (value) => (0, exports.isNumber)(value) && Number.isFinite(value); exports.isFinite = isFinite; /** * Checks if value is an integer */ const isInteger = (value) => (0, exports.isNumber)(value) && Number.isInteger(value); exports.isInteger = isInteger; /** * Checks if value is NaN */ const isNaN = (value) => Number.isNaN(value); exports.isNaN = isNaN; /** * Checks if value is empty * Works with strings, arrays, objects, maps, sets */ const isEmpty = (value) => { if ((0, exports.isNil)(value)) return true; if ((0, exports.isString)(value) || (0, exports.isArray)(value)) return value.length === 0; if (value instanceof Map || value instanceof Set) return value.size === 0; if ((0, exports.isObject)(value)) return Object.keys(value).length === 0; return false; }; exports.isEmpty = isEmpty; /** * Checks if two values are equal (deep comparison) */ const isEqual = (value, other) => { // Same reference or both NaN if (value === other || ((0, exports.isNaN)(value) && (0, exports.isNaN)(other))) return true; // Different types or null/undefined if ((0, exports.isNil)(value) || (0, exports.isNil)(other) || typeof value !== typeof other) return false; // Compare arrays if ((0, exports.isArray)(value) && (0, exports.isArray)(other)) { if (value.length !== other.length) return false; return value.every((item, i) => (0, exports.isEqual)(item, other[i])); } // Compare dates if ((0, exports.isDate)(value) && (0, exports.isDate)(other)) { return value.getTime() === other.getTime(); } // Compare regexps if ((0, exports.isRegExp)(value) && (0, exports.isRegExp)(other)) { return value.toString() === other.toString(); } // Compare objects if ((0, exports.isObject)(value) && (0, exports.isObject)(other)) { const valueKeys = Object.keys(value); const otherKeys = Object.keys(other); if (valueKeys.length !== otherKeys.length) return false; return valueKeys.every(key => { return Object.prototype.hasOwnProperty.call(other, key) && (0, exports.isEqual)(value[key], other[key]); }); } return false; }; exports.isEqual = isEqual;