summaryrefslogtreecommitdiffstats
path: root/node_modules/regexp-clone/index.js
blob: 19512b392f608dd4ea99bbed9449053750ed1811 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const toString = Object.prototype.toString;

function isRegExp (o) {
  return 'object' == typeof o
      && '[object RegExp]' == toString.call(o);
}

module.exports = exports = function (regexp) {
  if (!isRegExp(regexp)) {
    throw new TypeError('Not a RegExp');
  }

  const flags = [];
  if (regexp.global) flags.push('g');
  if (regexp.multiline) flags.push('m');
  if (regexp.ignoreCase) flags.push('i');
  if (regexp.dotAll) flags.push('s');
  if (regexp.unicode) flags.push('u');
  if (regexp.sticky) flags.push('y');
  const result = new RegExp(regexp.source, flags.join(''));
  if (typeof regexp.lastIndex === 'number') {
    result.lastIndex = regexp.lastIndex;
  }
  return result;
}