From 849fd71f768c5fe584bd91e46928848fcf26d058 Mon Sep 17 00:00:00 2001 From: James Kyle Date: Sat, 31 Jan 2015 15:09:31 -0800 Subject: [PATCH] Add _.startCase. --- lodash.src.js | 26 ++++++++++++++++++++++++++ test/test.js | 21 ++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index 0fc2c68a9..0bc101475 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -9451,6 +9451,31 @@ return result + (index ? '-' : '') + word.toLowerCase(); }); + /** + * Converts `string` to start case. + * See [Wikipedia](https://en.wikipedia.org/wiki/Letter_case#Stylistic_or_specialised_usage) + * for more details. + * + * @static + * @memberOf _ + * @category String + * @param {string} [string=''] The string to convert. + * @returns {string} Returns the start cased string. + * @example + * + * _.startCase('Foo Bar'); + * // => 'Foo Bar' + * + * _.startCase('fooBar'); + * // => 'Foo Bar' + * + * _.startCase('__foo_bar__'); + * // => 'Foo Bar' + */ + var startCase = createCompounder(function(result, word, index) { + return result + (index ? ' ' : '') + capitalize(word); + }); + /** * Pads `string` on the left and right sides if it is shorter then the given * padding length. The `chars` string may be truncated if the number of padding @@ -10747,6 +10772,7 @@ lodash.some = some; lodash.sortedIndex = sortedIndex; lodash.sortedLastIndex = sortedLastIndex; + lodash.startCase = startCase; lodash.startsWith = startsWith; lodash.template = template; lodash.trim = trim; diff --git a/test/test.js b/test/test.js index e1daf8b74..1f430da5c 100644 --- a/test/test.js +++ b/test/test.js @@ -1444,7 +1444,7 @@ QUnit.module('case methods'); - _.each(['camel', 'kebab', 'snake'], function(caseName) { + _.each(['camel', 'kebab', 'snake', 'start'], function(caseName) { var methodName = caseName + 'Case', func = _[methodName]; @@ -1458,12 +1458,17 @@ case 'camel': return 'fooBar'; case 'kebab': return 'foo-bar'; case 'snake': return 'foo_bar'; + case 'start': return 'Foo Bar'; } }()); test('`_.' + methodName + '` should convert `string` to ' + caseName + ' case', 1, function() { var actual = _.map(strings, function(string) { - return func(string) === expected; + if (caseName === 'start' && string === 'FOO BAR') { + return func(string) === 'FOO BAR'; + } else { + return func(string) === expected; + } }); deepEqual(actual, _.map(strings, _.constant(true))); @@ -1471,7 +1476,11 @@ test('`_.' + methodName + '` should handle double-converting strings', 1, function() { var actual = _.map(strings, function(string) { - return func(func(string)) === expected; + if (caseName === 'start' && string === 'FOO BAR') { + return func(func(string)) === 'FOO BAR'; + } else { + return func(func(string)) === expected; + } }); deepEqual(actual, _.map(strings, _.constant(true))); @@ -1479,7 +1488,9 @@ test('`_.' + methodName + '` should deburr letters', 1, function() { var actual = _.map(burredLetters, function(burred, index) { - return func(burred) === deburredLetters[index].toLowerCase(); + var letter = deburredLetters[index].toLowerCase(); + letter = caseName === 'start' ? _.capitalize(letter) : letter; + return func(burred) === letter; }); deepEqual(actual, _.map(burredLetters, _.constant(true))); @@ -14870,7 +14881,7 @@ var acceptFalsey = _.difference(allMethods, rejectFalsey); - test('should accept falsey arguments', 205, function() { + test('should accept falsey arguments', 206, function() { var emptyArrays = _.map(falsey, _.constant([])), isExposed = '_' in root, oldDash = root._;