es6.string.ends-with.js 840 B

1234567891011121314151617181920
  1. // 21.1.3.6 String.prototype.endsWith(searchString [, endPosition])
  2. 'use strict';
  3. var $export = require('./_export')
  4. , toLength = require('./_to-length')
  5. , context = require('./_string-context')
  6. , ENDS_WITH = 'endsWith'
  7. , $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. , endPosition = arguments.length > 1 ? arguments[1] : undefined
  12. , len = toLength(that.length)
  13. , end = endPosition === undefined ? len : Math.min(toLength(endPosition), len)
  14. , search = String(searchString);
  15. return $endsWith
  16. ? $endsWith.call(that, search, end)
  17. : that.slice(end - search.length, end) === search;
  18. }
  19. });