remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 2.59 kB
Source Map (JSON)
{"version":3,"file":"isEmpty.cjs","names":[],"sources":["../src/isEmpty.ts"],"sourcesContent":["import type { IterableContainer } from \"./internal/types/IterableContainer\";\n\n/**\n * A function that checks if the passed parameter is empty.\n *\n * This function has *limited* utility at the type level because **negating** it\n * does not yield a useful type in most cases because of TypeScript\n * limitations. Additionally, utilities which accept a narrower input type\n * provide better type-safety on their inputs. In most cases, you should use\n * one of the following functions instead:\n * * `isEmptyish` - supports a wider range of cases, accepts any input including nullish values, and does a better job at narrowing the result.\n * * `hasAtLeast` - when the input is just an array/tuple.\n * * `isStrictEqual` - when you just need to check for a specific literal value.\n * * `isNullish` - when you just care about `null` and `undefined`.\n * * `isTruthy` - when you need to also filter `number` and `boolean`.\n *\n * @param data - The variable to check.\n * @signature\n * R.isEmpty(data)\n * @example\n * R.isEmpty(''); //=> true\n * R.isEmpty([]); //=> true\n * R.isEmpty({}); //=> true\n *\n * R.isEmpty('test'); //=> false\n * R.isEmpty([1, 2, 3]); //=> false\n * R.isEmpty({ a: \"hello\" }); //=> false\n *\n * R.isEmpty(undefined); // Deprecated: use `isEmptyish`\n * @category Guard\n */\nexport function isEmpty(data: IterableContainer): data is [];\nexport function isEmpty<T extends object>(\n data: T,\n): data is Record<keyof T, never>;\nexport function isEmpty<T extends string>(\n data: T,\n): data is \"\" extends T ? \"\" : never;\n\n// TODO [>2]: remove this overload once we stop supporting `undefined`.\n// eslint-disable-next-line jsdoc/require-example, jsdoc/require-param, jsdoc/require-description\n/**\n * @deprecated Use `isEmptyish` instead!\n */\nexport function isEmpty<T extends string | undefined>(\n data: T,\n): data is\n | (\"\" extends T ? \"\" : never)\n | (undefined extends T ? undefined : never);\n\nexport function isEmpty(data: object | string | undefined): boolean {\n if (\n data === \"\" ||\n // TODO [>2]: remove `undefined` support! (don't forget to update the jsdoc example too!)\n data === undefined\n ) {\n return true;\n }\n\n if (Array.isArray(data)) {\n return data.length === 0;\n }\n\n return Object.keys(data).length === 0;\n}\n"],"mappings":"AAkDA,SAAgB,EAAQ,EAA4C,CAalE,OAXE,IAAS,IAET,IAAS,IAAA,GAEF,GAGL,MAAM,QAAQ,EAAK,CACd,EAAK,SAAW,EAGlB,OAAO,KAAK,EAAK,CAAC,SAAW"}