Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make _.include/_.contains work with strings. #667

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/collections.js
Expand Up @@ -164,6 +164,11 @@ $(document).ready(function() {
ok(!_.include([1,3,9], 2), 'two is not in the array'); 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(_.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(_([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() { test('collections: invoke', function() {
Expand Down
6 changes: 4 additions & 2 deletions underscore.js
Expand Up @@ -198,11 +198,13 @@
}; };


// Determine if a given value is included in the array or object using `===`. // 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) { _.include = _.contains = function(obj, target) {
var found = false; var found = false;
if (obj == null) return found; if (obj == null) return found;
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1; if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
if (_.isString(obj)) return obj.indexOf(target) != -1;
found = any(obj, function(value) { found = any(obj, function(value) {
return value === target; return value === target;
}); });
Expand Down Expand Up @@ -832,7 +834,7 @@
return toString.call(obj) == '[object ' + name + ']'; return toString.call(obj) == '[object ' + name + ']';
}; };
}); });

// Define a fallback version of the method in browsers (ahem, IE), where // Define a fallback version of the method in browsers (ahem, IE), where
// there isn't any inspectable "Arguments" type. // there isn't any inspectable "Arguments" type.
if (!_.isArguments(arguments)) { if (!_.isArguments(arguments)) {
Expand Down