function globStringToRegex()

in src/services/form-validation/utils/mime.ts [37:71]


function globStringToRegex(str: string) {
  str = str.replace(/\s/g, '');

  let regexp = '',
    excludes: string[] = [];
  if (str.length > 2 && str[0] === '/' && str[str.length - 1] === '/') {
    regexp = str.substring(1, str.length - 1);
  } else {
    const split = str.split(',');
    if (split.length > 1) {
      for (let i = 0; i < split.length; i++) {
        const r = globStringToRegex(split[i]);
        if (r.regexp) {
          regexp += `(${r.regexp})`;
          if (i < split.length - 1) {
            regexp += '|';
          }
        } else {
          excludes = excludes.concat(r.excludes);
        }
      }
    } else {
      if (str.startsWith('!')) {
        excludes.push(`^((?!${globStringToRegex(str.substring(1)).regexp}).)*$`);
      } else {
        if (str.startsWith('.')) {
          str = `*${str}`;
        }
        regexp = `^${str.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]', 'g'), '\\$&')}$`;
        regexp = regexp.replace(/\\\*/g, '.*').replace(/\\\?/g, '.');
      }
    }
  }
  return { regexp, excludes };
}