es6.string.starts-with.js 762 B

123456789101112131415161718
  1. // 21.1.3.18 String.prototype.startsWith(searchString [, position ])
  2. 'use strict';
  3. var $export = require('./_export');
  4. var toLength = require('./_to-length');
  5. var context = require('./_string-context');
  6. var STARTS_WITH = 'startsWith';
  7. var $startsWith = ''[STARTS_WITH];
  8. $export($export.P + $export.F * require('./_fails-is-regexp')(STARTS_WITH), 'String', {
  9. startsWith: function startsWith(searchString /* , position = 0 */) {
  10. var that = context(this, searchString, STARTS_WITH);
  11. var index = toLength(Math.min(arguments.length > 1 ? arguments[1] : undefined, that.length));
  12. var search = String(searchString);
  13. return $startsWith
  14. ? $startsWith.call(that, search, index)
  15. : that.slice(index, index + search.length) === search;
  16. }
  17. });