Ensure trim methods and _.words work as an iteratee for _.map with string objects.

This commit is contained in:
John-David Dalton
2014-12-17 19:54:26 -08:00
parent 022b295aaa
commit 56c1bf01a4
2 changed files with 20 additions and 12 deletions

View File

@@ -4926,9 +4926,10 @@
iteratee = isIterateeCall(array, isSorted, thisArg) ? null : isSorted; iteratee = isIterateeCall(array, isSorted, thisArg) ? null : isSorted;
isSorted = false; isSorted = false;
} }
if (iteratee != null) { iteratee = iteratee == null
iteratee = getCallback(iteratee, thisArg, 3); ? iteratee
} : getCallback(iteratee, thisArg, 3);
return (isSorted && getIndexOf() == baseIndexOf) return (isSorted && getIndexOf() == baseIndexOf)
? sortedUniq(array, iteratee) ? sortedUniq(array, iteratee)
: baseUniq(array, iteratee); : baseUniq(array, iteratee);
@@ -9196,8 +9197,8 @@
// Firefox < 21 and Opera < 15 follow ES3 for `parseInt` and // Firefox < 21 and Opera < 15 follow ES3 for `parseInt` and
// Chrome fails to trim leading <BOM> whitespace characters. // Chrome fails to trim leading <BOM> whitespace characters.
// See https://code.google.com/p/v8/issues/detail?id=3109. // See https://code.google.com/p/v8/issues/detail?id=3109.
string = trim(string);
radix = (guard && isIterateeCall(string, radix, guard)) ? 0 : +radix; radix = (guard && isIterateeCall(string, radix, guard)) ? 0 : +radix;
string = trim(string);
return nativeParseInt(string, radix || (reHexPrefix.test(string) ? 16 : 10)); return nativeParseInt(string, radix || (reHexPrefix.test(string) ? 16 : 10));
}; };
} }
@@ -9517,11 +9518,12 @@
* // => 'fred' * // => 'fred'
*/ */
function trim(string, chars, guard) { function trim(string, chars, guard) {
var value = string;
string = string == null ? '' : String(string); string = string == null ? '' : String(string);
if (!string) { if (!string) {
return string; return string;
} }
if (guard ? isIterateeCall(string, chars, guard) : chars == null) { if (guard ? isIterateeCall(value, chars, guard) : chars == null) {
return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1); return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1);
} }
chars = String(chars); chars = String(chars);
@@ -9547,11 +9549,12 @@
* // => 'fred-_-' * // => 'fred-_-'
*/ */
function trimLeft(string, chars, guard) { function trimLeft(string, chars, guard) {
var value = string;
string = string == null ? '' : String(string); string = string == null ? '' : String(string);
if (!string) { if (!string) {
return string; return string;
} }
if (guard ? isIterateeCall(string, chars, guard) : chars == null) { if (guard ? isIterateeCall(value, chars, guard) : chars == null) {
return string.slice(trimmedLeftIndex(string)) return string.slice(trimmedLeftIndex(string))
} }
chars = String(chars); chars = String(chars);
@@ -9577,11 +9580,12 @@
* // => '-_-fred' * // => '-_-fred'
*/ */
function trimRight(string, chars, guard) { function trimRight(string, chars, guard) {
var value = string;
string = string == null ? '' : String(string); string = string == null ? '' : String(string);
if (!string) { if (!string) {
return string; return string;
} }
if (guard ? isIterateeCall(string, chars, guard) : chars == null) { if (guard ? isIterateeCall(value, chars, guard) : chars == null) {
return string.slice(0, trimmedRightIndex(string) + 1) return string.slice(0, trimmedRightIndex(string) + 1)
} }
chars = String(chars); chars = String(chars);
@@ -9715,10 +9719,10 @@
* // => ['fred', 'barney', '&', 'pebbles'] * // => ['fred', 'barney', '&', 'pebbles']
*/ */
function words(string, pattern, guard) { function words(string, pattern, guard) {
string = string != null && String(string);
if (guard && isIterateeCall(string, pattern, guard)) { if (guard && isIterateeCall(string, pattern, guard)) {
pattern = null; pattern = null;
} }
string = string != null && String(string);
return (string && string.match(pattern || reWords)) || []; return (string && string.match(pattern || reWords)) || [];
} }

View File

@@ -8998,7 +8998,9 @@
}); });
test('should work as an iteratee for `_.map`', 2, function() { test('should work as an iteratee for `_.map`', 2, function() {
var actual = _.map(['6', '08', '10'], _.parseInt); var strings = _.map(['6', '08', '10'], Object),
actual = _.map(strings, _.parseInt);
deepEqual(actual, [6, 8, 10]); deepEqual(actual, [6, 8, 10]);
actual = _.map('123', _.parseInt); actual = _.map('123', _.parseInt);
@@ -12540,8 +12542,8 @@
strictEqual(func(string, ''), string); strictEqual(func(string, ''), string);
}); });
test('should work as an iteratee for `_.map`', 1, function() { test('`_.' + methodName + '` should work as an iteratee for `_.map`', 1, function() {
var string = whitespace + 'a b c' + whitespace, var string = Object(whitespace + 'a b c' + whitespace),
trimmed = (index == 2 ? whitespace : '') + 'a b c' + (index == 1 ? whitespace : ''), trimmed = (index == 2 ? whitespace : '') + 'a b c' + (index == 1 ? whitespace : ''),
actual = _.map([string, string, string], func); actual = _.map([string, string, string], func);
@@ -12898,7 +12900,9 @@
}); });
test('should work as an iteratee for `_.map`', 1, function() { test('should work as an iteratee for `_.map`', 1, function() {
var actual = _.map(['a', 'b', 'c'], _.words); var strings = _.map(['a', 'b', 'c'], Object),
actual = _.map(strings, _.words);
deepEqual(actual, [['a'], ['b'], ['c']]); deepEqual(actual, [['a'], ['b'], ['c']]);
}); });