remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 4.24 kB
Source Map (JSON)
{"version":3,"file":"toKebabCase.cjs","names":["purry","words"],"sources":["../src/toKebabCase.ts"],"sourcesContent":["import type { Join, Words } from \"type-fest\";\nimport { words } from \"./internal/words\";\nimport { purry } from \"./purry\";\n\ntype KebabCase<S extends string> = string extends S\n ? string\n : Lowercase<Join<Words<S>, \"-\">>;\n\n/**\n * Converts text to **kebab-case** by splitting it into words and joining them\n * back together with hyphens (`-`), then lowercasing the result.\n *\n * Because it uses [`toLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase),\n * the function shares its _[locale inaccuracies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase#description)_\n * too, making it best suited for simple strings like identifiers and internal\n * keys. For linguistic text processing, use [`Intl.Segmenter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter)\n * with [`granularity: \"word\"`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter#parameters), and\n * [`toLocaleLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase),\n * which are purpose-built to handle nuances in languages and locales.\n *\n * For other case manipulations see: `toLowerCase`, `toUpperCase`, `capitalize`,\n * `uncapitalize`, `toCamelCase`, `toSnakeCase`, and `toTitleCase`.\n *\n * For *COBOL-CASE* use `toUpperCase(toKebabCase(data))`.\n *\n * @param data - A string.\n * @signature\n * R.toKebabCase(data);\n * @example\n * R.toKebabCase(\"hello world\"); // \"hello-world\"\n * R.toKebabCase(\"__HELLO_WORLD__\"); // \"hello-world\"\n * @dataFirst\n * @category String\n */\nexport function toKebabCase<S extends string>(data: S): KebabCase<S>;\n\n/**\n * Converts text to **kebab-case** by splitting it into words and joining them\n * back together with hyphens (`-`), then lowercasing the result.\n *\n * Because it uses [`toLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase),\n * the function shares its _[locale inaccuracies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase#description)_\n * too, making it best suited for simple strings like identifiers and internal\n * keys. For linguistic text processing, use [`Intl.Segmenter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter)\n * with [`granularity: \"word\"`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter#parameters), and\n * [`toLocaleLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase),\n * which are purpose-built to handle nuances in languages and locales.\n *\n * For other case manipulations see: `toLowerCase`, `toUpperCase`, `capitalize`,\n * `uncapitalize`, `toCamelCase`, `toSnakeCase`, and `toTitleCase`.\n *\n * For *COBOL-CASE* use `toUpperCase(toKebabCase(data))`.\n *\n * @signature\n * R.toKebabCase()(data);\n * @example\n * R.pipe(\"hello world\", R.toKebabCase()); // \"hello-world\"\n * R.pipe(\"__HELLO_WORLD__\", R.toKebabCase()); // \"hello-world\"\n * @dataLast\n * @category String\n */\nexport function toKebabCase(): <S extends string>(data: S) => KebabCase<S>;\n\nexport function toKebabCase(...args: ReadonlyArray<unknown>): unknown {\n return purry(toKebabCaseImplementation, args);\n}\n\nconst toKebabCaseImplementation = <S extends string>(data: S): KebabCase<S> =>\n // @ts-expect-error [ts2322] -- To avoid importing our own utilities for this\n // we are using the built-in `join` and `toLowerCase` functions which aren't\n // typed as well. This is equivalent to `toLowerCase(join(words(data), \"-\"))`\n // which TypeScript infers correctly as KebabCase.\n words(data).join(\"-\").toLowerCase();\n"],"mappings":"0EA+DA,SAAgB,EAAY,GAAG,EAAuC,CACpE,OAAOA,EAAAA,EAAM,EAA2B,EAAK,CAG/C,MAAM,EAA+C,GAKnDC,EAAAA,EAAM,EAAK,CAAC,KAAK,IAAI,CAAC,aAAa"}