Case ConvertersLoading saved progress…

Case Converters

Case conversion turns a string from one naming style into another — camelCase, PascalCase, snake_case, or kebab-case — by first splitting it into its underlying words, then re-casing and re-joining them for the target style. Build caseConverters, an object with four methods, where each one accepts a string in any style and returns it in its own. The point of the exercise is the splitting step: your tokenizer has to find word boundaries that hide in separators (foo_bar), in capitalization (fooBar), and in acronym runs (XMLHttpRequest). Do not call lodash — write the tokenizer and the four formatters yourself.

Signature

// caseConverters is an object with four methods; each takes a string
// in ANY style and returns it re-cased for the target style.
caseConverters.camelCase(str);  // first word lower, rest Capitalized -> "fooBar"
caseConverters.pascalCase(str); // every word Capitalized             -> "FooBar"
caseConverters.snakeCase(str);  // lowercase words joined with "_"     -> "foo_bar"
caseConverters.kebabCase(str);  // lowercase words joined with "-"     -> "foo-bar"

Examples

// Any input style converts to any target style.
caseConverters.camelCase('foo_bar');   // 'fooBar'    (from snake_case)
caseConverters.camelCase('foo-bar');   // 'fooBar'    (from kebab-case)
caseConverters.camelCase('Foo Bar');   // 'fooBar'    (from spaced words)
caseConverters.snakeCase('fooBar');    // 'foo_bar'   (from camelCase)
caseConverters.kebabCase('FooBar');    // 'foo-bar'   (from PascalCase)
caseConverters.pascalCase('foo bar');  // 'FooBar'
// Acronym runs, digits, and edge cases.
caseConverters.camelCase('XMLHttpRequest'); // 'xmlHttpRequest'
caseConverters.kebabCase('XMLHttpRequest'); // 'xml-http-request'
caseConverters.snakeCase('foo2Bar');        // 'foo2_bar'
caseConverters.snakeCase('foo_bar');        // 'foo_bar'  (idempotent)
caseConverters.camelCase('');               // ''

Notes

  • Accept every input style. Each method must work whether the input is spaced, snake_case, kebab-case, camelCase, PascalCase, or a mix. Route them all through one shared tokenizer, then format.
  • Boundaries hide in the casing. A lowercase-to-uppercase transition is a word break: fooBar is two words even though there is no separator. Insert a boundary there before you split.
  • Acronym runs split before the last capital. XMLHttpRequest is three words — xml, http, request — matching lodash. The last capital of a run belongs to the next word, so do not split every capital.
  • Digits stay attached to their word. Treat foo2Bar as foo2 and bar, giving foo2_bar. There is no boundary between a letter and a following digit — pick this policy and test it.
  • Collapse separators and handle empties. Leading, trailing, and repeated separators (--foo--bar--) must not leave stray hyphens or underscores, and an empty or separator-only string returns ''.
  • Do not call lodash. Reimplement camelCase / snakeCase / kebabCase and add pascalCase from scratch.

FAQ

How do you convert a string from one case to another?
Do not convert directly between styles. First tokenize the input into a list of lowercase words, splitting on separators AND on camelCase boundaries, then join and re-case those words for the target style. One shared tokenizer feeds all four converters.
How does the tokenizer split an acronym like XMLHttpRequest?
It inserts a word boundary before the last capital of an uppercase run that is followed by a lowercase letter, so XMLHttpRequest becomes the words xml, http, and request. That matches lodash, which keeps the acronym together instead of splitting every capital.
How are digits handled when converting case?
A digit stays attached to the alphanumeric run it sits in, so foo2Bar tokenizes to foo2 and bar, giving foo2_bar in snake_case. There is no boundary inserted between a letter and a following digit.
Loading editor…