index.js 698 B

123456789101112131415161718192021222324252627
  1. const toString = Object.prototype.toString;
  2. function isRegExp (o) {
  3. return 'object' == typeof o
  4. && '[object RegExp]' == toString.call(o);
  5. }
  6. module.exports = exports = function (regexp) {
  7. if (!isRegExp(regexp)) {
  8. throw new TypeError('Not a RegExp');
  9. }
  10. const flags = [];
  11. if (regexp.global) flags.push('g');
  12. if (regexp.multiline) flags.push('m');
  13. if (regexp.ignoreCase) flags.push('i');
  14. if (regexp.dotAll) flags.push('s');
  15. if (regexp.unicode) flags.push('u');
  16. if (regexp.sticky) flags.push('y');
  17. const result = new RegExp(regexp.source, flags.join(''));
  18. if (typeof regexp.lastIndex === 'number') {
  19. result.lastIndex = regexp.lastIndex;
  20. }
  21. return result;
  22. }