From b504a557f49072cb1973e9683ff7346b95000e04 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 6 Nov 2012 02:56:14 -0800 Subject: [PATCH] Add unit tests for `_.max` and `_.min` accepting strings and adjust the build/test-build.js. Former-commit-id: b567c019146e96ad257dab8fe1b4138d07f470c0 --- lodash.js | 16 +++++++++++++--- test/test-build.js | 3 +++ test/test.js | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lodash.js b/lodash.js index fd7623dc9..c0467240a 100644 --- a/lodash.js +++ b/lodash.js @@ -1832,7 +1832,7 @@ fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex) || 0; if (typeof length == 'number') { - return (toString.call(collection) == stringClass + return (isString(collection) ? collection.indexOf(target, fromIndex) : indexOf(collection, target, fromIndex) ) > -1; @@ -2127,6 +2127,11 @@ result = computed; if (callback || !isArray(collection)) { + if (!callback && isString(collection)) { + callback = function(value) { + return value.charCodeAt(0); + }; + } callback = createCallback(callback, thisArg); forEach(collection, function(value, index, collection) { var current = callback(value, index, collection); @@ -2170,6 +2175,11 @@ result = computed; if (callback || !isArray(collection)) { + if (!callback && isString(collection)) { + callback = function(value) { + return value.charCodeAt(0); + }; + } callback = createCallback(callback, thisArg); forEach(collection, function(value, index, collection) { var current = callback(value, index, collection); @@ -2274,7 +2284,7 @@ if (typeof length != 'number') { var props = keys(collection); length = props.length; - } else if (noCharByIndex && toString.call(collection) == stringClass) { + } else if (noCharByIndex && isString(collection)) { iteratee = collection.split(''); } forEach(collection, function(value, index, collection) { @@ -2449,7 +2459,7 @@ */ function toArray(collection) { if (collection && typeof collection.length == 'number') { - return (noArraySliceOnStrings ? toString.call(collection) == stringClass : typeof collection == 'string') + return (noArraySliceOnStrings ? isString(collection) : typeof collection == 'string') ? collection.split('') : slice.call(collection); } diff --git a/test/test-build.js b/test/test-build.js index 1212aad7f..fe1f59e15 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -649,7 +649,10 @@ object = { 'length': 0, 'splice': Array.prototype.splice }; equal(lodash.isEmpty(object), false, '_.isEmpty should return `false` for jQuery/MooTools DOM query collections: ' + basename); + equal(lodash.isFinite('2'), false, '_.isFinite should return `false` for numeric string values: ' + basename); + equal(lodash.max('abc'), -Infinity, '_.max should return `-Infinity` for strings: ' + basename); + equal(lodash.min('abc'), Infinity, '_.min should return `Infinity` for strings: ' + basename); // avoid issues comparing objects with `deepEqual` object = { 'a': 1, 'b': 2, 'c': 3 }; diff --git a/test/test.js b/test/test.js index a4c42b944..fad3b7caf 100644 --- a/test/test.js +++ b/test/test.js @@ -1039,6 +1039,21 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.max and lodash.min string iteration'); + + _.each(['max', 'min'], function(methodName) { + var func = _[methodName]; + + test('lodash.' + methodName + ' should iterate a string', function() { + _.each(['abc', Object('abc')], function(value) { + var actual = func(value); + equal(actual, methodName == 'max' ? 'c' : 'a'); + }); + }); + }); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.merge'); (function() {