Created
September 25, 2017 10:11
-
-
Save simonewebdesign/8ce84663f0486417114bb5c8237ca0e0 to your computer and use it in GitHub Desktop.
Useful JS functions: camelize and parameterize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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