es.regexp.to-string.js 1.3 KB

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var uncurryThis = require('../internals/function-uncurry-this');
  3. var PROPER_FUNCTION_NAME = require('../internals/function-name').PROPER;
  4. var redefine = require('../internals/redefine');
  5. var anObject = require('../internals/an-object');
  6. var isPrototypeOf = require('../internals/object-is-prototype-of');
  7. var $toString = require('../internals/to-string');
  8. var fails = require('../internals/fails');
  9. var regExpFlags = require('../internals/regexp-flags');
  10. var TO_STRING = 'toString';
  11. var RegExpPrototype = RegExp.prototype;
  12. var n$ToString = RegExpPrototype[TO_STRING];
  13. var getFlags = uncurryThis(regExpFlags);
  14. var NOT_GENERIC = fails(function () { return n$ToString.call({ source: 'a', flags: 'b' }) != '/a/b'; });
  15. // FF44- RegExp#toString has a wrong name
  16. var INCORRECT_NAME = PROPER_FUNCTION_NAME && n$ToString.name != TO_STRING;
  17. // `RegExp.prototype.toString` method
  18. // https://tc39.es/ecma262/#sec-regexp.prototype.tostring
  19. if (NOT_GENERIC || INCORRECT_NAME) {
  20. redefine(RegExp.prototype, TO_STRING, function toString() {
  21. var R = anObject(this);
  22. var p = $toString(R.source);
  23. var rf = R.flags;
  24. var f = $toString(rf === undefined && isPrototypeOf(RegExpPrototype, R) && !('flags' in RegExpPrototype) ? getFlags(R) : rf);
  25. return '/' + p + '/' + f;
  26. }, { unsafe: true });
  27. }