diff --git a/test/collections.js b/test/collections.js index 4d9fdaa5d..ed8ea7afc 100644 --- a/test/collections.js +++ b/test/collections.js @@ -164,6 +164,11 @@ $(document).ready(function() { ok(!_.include([1,3,9], 2), 'two is not in the array'); ok(_.contains({moe:1, larry:3, curly:9}, 3) === true, '_.include on objects checks their values'); ok(_([1,2,3]).include(2), 'OO-style include'); + ok(_.include('test this out', 'out'), 'include with a string'); + ok(!_.include('test this out', 'not included'), 'include with a string (false)'); + ok(_.include('1 2 3 4', 3), 'include with a string and number'); + ok(_.include(new String('test this'), 'test'), 'works with boxed strings'); + ok(_('test this out').include('this'), 'OO-style include with a string'); }); test('collections: invoke', function() { diff --git a/underscore.js b/underscore.js index a23ef7872..1ff2c83bc 100644 --- a/underscore.js +++ b/underscore.js @@ -198,11 +198,13 @@ }; // Determine if a given value is included in the array or object using `===`. - // Aliased as `contains`. + // Aliased as `contains`. Or if `obj` is a string, test if the string contains the + // substring. _.include = _.contains = function(obj, target) { var found = false; if (obj == null) return found; if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1; + if (_.isString(obj)) return obj.indexOf(target) != -1; found = any(obj, function(value) { return value === target; }); @@ -832,7 +834,7 @@ return toString.call(obj) == '[object ' + name + ']'; }; }); - + // Define a fallback version of the method in browsers (ahem, IE), where // there isn't any inspectable "Arguments" type. if (!_.isArguments(arguments)) {