mirror of
https://github.com/thangisme/notes.git
synced 2024-11-09 07:47:30 -05:00
32 lines
651 B
JavaScript
32 lines
651 B
JavaScript
'use strict';
|
|
var isRegexp = require('is-regexp');
|
|
var isSupportedRegexpFlag = require('is-supported-regexp-flag');
|
|
|
|
var flagMap = {
|
|
global: 'g',
|
|
ignoreCase: 'i',
|
|
multiline: 'm'
|
|
};
|
|
|
|
if (isSupportedRegexpFlag('y')) {
|
|
flagMap.sticky = 'y';
|
|
}
|
|
|
|
if (isSupportedRegexpFlag('u')) {
|
|
flagMap.unicode = 'u';
|
|
}
|
|
|
|
module.exports = function (re, opts) {
|
|
if (!isRegexp(re)) {
|
|
throw new TypeError('Expected a RegExp instance');
|
|
}
|
|
|
|
opts = opts || {};
|
|
|
|
var flags = Object.keys(flagMap).map(function (el) {
|
|
return (typeof opts[el] === 'boolean' ? opts[el] : re[el]) ? flagMap[el] : '';
|
|
}).join('');
|
|
|
|
return new RegExp(opts.source || re.source, flags);
|
|
};
|