Write three functions that convert text to a naming style: camelCase(text), snakeCase(text), and kebabCase(text). They accept text in any style, or a mix of styles, so each needs to find the words first.
A word is a run of letters and digits; letters include accented ones like é and û. Every other character (spaces, -, _, ., and so on) separates words, and several in a row count as one boundary.
Inside a run of letters and digits, a new word starts at an uppercase letter that follows a lowercase letter or a digit (backgroundColor, version2Beta), and at the last capital of a run of capitals that's followed by a lowercase letter (XMLHttpRequest is XML, Http, Request). A digit stays in the word it follows: html5 and top10 are single words.
camelCase writes the first word in lowercase and each later word with an uppercase first letter and the rest lowercase, with nothing between them. snakeCase writes every word in lowercase, joined with _, and kebabCase writes them in lowercase, joined with -. Text with no words gives "".
camelCase("background-color"); // "backgroundColor"
snakeCase("XMLHttpRequest"); // "xml_http_request"
kebabCase("FOO_BAR baz"); // "foo-bar-baz"
camelCase("version2 beta"); // "version2Beta"
expect(camelCase("hello big world")).toBe("helloBigWorld");
expect(snakeCase("hello big world")).toBe("hello_big_world");
expect(kebabCase("hello big world")).toBe("hello-big-world");
expect(kebabCase("backgroundColor")).toBe("background-color");
expect(snakeCase("FirstName")).toBe("first_name");
expect(camelCase("FirstName")).toBe("firstName");
expect(camelCase("--foo_bar baz-")).toBe("fooBarBaz");
expect(snakeCase("foo--bar__baz.qux")).toBe("foo_bar_baz_qux");
expect(kebabCase(" foo / bar ")).toBe("foo-bar");
expect(kebabCase("XMLHttpRequest")).toBe("xml-http-request");
expect(snakeCase("userID")).toBe("user_id");
expect(camelCase("parseHTMLString")).toBe("parseHtmlString");
expect(camelCase("FOO_BAR")).toBe("fooBar");
expect(kebabCase("FOO BAR")).toBe("foo-bar");
expect(camelCase("hello WORLD")).toBe("helloWorld");
expect(kebabCase("version2Beta")).toBe("version2-beta");
expect(snakeCase("html5 parser")).toBe("html5_parser");
expect(camelCase("top 10 list")).toBe("top10List");
expect(kebabCase("crèmeBrûlée")).toBe("crème-brûlée");
expect(camelCase("crème brûlée")).toBe("crèmeBrûlée");
expect(snakeCase("Ça Va")).toBe("ça_va");
expect(camelCase("")).toBe("");
expect(snakeCase(" -- ")).toBe("");
expect(kebabCase("__")).toBe("");
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with camelCase, snake_case, and kebab-case. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.