Skip to content

Instantly share code, notes, and snippets.

@simonewebdesign
Created September 25, 2017 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonewebdesign/8ce84663f0486417114bb5c8237ca0e0 to your computer and use it in GitHub Desktop.
Save simonewebdesign/8ce84663f0486417114bb5c8237ca0e0 to your computer and use it in GitHub Desktop.
Useful JS functions: camelize and parameterize
// Converts 'foo-bar' to 'fooBar'
// Credits: https://stackoverflow.com/a/6661012
export function camelize(str) {
return str.replace(/-([a-z])/g, g => g[1].toUpperCase());
}
// Converts 'fooBar' to 'foo-bar'
// Credits: https://gist.github.com/youssman/745578062609e8acac9f
export function parameterize(str) {
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}
describe('camelize', () => {
it('converts a hyphenated string to camelCase', () => {
expect(camelize('foo')).to.eq('foo');
expect(camelize('foo-bar')).to.eq('fooBar');
expect(camelize('foo-bar-baz')).to.eq('fooBarBaz');
});
});
describe('parameterize', () => {
it('converts a camelCase string to hyphenated', () => {
expect(parameterize('foo')).to.eq('foo');
expect(parameterize('fooBar')).to.eq('foo-bar');
expect(parameterize('fooBarBaz')).to.eq('foo-bar-baz');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment