-
Notifications
You must be signed in to change notification settings - Fork 0
/
toCamelCase.ts
31 lines (30 loc) · 946 Bytes
/
toCamelCase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Converts a string to camelcase.
* @param str - String to convert.
* @returns Camelcased string.
* @example
* ```ts
* toCamelCase('Foo Bar'); // 'fooBar'
* toCamelCase('some_database_field_name'); // 'someDatabaseFieldName'
* toCamelCase('Some label that needs to be camelized');
* // 'someLabelThatNeedsToBeCamelized'
* toCamelCase('some-javascript-property'); // 'someJavascriptProperty'
* toCamelCase('some-mixed_string with spaces_underscores-and-hyphens');
* // 'someMixedStringWithSpacesUnderscoresAndHyphens'
* ```
* @since 0.1.16
* @category String
* @public
*/
const toCamelCase = (str: string) => {
const s =
str &&
str
.match(
/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g
)
?.map((x) => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase())
.join("");
return s ? s.slice(0, 1).toLowerCase() + s.slice(1) : str;
};
export default toCamelCase;