shim.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. var define = require('define-properties');
  3. var hasSymbols = require('has-symbols')();
  4. var getPolyfill = require('./polyfill');
  5. var regexpMatchAllPolyfill = require('./polyfill-regexp-matchall');
  6. var defineP = Object.defineProperty;
  7. var gOPD = Object.getOwnPropertyDescriptor;
  8. module.exports = function shimMatchAll() {
  9. var polyfill = getPolyfill();
  10. define(
  11. String.prototype,
  12. { matchAll: polyfill },
  13. { matchAll: function () { return String.prototype.matchAll !== polyfill; } }
  14. );
  15. if (hasSymbols) {
  16. // eslint-disable-next-line no-restricted-properties
  17. var symbol = Symbol.matchAll || (Symbol['for'] ? Symbol['for']('Symbol.matchAll') : Symbol('Symbol.matchAll'));
  18. define(
  19. Symbol,
  20. { matchAll: symbol },
  21. { matchAll: function () { return Symbol.matchAll !== symbol; } }
  22. );
  23. if (defineP && gOPD) {
  24. var desc = gOPD(Symbol, symbol);
  25. if (!desc || desc.configurable) {
  26. defineP(Symbol, symbol, {
  27. configurable: false,
  28. enumerable: false,
  29. value: symbol,
  30. writable: false
  31. });
  32. }
  33. }
  34. var regexpMatchAll = regexpMatchAllPolyfill();
  35. var func = {};
  36. func[symbol] = regexpMatchAll;
  37. var predicate = {};
  38. predicate[symbol] = function () {
  39. return RegExp.prototype[symbol] !== regexpMatchAll;
  40. };
  41. define(RegExp.prototype, func, predicate);
  42. }
  43. return polyfill;
  44. };