typedash
Version:
modern, type-safe collection of utility functions
1 lines • 1.97 kB
Source Map (JSON)
{"version":3,"sources":["../../src/functions/capitalize/capitalize.ts","../../src/functions/camelCase/camelCase.ts"],"names":[],"mappings":";AAUO,SAAS,WAA6B,QAA0B;AACrE,SAAO,GAAG,OAAO,OAAO,CAAC,EAAE,YAAY,CAAC,GAAG,OAAO,MAAM,CAAC,CAAC;AAC5D;;;ACIO,SAAS,UAA4B,QAAyB;AACnE,MAAI,CAAC,UAAU,KAAK,MAAM,GAAG;AAC3B,WAAO;AAAA,EACT;AAEA,QAAM,QAAQ,OAAO,KAAK,EAAE,MAAM,UAAU;AAC5C,SAAO,MACJ,IAAI,CAAC,MAAM,UAAW,UAAU,IAAI,KAAK,YAAY,IAAI,WAAW,IAAI,CAAE,EAC1E,KAAK,EAAE;AACZ;AAQA,IAAM,aAAa","sourcesContent":["/**\n * Returns a new string with the first character capitalized.\n * @param string The input string to capitalize.\n * @returns A new string with the first character capitalized.\n * @example\n * ```ts\n * capitalize('foo'); // 'Foo'\n * capitalize(''); // ''\n * ```\n */\nexport function capitalize<S extends string>(string: S): Capitalize<S> {\n return `${string.charAt(0).toUpperCase()}${string.slice(1)}` as Capitalize<S>;\n}\n","import type { CamelCase as CamelCaseImplementation } from 'type-fest';\n\nimport { capitalize } from '../capitalize';\n\n/**\n * Changes the casing of a string to kebab case.\n * @param string The input string to change the casing of.\n * @returns A new string with the casing changed to kebab case.\n * @example\n * ```ts\n * camelCase('fooBar') // 'fooFar'\n * camelCase('foo bar') // 'fooBar'\n * camelCase('foo-bar') // 'fooBar'\n * camelCase('fooBar42') // 'fooBar42'\n * ```\n */\nexport function camelCase<S extends string>(string: S): CamelCase<S> {\n if (!/[a-z]+/i.test(string)) {\n return string as CamelCase<S>;\n }\n\n const words = string.trim().split(wordsRegex);\n return words\n .map((word, index) => (index === 0 ? word.toLowerCase() : capitalize(word)))\n .join('') as CamelCase<S>;\n}\n\n/**\n * Changes the casing of a string to camel case.\n * @see {@link camelCase}.\n */\nexport type CamelCase<S extends string> = CamelCaseImplementation<S>;\n\nconst wordsRegex = /[\\s_-]+|(?<=[a-z])(?=[A-Z])/;\n"]}