es6.string.ends-with.js 855 B

123456789101112131415161718192021
  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. , $$ = arguments
  12. , endPosition = $$.length > 1 ? $$[1] : undefined
  13. , len = toLength(that.length)
  14. , end = endPosition === undefined ? len : Math.min(toLength(endPosition), len)
  15. , search = String(searchString);
  16. return $endsWith
  17. ? $endsWith.call(that, search, end)
  18. : that.slice(end - search.length, end) === search;
  19. }
  20. });