UNPKG

funtool

Version:

A modern, efficient, and modular JavaScript utility library designed to enhance developer productivity.

494 lines (468 loc) 15.4 kB
/** * Check if the given value is an array * @param v - Any type value * @returns Returns true if the value is an array, otherwise returns false * @example * isArray([]) // => ✅ true * isArray({}) // => ❌ false */ declare function isArray<T = any>(v: any): v is T[]; /** * Check if the given value is an ArrayBuffer * @param v - Any type value * @returns Returns true if the value is an ArrayBuffer, otherwise returns false * @example * isArrayBuffer(new ArrayBuffer(8)) // => ✅ true * isArrayBuffer([]) // => ❌ false */ declare function isArrayBuffer(v: any): v is ArrayBuffer; /** * Check if the given value is a Blob * @param v - Any type value * @returns Returns true if the value is a Blob, otherwise returns false * @example * isBlob(new Blob()) // => ✅ true * isBlob({}) // => ❌ false */ declare function isBlob(v: any): v is Blob; /** * Check if the given value is a boolean * @param v - Any type value * @returns Returns true if the value is a boolean, otherwise returns false * @example * isBoolean(true) // => ✅ true * isBoolean('true') // => ❌ false */ declare function isBoolean(v: any): v is boolean; /** * Check if the given value is a Buffer * @param v - Any type value * @returns Returns true if the value is a Buffer, otherwise returns false * @example * isBuffer(Buffer.from('test')) // => ✅ true * isBuffer('test') // => ❌ false */ declare function isBuffer(v: any): boolean; /** * Check if the given value is a DataView * @param v - Any type value * @returns Returns true if the value is a DataView, otherwise returns false * @example * isDataView(new DataView(new ArrayBuffer(1))) // => ✅ true * isDataView([]) // => ❌ false */ declare function isDataView(v: any): v is DataView; /** * Check if the given value is a Date object * @param v - Any type value * @returns Returns true if the value is a Date object, otherwise returns false * @example * isDate(new Date()) // => ✅ true * isDate('2023-01-01') // => ❌ false */ declare function isDate(v: any): v is Date; /** * Checks whether the given value is a DOM Element. * * This function uses the `instanceof Element` check, which is the most reliable * and type-safe way to determine if a value is an Element in the browser environment. * * @template T - The input value type. * @param value - The value to check. * @returns {value is T & Element} - Returns true if the value is a DOM Element, * and narrows the type to `T & Element`. * * @example * const el = document.createElement('div'); * if (isElement(el)) { * // `el` is now inferred as `Element`, with all its methods and properties. * el.classList.add('active'); * } * * const notEl = {}; * if (!isElement(notEl)) { * console.log('Not a DOM element'); * } */ declare function isElement<T = unknown>(value: T): value is T & Element; /** * Checks if the value is a Text node. * * @template T * @param value - The value to check. * @returns {value is T & Text} * * @example * isTextNode(document.createTextNode('Hello')); // true */ declare function isTextNode<T = unknown>(value: T): value is T & Text; /** * Checks if the value is a Comment node. * * @template T * @param value - The value to check. * @returns {value is T & Comment} * * @example * isCommentNode(document.createComment('test')); // true */ declare function isCommentNode<T = unknown>(value: T): value is T & Comment; /** * Checks if the value is a Document node. * * @template T * @param value - The value to check. * @returns {value is T & Document} * * @example * isDocument(document); // true */ declare function isDocument<T = unknown>(value: T): value is T & Document; /** * Checks if the value is a DocumentFragment. * * @template T * @param value - The value to check. * @returns {value is T & DocumentFragment} * * @example * isDocumentFragment(document.createDocumentFragment()); // true */ declare function isDocumentFragment<T = unknown>(value: T): value is T & DocumentFragment; /** * Checks if the value is a general DOM Node. * * @template T * @param value - The value to check. * @returns {value is T & Node} * * @example * isNode(document.createTextNode('Hi')); // true * isNode(document.createElement('div')); // true */ declare function isNode<T = unknown>(value: T): value is T & Node; /** * Checks if the value is a global window object. * * @template T * @param value - The value to check. * @returns {value is T & Window} * * @example * isWindow(window); // true */ declare function isWindow<T = unknown>(value: T): value is T & Window; /** * Checks if a value is empty. * * @param v - The value to check. * @returns Returns `true` if the value is considered empty, otherwise `false`. * * @example * // ✅ Returns true * isEmpty(null); * isEmpty(undefined); * isEmpty(''); * isEmpty(' '); * isEmpty(NaN); * isEmpty([]); * isEmpty({}); * isEmpty(new Date('invalid date')); * * @example * // ❌ Returns false * isEmpty('hello'); * isEmpty(123); * isEmpty([1, 2, 3]); * isEmpty({ key: 'value' }); * isEmpty(new Date()); */ declare function isEmpty(v: any): boolean; /** * Deeply compares two values for equality, handling objects, arrays, dates, regex, maps, sets, symbols, and BigInt. * * @param {any} a - The first value to compare. * @param {any} b - The second value to compare. * @param {boolean | { ordered: boolean }} [options=true] - If true, arrays are compared in order, otherwise unordered. * @param {WeakMap<any, any>} [seen=new WeakMap()] - A map used to track circular references during comparison. * @returns {boolean} - Returns true if the values are deeply equal, false otherwise. * * @example * // Comparing primitive values * console.log(isEqual(5, 5)); // ✅ true * console.log(isEqual(5, '5')); // ❌ false * * @example * // Comparing arrays (ordered) * console.log(isEqual([1, 2, 3], [1, 2, 3])); // ✅ true * console.log(isEqual([1, 2, 3], [3, 2, 1])); // ❌ false (because default ordered is true) * * @example * // Comparing arrays (unordered) * console.log(isEqual([1, 2, 3], [3, 2, 1], { ordered: false })); // ✅ true * * @example * // Comparing objects (unordered) * console.log(isEqual({ a: 1, b: 2 }, { b: 2, a: 1 })); // ✅ true * console.log(isEqual({ a: 1, b: 2 }, { a: 1, b: 3 })); // ❌ false * * @example * // Comparing objects with nested objects * console.log(isEqual({ a: { b: 2 } }, { a: { b: 2 } })); // ✅ true * console.log(isEqual({ a: { b: 2 } }, { a: { b: 3 } })); // ❌ false * * @example * // Comparing Dates * console.log(isEqual(new Date('2025-05-01'), new Date('2025-05-01'))); // ✅ true * console.log(isEqual(new Date('2025-05-01'), new Date('2025-05-02'))); // ❌ false * * @example * // Comparing Maps * const map1 = new Map([['key1', 'value1'], ['key2', 'value2']]); * const map2 = new Map([['key1', 'value1'], ['key2', 'value2']]); * console.log(isEqual(map1, map2)); // ✅ true * * @example * // Comparing Sets * const set1 = new Set([1, 2, 3]); * const set2 = new Set([1, 2, 3]); * console.log(isEqual(set1, set2)); // ✅ true * * @example * // Comparing Symbol * const sym1 = Symbol('sym'); * const sym2 = Symbol('sym'); * sym1.description === sym2.description ✅ true; * console.log(isEqual(sym1, sym2)); // ✅ true (compared symbol description) * * @example * // Comparing BigInt * console.log(isEqual(BigInt(123), BigInt(123))); // ✅ true * console.log(isEqual(BigInt(123), BigInt(124))); // ❌ false */ declare function isEqual(a: any, b: any, options?: boolean | { ordered: boolean; }, seen?: WeakMap<object, object>): boolean; /** * Check if the given value is a function * @param v - Any type value * @returns Returns true if the value is a function, otherwise returns false * @example * isFunction(() => {}) // => ✅ true * isFunction({}) // => ❌ false */ declare function isFunction(v: any): v is Function; /** * Check if the given value is a Map * @param v - Any type value * @returns Returns true if the value is a Map, otherwise returns false * @example * isMap(new Map()) // => ✅ true * isMap({}) // => ❌ false */ declare function isMap<K = any, V = any>(v: any): v is Map<K, V>; /** * Check if the given value is a WeakMap * @param v - Any type value * @returns Returns true if the value is a WeakMap, otherwise returns false * @example * isWeakMap(new WeakMap()) // => ✅ true * isWeakMap(new Map()) // => ❌ false */ declare function isWeakMap<T = any>(v: any): v is WeakMap<WeakKey, T>; /** * Check if the current environment is Node.js * @returns Returns true if the environment is Node.js, otherwise returns false * @example * isNodeJS() // => ✅ true (in Node.js) * isNodeJS() // => ❌ false (in browser) */ declare function isNodeJS(): boolean; /** * Check if the given value is null * @param v - Any type value * @returns Returns true if the value is null, otherwise returns false * @example * isNull(null) // => true ✅ * isNull(undefined) // => false ❌ */ declare function isNull(v: any): boolean; /** * Check if the given value is null or undefined * @param v - Any type value * @returns Returns true if the value is null or undefined, otherwise returns false * @example * isNil(null) // => ✅ true * isNil(undefined) // => ✅ true * isNil(0) // => ❌ false */ declare function isNil(v: any): boolean; /** * Check if the given value is a number * @param v - Any type value * @returns Returns true if the value is a number, otherwise returns false * @example * isNumber(123) // => true ✅ * isNumber('123') // => false ❌ */ declare function isNumber(v: any): v is number; /** * Check if the given value is an integer * @param v - Any type value * @returns Returns true if the value is an integer, otherwise returns false * @example * isInt(123) // => true ✅ * isInt(123.1) // => false ❌ */ declare function isInt(v: any): boolean; /** * Check if the given value is a bigint * @param v - Any type value * @returns Returns true if the value is a bigint, otherwise returns false * @example * isBigInt(123n) // => true ✅ * isBigInt(123) // => false ❌ */ declare function isBigInt(v: any): boolean; /** * Check if the given value is a float * @param v - Any type value * @returns Returns true if the value is a float, otherwise returns false * @example * isFloat(123.1) // => true ✅ * isFloat(123) // => false ❌ */ declare function isFloat(v: any): boolean; /** * Check if the given value is NaN * @param v - Any type value * @returns Returns true if the value is NaN, otherwise returns false * @example * isNaN(NaN) // => ✅ true * isNaN(123) // => ❌ false * isNaN('abc') // => ❌ false */ declare function isNaN(v: any): v is number; /** * Check if the given value is an object * @param v - Any type value * @returns Returns true if the value is an object, otherwise returns false * @example * isObject({}) // => true ✅ * isObject([]) // => true ✅ * isObject(() => {}) // => true ✅ * isObject(null) // => false ❌ */ declare function isObject(v: any): boolean; /** * Check if the given value is an object like * @param v - Any type value * @returns Returns true if the value is an object like, otherwise returns false * @example * isObjectLike({}) // => true ✅ * isObjectLike([]) // => true ✅ * isObjectLike(() => {}) // => false ❌ * isObjectLike(null) // => false ❌ */ declare function isObjectLike(v: any): boolean; /** * Check if the given value is a plain object * @param v - Any type value * @returns Returns true if the value is a plain object, otherwise returns false * @example * isPlainObject({}) // => ✅ true * isPlainObject(new Date()) // => ❌ false */ declare function isPlainObject(v: any): boolean; /** * Check if the given value is a primitive type * @param v - Any type value * @returns Returns true if the value is a primitive, otherwise returns false * @example * isPrimitive(123) // => ✅ true * isPrimitive({}) // => ❌ false */ declare function isPrimitive(v: any): boolean; /** * Check if the given value is a Promise * @param v - Any type value * @returns Returns true if the value is a Promise, otherwise returns false * @example * isPromise(Promise.resolve()) // => ✅ true * isPromise({}) // => ❌ false */ declare function isPromise(v: any): boolean; /** * Check if the given value is a regular expression * @param v - Any type value * @returns Returns true if the value is a regular expression, otherwise returns false * @example * isRegExp(/abc/) // => ✅ true * isRegExp('abc') // => ❌ false */ declare function isRegExp(v: any): v is RegExp; /** * Check if the given value is a Set * @param v - Any type value * @returns Returns true if the value is a Set, otherwise returns false * @example * isSet(new Set()) // => true ✅ * isSet({}) // => false ❌ */ declare function isSet<T = any>(v: any): v is Set<T>; /** * Check if the given value is a WeakSet * @param v - Any type value * @returns Returns true if the value is a WeakSet, otherwise returns false * @example * isWeakSet(new WeakSet()) // => ✅ true * isWeakSet(new Set()) // => ❌ false */ declare function isWeakSet(v: any): v is WeakSet<WeakKey>; /** * Check if the given value is a string * @param v - Any type value * @returns Returns true if the value is a string, otherwise returns false * @example * isString('hello') // => ✅ true * isString(123) // => ❌ false */ declare function isString(v: any): v is string; /** * Check if the given value is a Symbol * @param v - Any type value * @returns Returns true if the value is a Symbol, otherwise returns false * @example * isSymbol(Symbol()) // => ✅ true * isSymbol('symbol') // => ❌ false */ declare function isSymbol(v: any): v is symbol; type TypedArrayTypes = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | BigUint64Array | Int8Array | Int16Array | Int32Array | BigInt64Array | Float32Array | Float64Array; /** * Check if the given value is a TypedArray * @param v - Any type value * @returns Returns true if the value is a TypedArray, otherwise returns false * @example * isTypedArray(new Uint8Array()) // => ✅ true * isTypedArray([]) // => ❌ false */ declare function isTypedArray(v: any): v is TypedArrayTypes; /** * Check if the given value is undefined * @param v - Any type value * @returns Returns true if the value is undefined, otherwise returns false * @example * isUndefined(undefined) // => ✅ true * isUndefined(null) // => ❌ false */ declare function isUndefined(v: any): boolean; /** * Get the type of the given value * @param v - Any type value * @returns Returns the type of the value as a string * @example * typeOf(123) // => ✅ 'number' * typeOf({}) // => ✅ 'object' */ declare function typeOf(v: any): string; export { isArray, isArrayBuffer, isBigInt, isBlob, isBoolean, isBuffer, isCommentNode, isDataView, isDate, isDocument, isDocumentFragment, isElement, isEmpty, isEqual, isFloat, isFunction, isInt, isMap, isNaN, isNil, isNode, isNodeJS, isNull, isNumber, isObject, isObjectLike, isPlainObject, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isTextNode, isTypedArray, isUndefined, isWeakMap, isWeakSet, isWindow, typeOf };