Make _.camelCase process words as lower cased.

This commit is contained in:
John-David Dalton
2014-09-07 01:20:18 -07:00
parent 172eaf84b5
commit 3f64316e00
2 changed files with 31 additions and 40 deletions

View File

@@ -46,9 +46,6 @@
/** Used to generate unique IDs */ /** Used to generate unique IDs */
var idCounter = 0; var idCounter = 0;
/** Used to detect words composed of all capital letters */
var reAllCaps = /^[A-Z]+$/;
/** Used to match empty string literals in compiled template source */ /** Used to match empty string literals in compiled template source */
var reEmptyStringLeading = /\b__p \+= '';/g, var reEmptyStringLeading = /\b__p \+= '';/g,
reEmptyStringMiddle = /\b(__p \+=) '' \+/g, reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
@@ -102,7 +99,7 @@
var reUnescapedString = /['\n\r\u2028\u2029\\]/g; var reUnescapedString = /['\n\r\u2028\u2029\\]/g;
/** Used to match words to create compound words */ /** Used to match words to create compound words */
var reWords = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]+|[0-9]+/g; var reWords = /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*)|[A-Z]?[a-z]+[0-9]*|[A-Z]+|[0-9]+/g;
/** Used to detect and test whitespace */ /** Used to detect and test whitespace */
var whitespace = ( var whitespace = (
@@ -245,22 +242,22 @@
* for more details. * for more details.
*/ */
var deburredLetters = { var deburredLetters = {
'\xC0': 'A', '\xC1': 'A', '\xC2': 'A', '\xC3': 'A', '\xC4': 'A', '\xC5': 'A', '\xC0': 'a', '\xC1': 'a', '\xC2': 'a', '\xC3': 'a', '\xC4': 'a', '\xC5': 'a',
'\xE0': 'a', '\xE1': 'a', '\xE2': 'a', '\xE3': 'a', '\xE4': 'a', '\xE5': 'a', '\xE0': 'a', '\xE1': 'a', '\xE2': 'a', '\xE3': 'a', '\xE4': 'a', '\xE5': 'a',
'\xC7': 'C', '\xE7': 'c', '\xC7': 'c', '\xE7': 'c',
'\xD0': 'D', '\xF0': 'd', '\xD0': 'd', '\xF0': 'd',
'\xC8': 'E', '\xC9': 'E', '\xCA': 'E', '\xCB': 'E', '\xC8': 'e', '\xC9': 'e', '\xCA': 'e', '\xCB': 'e',
'\xE8': 'e', '\xE9': 'e', '\xEA': 'e', '\xEB': 'e', '\xE8': 'e', '\xE9': 'e', '\xEA': 'e', '\xEB': 'e',
'\xCC': 'I', '\xCD': 'I', '\xCE': 'I', '\xCF': 'I', '\xCC': 'i', '\xCD': 'i', '\xCE': 'i', '\xCF': 'i',
'\xEC': 'i', '\xED': 'i', '\xEE': 'i', '\xEF': 'i', '\xEC': 'i', '\xED': 'i', '\xEE': 'i', '\xEF': 'i',
'\xD1': 'N', '\xF1': 'n', '\xD1': 'n', '\xF1': 'n',
'\xD2': 'O', '\xD3': 'O', '\xD4': 'O', '\xD5': 'O', '\xD6': 'O', '\xD8': 'O', '\xD2': 'o', '\xD3': 'o', '\xD4': 'o', '\xD5': 'o', '\xD6': 'o', '\xD8': 'o',
'\xF2': 'o', '\xF3': 'o', '\xF4': 'o', '\xF5': 'o', '\xF6': 'o', '\xF8': 'o', '\xF2': 'o', '\xF3': 'o', '\xF4': 'o', '\xF5': 'o', '\xF6': 'o', '\xF8': 'o',
'\xD9': 'U', '\xDA': 'U', '\xDB': 'U', '\xDC': 'U', '\xD9': 'u', '\xDA': 'u', '\xDB': 'u', '\xDC': 'u',
'\xF9': 'u', '\xFA': 'u', '\xFB': 'u', '\xFC': 'u', '\xF9': 'u', '\xFA': 'u', '\xFB': 'u', '\xFC': 'u',
'\xDD': 'Y', '\xFD': 'y', '\xFF': 'y', '\xDD': 'y', '\xFD': 'y', '\xFF': 'y',
'\xC6': 'AE', '\xE6': 'ae', '\xC6': 'ae', '\xE6': 'ae',
'\xDE': 'Th', '\xFE': 'th', '\xDE': 'th', '\xFE': 'th',
'\xDF': 'ss', '\xD7': ' ', '\xF7': ' ' '\xDF': 'ss', '\xD7': ' ', '\xF7': ' '
}; };
@@ -503,7 +500,7 @@
result = ''; result = '';
while (++index < length) { while (++index < length) {
result = callback(result, words[index], index, words); result = callback(result, words[index].toLowerCase(), index, words);
} }
return result; return result;
}; };
@@ -7995,10 +7992,7 @@
* // => 'helloWorld' * // => 'helloWorld'
*/ */
var camelCase = createCompounder(function(result, word, index) { var camelCase = createCompounder(function(result, word, index) {
if (!index && reAllCaps.test(word)) { return index ? (result + word.charAt(0).toUpperCase() + word.slice(1)) : word;
return result + word.toLowerCase();
}
return result + (word.charAt(0)[index ? 'toUpperCase' : 'toLowerCase']() + word.slice(1));
}); });
/** /**
@@ -8125,7 +8119,7 @@
* // => 'hello-world' * // => 'hello-world'
*/ */
var kebabCase = createCompounder(function(result, word, index) { var kebabCase = createCompounder(function(result, word, index) {
return result + (index ? '-' : '') + word.toLowerCase(); return result + (index ? '-' : '') + word;
}); });
/** /**
@@ -8286,7 +8280,7 @@
* // => 'hello_world' * // => 'hello_world'
*/ */
var snakeCase = createCompounder(function(result, word, index) { var snakeCase = createCompounder(function(result, word, index) {
return result + (index ? '_' : '') + word.toLowerCase(); return result + (index ? '_' : '') + word;
}); });
/** /**

View File

@@ -1286,8 +1286,7 @@
_.each(['camel', 'kebab', 'snake'], function(caseName) { _.each(['camel', 'kebab', 'snake'], function(caseName) {
var methodName = caseName + 'Case', var methodName = caseName + 'Case',
func = _[methodName], func = _[methodName];
isCamel = caseName == 'camel';
var expected = (function() { var expected = (function() {
switch (caseName) { switch (caseName) {
@@ -1324,22 +1323,12 @@
}); });
test('`_.' + methodName + '` should work with words in all caps', 1, function() { test('`_.' + methodName + '` should work with words in all caps', 1, function() {
strictEqual(func('HELLO WORLD'), isCamel ? 'helloWORLD' : expected); strictEqual(func('HELLO WORLD'), expected);
}); });
test('`_.' + methodName + '` should deburr letters', 1, function() { test('`_.' + methodName + '` should deburr letters', 1, function() {
var actual = _.map(burredLetters, function(burred, index) { var actual = _.map(burredLetters, function(burred, index) {
var deburrLetter = deburredLetters[index]; return func(burred) == deburredLetters[index].toLowerCase();
var string = isCamel
? func('z' + burred)
: func(burred);
var deburredString = isCamel
? 'z' + deburrLetter
: deburrLetter.toLowerCase();
return string == deburredString;
}); });
ok(_.every(actual, _.identity)); ok(_.every(actual, _.identity));
@@ -1372,10 +1361,18 @@
strictEqual(_.camelCase('xhr2 request'), 'xhr2Request'); strictEqual(_.camelCase('xhr2 request'), 'xhr2Request');
}); });
test('should handle acronyms', 3, function() { test('should handle acronyms', 6, function() {
strictEqual(_.camelCase('safe HTML'), 'safeHTML'); _.each(['safe HTML', 'safeHTML'], function(string) {
strictEqual(_.camelCase('escape HTML entities'), 'escapeHTMLEntities'); strictEqual(_.camelCase(string), 'safeHtml');
strictEqual(_.camelCase('XMLHttpRequest'), 'xmlHttpRequest'); });
_.each(['escape HTML entities', 'escapeHTMLEntities'], function(string) {
strictEqual(_.camelCase(string), 'escapeHtmlEntities');
});
_.each(['XMLHttpRequest', 'XmlHTTPRequest'], function(string) {
strictEqual(_.camelCase(string), 'xmlHttpRequest');
});
}); });
}()); }());