mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 10:17:48 +00:00
Simplify _.camelCase and add tests.
This commit is contained in:
@@ -6947,14 +6947,11 @@
|
|||||||
* _.camelCase('__hello_world__');
|
* _.camelCase('__hello_world__');
|
||||||
* // => 'helloWorld'
|
* // => 'helloWorld'
|
||||||
*/
|
*/
|
||||||
var camelCase = createCompounder(function(result, word, index, words) {
|
var camelCase = createCompounder(function(result, word, index) {
|
||||||
if (!index && reAllCaps.test(word)) {
|
if (!index && reAllCaps.test(word)) {
|
||||||
return result + word.toLowerCase();
|
return result + word.toLowerCase();
|
||||||
}
|
}
|
||||||
var lastWord = index && words[index - 1],
|
return result + (word.charAt(0)[index ? 'toUpperCase' : 'toLowerCase']() + word.slice(1));
|
||||||
isCapped = index && !(index > 1 && words.length == 3 && (lastWord == 2 || lastWord == 4));
|
|
||||||
|
|
||||||
return result + (word.charAt(0)[isCapped ? 'toUpperCase' : 'toLowerCase']() + word.slice(1));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
11
test/test.js
11
test/test.js
@@ -1068,16 +1068,15 @@
|
|||||||
QUnit.module('lodash.camelCase');
|
QUnit.module('lodash.camelCase');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
test('should work with numbers', 6, function() {
|
test('should work with numbers', 3, function() {
|
||||||
equal(_.camelCase('P2P'), 'p2p');
|
equal(_.camelCase('too legit 2 quit'), 'tooLegit2Quit');
|
||||||
equal(_.camelCase('ascii 2 unicode'), 'ascii2unicode');
|
|
||||||
equal(_.camelCase('two 4 one'), 'two4one');
|
|
||||||
equal(_.camelCase('walk 500 miles'), 'walk500Miles');
|
equal(_.camelCase('walk 500 miles'), 'walk500Miles');
|
||||||
equal(_.camelCase('xhr2 request'), 'xhr2Request');
|
equal(_.camelCase('xhr2 request'), 'xhr2Request');
|
||||||
equal(_.camelCase('too legit 2 quit'), 'tooLegit2Quit');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should handle acronyms', 1, function() {
|
test('should handle acronyms', 3, function() {
|
||||||
|
equal(_.camelCase('safe HTML'), 'safeHTML');
|
||||||
|
equal(_.camelCase('escape HTML entities'), 'escapeHTMLEntities');
|
||||||
equal(_.camelCase('XMLHttpRequest'), 'xmlHttpRequest');
|
equal(_.camelCase('XMLHttpRequest'), 'xmlHttpRequest');
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|||||||
Reference in New Issue
Block a user