es.string.ends-with.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
  4. var toLength = require('../internals/to-length');
  5. var notARegExp = require('../internals/not-a-regexp');
  6. var requireObjectCoercible = require('../internals/require-object-coercible');
  7. var correctIsRegExpLogic = require('../internals/correct-is-regexp-logic');
  8. var IS_PURE = require('../internals/is-pure');
  9. var nativeEndsWith = ''.endsWith;
  10. var min = Math.min;
  11. var CORRECT_IS_REGEXP_LOGIC = correctIsRegExpLogic('endsWith');
  12. // https://github.com/zloirock/core-js/pull/702
  13. var MDN_POLYFILL_BUG = !IS_PURE && !CORRECT_IS_REGEXP_LOGIC && !!function () {
  14. var descriptor = getOwnPropertyDescriptor(String.prototype, 'endsWith');
  15. return descriptor && !descriptor.writable;
  16. }();
  17. // `String.prototype.endsWith` method
  18. // https://tc39.github.io/ecma262/#sec-string.prototype.endswith
  19. $({ target: 'String', proto: true, forced: !MDN_POLYFILL_BUG && !CORRECT_IS_REGEXP_LOGIC }, {
  20. endsWith: function endsWith(searchString /* , endPosition = @length */) {
  21. var that = String(requireObjectCoercible(this));
  22. notARegExp(searchString);
  23. var endPosition = arguments.length > 1 ? arguments[1] : undefined;
  24. var len = toLength(that.length);
  25. var end = endPosition === undefined ? len : min(toLength(endPosition), len);
  26. var search = String(searchString);
  27. return nativeEndsWith
  28. ? nativeEndsWith.call(that, search, end)
  29. : that.slice(end - search.length, end) === search;
  30. }
  31. });