Skip to content

Commit

Permalink
String#includes should throw when given a RegExp.
Browse files Browse the repository at this point in the history
Fixes #349.
  • Loading branch information
ljharb committed Jun 17, 2015
1 parent e7a8904 commit 480809e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 7 additions & 1 deletion es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,13 @@
},

includes: function includes(searchString) {
var position = arguments.length > 1 ? arguments[1] : void 0;
if (Type.regex(searchString)) {
throw new TypeError('"includes" does not accept a RegExp');
}
var position;
if (arguments.length > 1) {
position = arguments[1];
}
// Somehow this trick makes method 100% compat with the spec.
return _indexOf(this, searchString, position) !== -1;
},
Expand Down
4 changes: 4 additions & 0 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ var runStringTests = function () {
testObjectCoercible('includes');
});

it('throws a TypeError when given a regex', function () {
expect(function () { 'foo'.includes(/a/g); }).to['throw'](TypeError);
});

it('should be truthy on correct results', function () {
expect('test'.includes('es')).to.equal(true);
expect('abc'.includes('a')).to.equal(true);
Expand Down

0 comments on commit 480809e

Please sign in to comment.