es6.string.ends-with.js 840 B

123456789101112131415161718192021
  1. // 21.1.3.6 String.prototype.endsWith(searchString [, endPosition])
  2. 'use strict';
  3. var $export = require('./_export');
  4. var toLength = require('./_to-length');
  5. var context = require('./_string-context');
  6. var ENDS_WITH = 'endsWith';
  7. var $endsWith = ''[ENDS_WITH];
  8. $export($export.P + $export.F * require('./_fails-is-regexp')(ENDS_WITH), 'String', {
  9. endsWith: function endsWith(searchString /* , endPosition = @length */) {
  10. var that = context(this, searchString, ENDS_WITH);
  11. var endPosition = arguments.length > 1 ? arguments[1] : undefined;
  12. var len = toLength(that.length);
  13. var end = endPosition === undefined ? len : Math.min(toLength(endPosition), len);
  14. var search = String(searchString);
  15. return $endsWith
  16. ? $endsWith.call(that, search, end)
  17. : that.slice(end - search.length, end) === search;
  18. }
  19. });