generateFullMatchRegExp.js 335 B

12345678910
  1. module.exports = function generateFullMatchRegExp(source) {
  2. if (source instanceof RegExp) {
  3. return source;
  4. }
  5. if (typeof source !== 'string') {
  6. throw new Error('generateFullMatchRegExp: expect string but get', source);
  7. }
  8. // allow dot ahead
  9. return new RegExp(`(^|\\.)${source}${source.endsWith('$') ? '' : '$'}`);
  10. };