camelCase
string
Converts string to camel case.
Installation
Import
import { camelCase } from '@tulx/utils';Source Code
Implementation
/**
* Converts string to camel case.
*
* @param string - The string to convert.
* @returns Returns the camel cased string.
*
* @example
* ```ts
* camelCase('Foo Bar'); // 'fooBar'
* camelCase('--foo-bar--'); // 'fooBar'
* camelCase('__FOO_BAR__'); // 'fooBar'
* ```
*/
export function camelCase(string: string): string {
return string
.replace(/[\s_-]+/g, ' ')
.replace(/[A-Z]+/g, (match) => ` ${match.toLowerCase()}`)
.trim()
.replace(/\s+(\w)/g, (_, char) => char.toUpperCase())
.replace(/^[A-Z]/, (char) => char.toLowerCase());
}
Example
import { camelCase } from '@tulx/utils';
camelCase('Foo Bar'); // 'fooBar'
camelCase('--foo-bar--'); // 'fooBar'
camelCase('__FOO_BAR__'); // 'fooBar'Related Functions
capitalize
Converts the first character of string to upper case and the remaining to lower case.
deburr
Deburrs string by converting Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and removing combining diacritical marks.
endsWith
Checks if string ends with the given target string.
escape
Converts the characters "&", "<", ">", '"', and "'" in string to their corresponding HTML entities.
escapeRegExp
Escapes the RegExp special characters "^", "$", "\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in string.
kebabCase
Converts string to kebab case.