camelCase to kebab-case

EasyUtility Functions10 min4 tests

Implement a camelToKebab function that converts a camelCase string to kebab-case.

Requirements

  • Insert a hyphen before each uppercase letter and convert it to lowercase
  • Handle consecutive uppercase letters gracefully
  • Return an empty string for empty input

Example

camelToKebab("backgroundColor"); // "background-color"
camelToKebab("fontSize");        // "font-size"
camelToKebab("borderTopWidth");  // "border-top-width"
Hints (4)
  1. Look for uppercase letters in the string.
  2. A regular expression like /[A-Z]/g can match all uppercase letters.
  3. Use String.prototype.replace to insert a hyphen before each match.
  4. Convert the entire result to lowercase at the end.

Topics

  • Utility Functions

Asked at

Shopify · Stripe · Netflix

Join Us
blur