UNPKG

typedash

Version:

modern, type-safe collection of utility functions

1 lines 3.48 kB
{"version":3,"file":"isEmpty-Q-g73dbK.cjs","names":["isArray"],"sources":["../src/functions/isEmpty/isEmpty.ts"],"sourcesContent":["import type { EmptyObject, Maybe } from '../../types';\nimport { isArray } from '../isArray';\n\n/**\n * Returns whether the input value is empty.\n * @param value The value to check.\n * @returns Whether the input value is empty.\n * @example\n * ```ts\n * isEmpty('') // true\n * isEmpty('abc') // false\n * ```\n */\nexport function isEmpty(value: string): value is '';\n/**\n * Returns whether the input value is empty.\n * @param value The value to check.\n * @returns Whether the input value is empty.\n * @example\n * ```ts\n * isEmpty(0) // true\n * isEmpty(1) // false\n * ```\n */\nexport function isEmpty(value: number): value is number;\n/**\n * Returns whether the input value is empty.\n * @param value The value to check.\n * @returns Whether the input value is empty.\n * @example\n * ```ts\n * isEmpty(new Map()) // true\n * isEmpty(new Map([['a', 1]])) // false\n *\n * isEmpty(new Set()) // true\n * isEmpty(new Set([1])) // false\n * ```\n */\nexport function isEmpty(\n value: Maybe<ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>>\n): boolean;\n/**\n * Returns whether the input value is empty.\n * @param value The value to check.\n * @returns Whether the input value is empty.\n * @example\n * ```ts\n * isEmpty([]) // true\n * isEmpty([1]) // false\n * ```\n */\nexport function isEmpty<T extends readonly unknown[] | EmptyArray>(\n value: Maybe<T>\n): value is Maybe<T & EmptyArray>;\n/**\n * Returns whether the input value is empty.\n * @param value The value to check.\n * @returns Whether the input value is empty.\n * @example\n * ```ts\n * isEmpty({}) // true\n * isEmpty({ a: 1 }) // false\n * ```\n */\nexport function isEmpty<T extends object>(\n value: Maybe<EmptyObject<T> | T>\n): value is T & Record<string, never>;\n/**\n * Returns whether the input value is empty.\n * @param value The value to check.\n * @returns Whether the input value is empty.\n * @example\n * ```ts\n * isEmpty(null) // true\n * isEmpty(undefined) // true\n * isEmpty(0) // true\n * isEmpty('') // true\n * isEmpty([]) // true\n * isEmpty({}) // true\n * isEmpty(new Map()) // true\n * isEmpty(new Set()) // true\n * isEmpty(false) // true\n *\n * isEmpty(true) // false\n * isEmpty(1) // false\n * isEmpty('abc') // false\n * isEmpty([1]) // false\n * isEmpty({ a: 1 }) // false\n * isEmpty(new Map([['a', 1]])) // false\n * isEmpty(new Set([1])) // false\n * ```\n */\nexport function isEmpty<T>(value: Maybe<T>): boolean;\n/**\n * Implementation for all overloads.\n * @param value The value to check.\n * @returns Whether the input value is empty.\n */\nexport function isEmpty<T>(value: Maybe<T>): boolean {\n if (value == null) {\n return true;\n }\n\n if (isArray(value) || typeof value === 'string') {\n return value.length === 0;\n }\n\n if (value instanceof Map || value instanceof Set) {\n return value.size === 0;\n }\n\n if (typeof value === 'number') {\n return !value;\n }\n\n if (typeof value === 'object') {\n return Object.keys(value).length === 0;\n }\n\n return false;\n}\n\ntype EmptyArray = [];\n"],"mappings":";;;;;;;;AAkGA,SAAgB,QAAW,OAA0B;AACnD,KAAI,SAAS,KACX,QAAO;AAGT,KAAIA,wBAAQ,MAAM,IAAI,OAAO,UAAU,SACrC,QAAO,MAAM,WAAW;AAG1B,KAAI,iBAAiB,OAAO,iBAAiB,IAC3C,QAAO,MAAM,SAAS;AAGxB,KAAI,OAAO,UAAU,SACnB,QAAO,CAAC;AAGV,KAAI,OAAO,UAAU,SACnB,QAAO,OAAO,KAAK,MAAM,CAAC,WAAW;AAGvC,QAAO"}