es.string.starts-with.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var uncurryThis = require('../internals/function-uncurry-this-clause');
  4. var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
  5. var toLength = require('../internals/to-length');
  6. var toString = require('../internals/to-string');
  7. var notARegExp = require('../internals/not-a-regexp');
  8. var requireObjectCoercible = require('../internals/require-object-coercible');
  9. var correctIsRegExpLogic = require('../internals/correct-is-regexp-logic');
  10. var IS_PURE = require('../internals/is-pure');
  11. // eslint-disable-next-line es/no-string-prototype-startswith -- safe
  12. var nativeStartsWith = uncurryThis(''.startsWith);
  13. var stringSlice = uncurryThis(''.slice);
  14. var min = Math.min;
  15. var CORRECT_IS_REGEXP_LOGIC = correctIsRegExpLogic('startsWith');
  16. // https://github.com/zloirock/core-js/pull/702
  17. var MDN_POLYFILL_BUG = !IS_PURE && !CORRECT_IS_REGEXP_LOGIC && !!function () {
  18. var descriptor = getOwnPropertyDescriptor(String.prototype, 'startsWith');
  19. return descriptor && !descriptor.writable;
  20. }();
  21. // `String.prototype.startsWith` method
  22. // https://tc39.es/ecma262/#sec-string.prototype.startswith
  23. $({ target: 'String', proto: true, forced: !MDN_POLYFILL_BUG && !CORRECT_IS_REGEXP_LOGIC }, {
  24. startsWith: function startsWith(searchString /* , position = 0 */) {
  25. var that = toString(requireObjectCoercible(this));
  26. notARegExp(searchString);
  27. var index = toLength(min(arguments.length > 1 ? arguments[1] : undefined, that.length));
  28. var search = toString(searchString);
  29. return nativeStartsWith
  30. ? nativeStartsWith(that, search, index)
  31. : stringSlice(that, index, index + search.length) === search;
  32. }
  33. });