All files / webview/search regexUtils.ts

100% Statements 9/9
100% Branches 8/8
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21              26x       30x 28x 28x 30x 28x   2x      
export interface RegexOptions {
  caseSensitive: boolean;
  wholeWord: boolean;
  useRegex: boolean;
}
 
export function escapeRegExp(s: string): string {
  return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
 
export function buildSearchRegex(query: string, opts: RegexOptions): RegExp | null {
  if (!query) return null;
  try {
    let pattern = opts.useRegex ? query : escapeRegExp(query);
    if (opts.wholeWord) pattern = `\\b${pattern}\\b`;
    return new RegExp(pattern, opts.caseSensitive ? 'g' : 'gi');
  } catch {
    return null;
  }
}