es.regexp.to-string.js 985 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. var redefine = require('../internals/redefine');
  3. var anObject = require('../internals/an-object');
  4. var fails = require('../internals/fails');
  5. var flags = require('../internals/regexp-flags');
  6. var TO_STRING = 'toString';
  7. var RegExpPrototype = RegExp.prototype;
  8. var nativeToString = RegExpPrototype[TO_STRING];
  9. var NOT_GENERIC = fails(function () { return nativeToString.call({ source: 'a', flags: 'b' }) != '/a/b'; });
  10. // FF44- RegExp#toString has a wrong name
  11. var INCORRECT_NAME = nativeToString.name != TO_STRING;
  12. // `RegExp.prototype.toString` method
  13. // https://tc39.github.io/ecma262/#sec-regexp.prototype.tostring
  14. if (NOT_GENERIC || INCORRECT_NAME) {
  15. redefine(RegExp.prototype, TO_STRING, function toString() {
  16. var R = anObject(this);
  17. var p = String(R.source);
  18. var rf = R.flags;
  19. var f = String(rf === undefined && R instanceof RegExp && !('flags' in RegExpPrototype) ? flags.call(R) : rf);
  20. return '/' + p + '/' + f;
  21. }, { unsafe: true });
  22. }