Skip to content

Instantly share code, notes, and snippets.

@kucherenko
Created December 7, 2020 11:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kucherenko/d89b867ff0d0877eb45e68d983968b7a to your computer and use it in GitHub Desktop.
Save kucherenko/d89b867ff0d0877eb45e68d983968b7a to your computer and use it in GitHub Desktop.
const slugify = string => string.toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]+/g, '');
const kebabToCamel = str => str.replace(/-./g, m => m.toUpperCase()[1]);
const camelToKebab = str => str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
const toPascalCase = str => (str.match(/[a-zA-Z0-9]+/g) || [])
.map(w => `${w.charAt(0).toUpperCase()}${w.slice(1)}`).join('');
// Examples
slugify('Chapter One: Once upon a time...'); // 'chapter-one-once-upon-a-time'
kebabToCamel('background-color'); // 'backgroundColor'
camelToKebab('backgroundColor'); // 'background-color'
toPascalCase('test-string'); // TestString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment