From 3f64316e00dafad372f1dc0e6a351302778215b5 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 7 Sep 2014 01:20:18 -0700 Subject: [PATCH] Make `_.camelCase` process words as lower cased. --- lodash.js | 38 ++++++++++++++++---------------------- test/test.js | 33 +++++++++++++++------------------ 2 files changed, 31 insertions(+), 40 deletions(-) diff --git a/lodash.js b/lodash.js index e53ebfa08..764d1ae2e 100644 --- a/lodash.js +++ b/lodash.js @@ -46,9 +46,6 @@ /** Used to generate unique IDs */ 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 */ var reEmptyStringLeading = /\b__p \+= '';/g, reEmptyStringMiddle = /\b(__p \+=) '' \+/g, @@ -102,7 +99,7 @@ var reUnescapedString = /['\n\r\u2028\u2029\\]/g; /** 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 */ var whitespace = ( @@ -245,22 +242,22 @@ * for more details. */ 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', - '\xC7': 'C', '\xE7': 'c', - '\xD0': 'D', '\xF0': 'd', - '\xC8': 'E', '\xC9': 'E', '\xCA': 'E', '\xCB': 'E', + '\xC7': 'c', '\xE7': 'c', + '\xD0': 'd', '\xF0': 'd', + '\xC8': 'e', '\xC9': 'e', '\xCA': 'e', '\xCB': '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', - '\xD1': 'N', '\xF1': 'n', - '\xD2': 'O', '\xD3': 'O', '\xD4': 'O', '\xD5': 'O', '\xD6': 'O', '\xD8': 'O', + '\xD1': 'n', '\xF1': 'n', + '\xD2': 'o', '\xD3': 'o', '\xD4': 'o', '\xD5': 'o', '\xD6': 'o', '\xD8': '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', - '\xDD': 'Y', '\xFD': 'y', '\xFF': 'y', - '\xC6': 'AE', '\xE6': 'ae', - '\xDE': 'Th', '\xFE': 'th', + '\xDD': 'y', '\xFD': 'y', '\xFF': 'y', + '\xC6': 'ae', '\xE6': 'ae', + '\xDE': 'th', '\xFE': 'th', '\xDF': 'ss', '\xD7': ' ', '\xF7': ' ' }; @@ -503,7 +500,7 @@ result = ''; while (++index < length) { - result = callback(result, words[index], index, words); + result = callback(result, words[index].toLowerCase(), index, words); } return result; }; @@ -7995,10 +7992,7 @@ * // => 'helloWorld' */ var camelCase = createCompounder(function(result, word, index) { - if (!index && reAllCaps.test(word)) { - return result + word.toLowerCase(); - } - return result + (word.charAt(0)[index ? 'toUpperCase' : 'toLowerCase']() + word.slice(1)); + return index ? (result + word.charAt(0).toUpperCase() + word.slice(1)) : word; }); /** @@ -8125,7 +8119,7 @@ * // => 'hello-world' */ var kebabCase = createCompounder(function(result, word, index) { - return result + (index ? '-' : '') + word.toLowerCase(); + return result + (index ? '-' : '') + word; }); /** @@ -8286,7 +8280,7 @@ * // => 'hello_world' */ var snakeCase = createCompounder(function(result, word, index) { - return result + (index ? '_' : '') + word.toLowerCase(); + return result + (index ? '_' : '') + word; }); /** diff --git a/test/test.js b/test/test.js index 61a7fcf1a..fb47a4c54 100644 --- a/test/test.js +++ b/test/test.js @@ -1286,8 +1286,7 @@ _.each(['camel', 'kebab', 'snake'], function(caseName) { var methodName = caseName + 'Case', - func = _[methodName], - isCamel = caseName == 'camel'; + func = _[methodName]; var expected = (function() { switch (caseName) { @@ -1324,22 +1323,12 @@ }); 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() { var actual = _.map(burredLetters, function(burred, index) { - var deburrLetter = deburredLetters[index]; - - var string = isCamel - ? func('z' + burred) - : func(burred); - - var deburredString = isCamel - ? 'z' + deburrLetter - : deburrLetter.toLowerCase(); - - return string == deburredString; + return func(burred) == deburredLetters[index].toLowerCase(); }); ok(_.every(actual, _.identity)); @@ -1372,10 +1361,18 @@ strictEqual(_.camelCase('xhr2 request'), 'xhr2Request'); }); - test('should handle acronyms', 3, function() { - strictEqual(_.camelCase('safe HTML'), 'safeHTML'); - strictEqual(_.camelCase('escape HTML entities'), 'escapeHTMLEntities'); - strictEqual(_.camelCase('XMLHttpRequest'), 'xmlHttpRequest'); + test('should handle acronyms', 6, function() { + _.each(['safe HTML', 'safeHTML'], function(string) { + strictEqual(_.camelCase(string), 'safeHtml'); + }); + + _.each(['escape HTML entities', 'escapeHTMLEntities'], function(string) { + strictEqual(_.camelCase(string), 'escapeHtmlEntities'); + }); + + _.each(['XMLHttpRequest', 'XmlHTTPRequest'], function(string) { + strictEqual(_.camelCase(string), 'xmlHttpRequest'); + }); }); }());