Created
December 28, 2017 01:56
-
-
Save jlarocque/819f189809537d7a8e93cd5a92625cca to your computer and use it in GitHub Desktop.
Underscore to camelCase
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
<script src='https://scottdalessandro.github.io/jasmine-total/js/jasmine-suite.min.js'></script> |
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
/* | |
Underscore To Camel Case | |
Write a function to convert a variable name from under_score format to camelCase. | |
Make sure you support an unlimited number of underscores in the input! | |
#### Examples: | |
```js | |
- INPUT: underToCamel("first_name"); | |
- OUTPUT: "firstName" | |
- INPUT: underToCamel("my_income_tax_from_2015"); | |
- OUTPUT: "myIncomeTaxFrom2015" | |
``` | |
*/ | |
// Write Code Below: | |
function underToCamel(str){ | |
str = str.toLowerCase(); | |
var array = (str.split('_')); | |
var newArray = []; | |
for (var index = 1; index < array.length; index ++){ | |
newArray.push(array[index].charAt(0).toUpperCase()+array[index].slice(1)); | |
} | |
return array[0] + newArray.join(''); | |
} | |
describe('underscore_toCamelCase', function(){ | |
it('underToCamel is a function', function(){ | |
expect(typeof underToCamel).toEqual('function'); | |
}); | |
it('underToCamel returns a number value', function(){ | |
expect(typeof underToCamel("html_css")).toEqual('string'); | |
}); | |
it('removes any "_" characters and makes the next character capitalized', function(){ | |
expect(underToCamel('new_york')).toEqual('newYork'); | |
expect(underToCamel('grace_hopper')).toEqual('graceHopper'); | |
}); | |
it('removes any "_" characters and makes the next character capitalized for multiple words', function(){ | |
expect(underToCamel('new_york_the_big_apple')).toEqual('newYorkTheBigApple'); | |
expect(underToCamel('u_s_a')).toEqual('uSA'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment