Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,8 @@
var thisStr = String(this);
searchStr = String(searchStr);
var thisLen = thisStr.length;
var pos = (arguments[1] === undefined) ?
thisLen : ES.toInteger(arguments[1]);
var end = Math.min(pos, thisLen);
var pos = arguments[1] === undefined ? thisLen : ES.toInteger(arguments[1]);
var end = Math.min(Math.max(pos, 0), thisLen);
return thisStr.slice(end - searchStr.length, end) === searchStr;
},

Expand Down
7 changes: 7 additions & 0 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ describe('String', function() {
expect(function() { return 'abcd'.endsWith(/abc/); }).to.throw(TypeError);
expect(function() { return 'abcd'.endsWith(new RegExp('abc')); }).to.throw(TypeError);
});

it('should handle negative and zero positions properly', function() {
expect('abcd'.endsWith('bcd', 0)).not.to.be.ok;
expect('abcd'.endsWith('bcd', -2)).not.to.be.ok;
expect('abcd'.endsWith('b', -2)).not.to.be.ok;
expect('abcd'.endsWith('ab', -2)).not.to.be.ok;
});
});

describe('#contains()', function() {
Expand Down