Skip to content

Commit

Permalink
String#contains → String#includes per 2014-11-19 TC39 meeting.
Browse files Browse the repository at this point in the history
Fixes #306.
  • Loading branch information
ljharb committed Nov 21, 2014
1 parent f19261a commit 226517d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ details.
* `repeat()` ([a standalone shim is also available](http://mths.be/repeat))
* `startsWith()` ([a standalone shim is also available](http://mths.be/startswith))
* `endsWith()` ([a standalone shim is also available](http://mths.be/endswith))
* `contains()` ([a standalone shim is also available](http://mths.be/contains))
* `includes()` ([a standalone shim is also available](http://mths.be/includes))
* `Number`:
* `MAX_SAFE_INTEGER`
* `MIN_SAFE_INTEGER`
Expand Down Expand Up @@ -126,7 +126,7 @@ WeakMap has a very unusual use-case so you probably won't need it at all
```javascript
'abc'.startsWith('a') // true
'abc'.endsWith('a') // false
'john alice'.contains('john') // true
'john alice'.includes('john') // true
'123'.repeat(2) // '123123'

Object.is(NaN, NaN) // Fixes ===. 0 isnt -0, NaN is NaN
Expand Down
2 changes: 1 addition & 1 deletion es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@
return thisStr.slice(end - searchStr.length, end) === searchStr;
},

contains: function (searchString) {
includes: function includes(searchString) {
var position = arguments.length > 1 ? arguments[1] : void 0;
// Somehow this trick makes method 100% compat with the spec.
return _indexOf.call(this, searchString, position) !== -1;
Expand Down
68 changes: 34 additions & 34 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,46 +224,46 @@ var runStringTests = function () {
});
});

describe('#contains()', function () {
describe('#includes()', function () {
it('should throw a TypeError when called on null or undefined', function () {
testObjectCoercible('contains');
testObjectCoercible('includes');
});

it('should be truthy on correct results', function () {
expect('test'.contains('es')).to.equal(true);
expect('abc'.contains('a')).to.equal(true);
expect('abc'.contains('b')).to.equal(true);
expect('abc'.contains('abc')).to.equal(true);
expect('abc'.contains('bc')).to.equal(true);
expect('abc'.contains('d')).to.equal(false);
expect('abc'.contains('abcd')).to.equal(false);
expect('abc'.contains('ac')).to.equal(false);
expect('abc'.contains('abc', 0)).to.equal(true);
expect('abc'.contains('bc', 0)).to.equal(true);
expect('abc'.contains('de', 0)).to.equal(false);
expect('abc'.contains('bc', 1)).to.equal(true);
expect('abc'.contains('c', 1)).to.equal(true);
expect('abc'.contains('a', 1)).to.equal(false);
expect('abc'.contains('abc', 1)).to.equal(false);
expect('abc'.contains('c', 2)).to.equal(true);
expect('abc'.contains('d', 2)).to.equal(false);
expect('abc'.contains('dcd', 2)).to.equal(false);
expect('abc'.contains('a', 42)).to.equal(false);
expect('abc'.contains('a', Infinity)).to.equal(false);
expect('abc'.contains('ab', -43)).to.equal(true);
expect('abc'.contains('cd', -42)).to.equal(false);
expect('abc'.contains('ab', -Infinity)).to.equal(true);
expect('abc'.contains('cd', -Infinity)).to.equal(false);
expect('abc'.contains('ab', NaN)).to.equal(true);
expect('abc'.contains('cd', NaN)).to.equal(false);
expect('test'.includes('es')).to.equal(true);
expect('abc'.includes('a')).to.equal(true);
expect('abc'.includes('b')).to.equal(true);
expect('abc'.includes('abc')).to.equal(true);
expect('abc'.includes('bc')).to.equal(true);
expect('abc'.includes('d')).to.equal(false);
expect('abc'.includes('abcd')).to.equal(false);
expect('abc'.includes('ac')).to.equal(false);
expect('abc'.includes('abc', 0)).to.equal(true);
expect('abc'.includes('bc', 0)).to.equal(true);
expect('abc'.includes('de', 0)).to.equal(false);
expect('abc'.includes('bc', 1)).to.equal(true);
expect('abc'.includes('c', 1)).to.equal(true);
expect('abc'.includes('a', 1)).to.equal(false);
expect('abc'.includes('abc', 1)).to.equal(false);
expect('abc'.includes('c', 2)).to.equal(true);
expect('abc'.includes('d', 2)).to.equal(false);
expect('abc'.includes('dcd', 2)).to.equal(false);
expect('abc'.includes('a', 42)).to.equal(false);
expect('abc'.includes('a', Infinity)).to.equal(false);
expect('abc'.includes('ab', -43)).to.equal(true);
expect('abc'.includes('cd', -42)).to.equal(false);
expect('abc'.includes('ab', -Infinity)).to.equal(true);
expect('abc'.includes('cd', -Infinity)).to.equal(false);
expect('abc'.includes('ab', NaN)).to.equal(true);
expect('abc'.includes('cd', NaN)).to.equal(false);

var myobj = {
toString: function () {return 'abc';},
contains: String.prototype.contains
includes: String.prototype.includes
};

expect(myobj.contains('abc')).to.equal(true);
expect(myobj.contains('cd')).to.equal(false);
expect(myobj.includes('abc')).to.equal(true);
expect(myobj.includes('cd')).to.equal(false);

var gotStr = false, gotPos = false;

Expand All @@ -274,7 +274,7 @@ var runStringTests = function () {
return 'xyz';
},

contains: String.prototype.contains
includes: String.prototype.includes
};

var idx = {
Expand All @@ -285,12 +285,12 @@ var runStringTests = function () {
}
};

myobj.contains('elephant', idx);
myobj.includes('elephant', idx);
expect(gotPos).to.equal(true);
});

it('should be falsy on incorrect results', function () {
expect('test'.contains('1290')).to.equal(false);
expect('test'.includes('1290')).to.equal(false);
});
});

Expand Down

0 comments on commit 226517d

Please sign in to comment.