UNPKG

1.08 kBJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var toLength = require('../internals/to-length');
4var notARegExp = require('../internals/not-a-regexp');
5var requireObjectCoercible = require('../internals/require-object-coercible');
6var correctIsRegExpLogic = require('../internals/correct-is-regexp-logic');
7
8var nativeEndsWith = ''.endsWith;
9var min = Math.min;
10
11// `String.prototype.endsWith` method
12// https://tc39.github.io/ecma262/#sec-string.prototype.endswith
13$({ target: 'String', proto: true, forced: !correctIsRegExpLogic('endsWith') }, {
14 endsWith: function endsWith(searchString /* , endPosition = @length */) {
15 var that = String(requireObjectCoercible(this));
16 notARegExp(searchString);
17 var endPosition = arguments.length > 1 ? arguments[1] : undefined;
18 var len = toLength(that.length);
19 var end = endPosition === undefined ? len : min(toLength(endPosition), len);
20 var search = String(searchString);
21 return nativeEndsWith
22 ? nativeEndsWith.call(that, search, end)
23 : that.slice(end - search.length, end) === search;
24 }
25});