UNPKG

apphouse

Version:

Component library for React that uses observable state management and theme-able components.

29 lines (25 loc) 949 B
/** * You can use this `camelCase` function to convert a string to camel case. For example: * * ```typescript * console.log(camelCase('foo bar')); // Output: 'fooBar' * console.log(camelCase('hello-world')); // Output: 'helloWorld' * console.log(camelCase('foo_bar_baz')); // Output: 'fooBarBaz' * ``` * @param str * @returns */ export function camelCase(str: string): string { // Break the string into individual words using non-alphanumeric characters as delimiters const words = str.split(/[^a-zA-Z0-9]+/); // Capitalize the first letter of each word after the first word const capitalizedWords = words.map((word, index) => { if (index === 0) { return word.toLowerCase(); } return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); }); // Join the capitalized words without any spaces or special characters const camelCasedStr = capitalizedWords.join(''); return camelCasedStr; }