UNPKG

@cc-heart/utils

Version:

🔧 javascript common tools collection

155 lines (153 loc) • 4.18 kB
const _toString = Object.prototype.toString; /** * Checks if the given value is an object. * * @param val - The value to be checked. * @returns Returns true if the value is an object, otherwise false. */ function isObject(val) { return _toString.call(val) === '[object Object]'; } /** * Checks if the given value is an symbol. * * @param val - The value to be checked. * @returns Returns true if the value is an object, otherwise false. */ function isSymbol(val) { return typeof val === 'symbol'; } /** * Checks if the given value is a function. * * @param val - The value to be checked. * @returns Returns true if the value is a function, false otherwise. */ function isFn(val) { return typeof val === 'function'; } /** * Checks if the given value is a string. * * @param val - The value to be checked. * @returns Returns true if the value is a string, false otherwise. */ function isStr(val) { return typeof val === 'string'; } /** * Checks if the provided value is a boolean. * * @param val - The value to check. * @returns Returns true if the value is a boolean, false otherwise. */ function isBool(val) { return typeof val === 'boolean'; } /** * Checks if a value is undefined. * * @param val - The value to check. * @returns Returns true if the value is undefined, otherwise false. */ function isUndef(val) { return typeof val === 'undefined'; } /** * Checks if the given value is null. * * @param val - The value to check. * @returns Returns true if the value is null, false otherwise. */ function isNull(val) { return val === null; } const isNil = isNull; /** * Determines whether a value is a primitive. * * @param val - The value to check. * @returns Returns `true` if the value is a primitive, `false` otherwise. */ function isPrimitive(val) { return typeof val !== 'object' || val === null; } /** * Checks if a value is falsy. * * @param val - The value to check. * @returns Returns true if the value is falsy, otherwise false. */ function isFalsy(val) { return !val; } /** * Checks if the given value is a number. * * @param val - The value to be checked. * @returns Returns true if the value is a number, false otherwise. */ function isNumber(val) { return typeof val === 'number'; } /** * determines if it is a valid value other than NaN * @param val * @returns */ function isEffectiveNumber(val) { if (!isNumber(val)) return false; return !isNaN(val); } /** * Checks if a value is a Promise. * * @param val - The value to check. * @returns Returns `true` if the value is a Promise, else `false`. */ function isPromise(val) { return (typeof val === 'object' && !isNull(val) && (_toString.call(val) === '[object Promise]' || isFn(Reflect.get(val, 'then')))); } /** * Checks if the given object has its own property. * * @param obj - The object to check. * @param prop - The property to check. * @returns Returns true if the object has its own property, otherwise false. */ function hasOwn(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); } /** * An array is considered valid if it is an array and its length is greater than or equal to 0. * * @param arr - The array to be checked. * @returns Returns true if the array is valid, false otherwise. */ function isValidArray(arr) { return Array.isArray(arr) && arr.length > 0; } /** * Checks if a given value is a valid PropertyKey. * A PropertyKey is a string, number, or symbol that can be used as a property name. * * @param val - The value to check. * @returns True if the value is a PropertyKey, false otherwise. */ function isPropertyKey(val) { return isStr(val) || isNumber(val) || isSymbol(val); } /** * Checks if a given date is valid. * * @param date - The date to check. * @returns True if the date is valid, false otherwise. */ function isValidDate(date) { return date.toString() !== 'Invalid Date'; } export { _toString, hasOwn, isBool, isEffectiveNumber, isFalsy, isFn, isNil, isNull, isNumber, isObject, isPrimitive, isPromise, isPropertyKey, isStr, isSymbol, isUndef, isValidArray, isValidDate };