UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

1 lines 6.81 kB
{"version":3,"file":"toCamelCase.cjs","names":["words"],"sources":["../src/toCamelCase.ts"],"sourcesContent":["import type { CamelCase } from \"type-fest\";\nimport type { OptionalOptionsWithDefaults } from \"./internal/types/OptionalOptionsWithDefaults\";\nimport { words } from \"./internal/words\";\n\nconst LOWER_CASE_CHARACTER_RE = /[a-z]/u;\n\nconst DEFAULT_PRESERVE_CONSECUTIVE_UPPERCASE = true;\n\ntype CamelCaseOptions = {\n readonly preserveConsecutiveUppercase?: boolean;\n};\n\ntype CamelCaseOptionsWithDefaults<Options extends CamelCaseOptions> =\n OptionalOptionsWithDefaults<\n CamelCaseOptions,\n Options,\n {\n // We use the runtime const for the default type so they stay coupled.\n preserveConsecutiveUppercase: typeof DEFAULT_PRESERVE_CONSECUTIVE_UPPERCASE;\n }\n >;\n\n/**\n * Converts text to **camelCase** by splitting it into words, lowercasing the\n * first word, capitalizing the rest, then joining them back together.\n *\n * Because it uses the built-in case conversion methods, the function shares\n * their _[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),\n * [`toLocaleLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase),\n * and [`toLocaleUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase)\n * which are purpose-built to handle nuances in languages and locales.\n *\n * For other case manipulations see: `toLowerCase`, `toUpperCase`, `capitalize`,\n * `uncapitalize`, `toKebabCase`, `toSnakeCase`, and `toTitleCase`.\n *\n * For *PascalCase* use `capitalize(toCamelCase(data))`.\n *\n * @param data - A string.\n * @param options - An _optional_ object with the _optional_ property\n * `preserveConsecutiveUppercase` that can be used to change the way consecutive\n * uppercase characters are handled. Defaults to `true`.\n * @signature\n * R.toCamelCase(data);\n * R.toCamelCase(data, { preserveConsecutiveUppercase });\n * @example\n * R.toCamelCase(\"hello world\"); // \"helloWorld\"\n * R.toCamelCase(\"__HELLO_WORLD__\"); // \"helloWorld\"\n * R.toCamelCase(\"HasHTML\"); // \"hasHTML\"\n * R.toCamelCase(\"HasHTML\", { preserveConsecutiveUppercase: false }); // \"hasHtml\"\n * @dataFirst\n * @category String\n */\nexport function toCamelCase<T extends string, Options extends CamelCaseOptions>(\n data: T,\n options?: Options,\n): CamelCase<T, CamelCaseOptionsWithDefaults<Options>>;\n\n/**\n * Converts text to **camelCase** by splitting it into words, lowercasing the\n * first word, capitalizing the rest, then joining them back together.\n *\n * Because it uses the built-in case conversion methods, the function shares\n * their _[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),\n * [`toLocaleLowerCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase),\n * and [`toLocaleUpperCase`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase)\n * which are purpose-built to handle nuances in languages and locales.\n *\n * For other case manipulations see: `toLowerCase`, `toUpperCase`, `capitalize`,\n * `uncapitalize`, `toKebabCase`, `toSnakeCase`, and `toTitleCase`.\n *\n * For *PascalCase* use `capitalize(toCamelCase(data))`.\n *\n * @param options - An _optional_ object with the _optional_ property\n * `preserveConsecutiveUppercase` that can be used to change the way consecutive\n * uppercase characters are handled. Defaults to `true`.\n * @signature\n * R.toCamelCase()(data);\n * R.toCamelCase({ preserveConsecutiveUppercase })(data);\n * @example\n * R.pipe(\"hello world\", R.toCamelCase()); // \"helloWorld\"\n * R.pipe(\"__HELLO_WORLD__\", R.toCamelCase()); // \"helloWorld\"\n * R.pipe(\"HasHTML\", R.toCamelCase()); // \"hasHTML\"\n * R.pipe(\n * \"HasHTML\",\n * R.toCamelCase({ preserveConsecutiveUppercase: false }),\n * ); // \"hasHtml\"\n * @dataLast\n * @category String\n */\nexport function toCamelCase<Options extends CamelCaseOptions>(\n options?: Options,\n): <T extends string>(\n data: T,\n) => CamelCase<T, CamelCaseOptionsWithDefaults<Options>>;\n\nexport function toCamelCase(\n dataOrOptions: CamelCaseOptions | string,\n options?: CamelCaseOptions,\n): unknown {\n return typeof dataOrOptions === \"string\"\n ? toCamelCaseImplementation(dataOrOptions, options)\n : (data: string) => toCamelCaseImplementation(data, dataOrOptions);\n}\n\n// Based on the type definition from type-fest.\n// @see https://github.com/sindresorhus/type-fest/blob/main/source/camel-case.d.ts#L76-L80\nconst toCamelCaseImplementation = (\n data: string,\n {\n preserveConsecutiveUppercase = DEFAULT_PRESERVE_CONSECUTIVE_UPPERCASE,\n }: CamelCaseOptions = {},\n): string =>\n words(\n LOWER_CASE_CHARACTER_RE.test(data)\n ? data\n : // If the text doesn't have **any** lower case characters we also lower\n // case everything, but if it does we need to maintain them as it\n // affects the word boundaries.\n data.toLowerCase(),\n )\n .map(\n (word, index) =>\n `${\n // The first word is uncapitalized, the rest are capitalized\n index === 0 ? word[0]!.toLowerCase() : word[0]!.toUpperCase()\n }${preserveConsecutiveUppercase ? word.slice(1) : word.slice(1).toLowerCase()}`,\n )\n .join(\"\");\n"],"mappings":"wCAIM,EAA0B,SAE1B,EAAyC,GA+F/C,SAAgB,EACd,EACA,EACS,CACT,OAAO,OAAO,GAAkB,SAC5B,EAA0B,EAAe,EAAQ,CAChD,GAAiB,EAA0B,EAAM,EAAc,CAKtE,MAAM,GACJ,EACA,CACE,+BAA+B,IACX,EAAE,GAExBA,EAAAA,EACE,EAAwB,KAAK,EAAK,CAC9B,EAIA,EAAK,aAAa,CACvB,CACE,KACE,EAAM,IACL,GAEE,IAAU,EAAI,EAAK,GAAI,aAAa,CAAG,EAAK,GAAI,aAAa,GAC5D,EAA+B,EAAK,MAAM,EAAE,CAAG,EAAK,MAAM,EAAE,CAAC,aAAa,GAChF,CACA,KAAK,GAAG"}