MCPcopy
hub / github.com/angular/angular / globToRegex

Function globToRegex

packages/service-worker/config/src/glob.ts:21–45  ·  view source on GitHub ↗
(glob: string, literalQuestionMark = false)

Source from the content-addressed store, hash-verified

19const TO_ESCAPE_LITERAL_QM = [...TO_ESCAPE_BASE, {replace: /\?/g, with: '\\?'}];
20
21export function globToRegex(glob: string, literalQuestionMark = false): string {
22 const toEscape = literalQuestionMark ? TO_ESCAPE_LITERAL_QM : TO_ESCAPE_WILDCARD_QM;
23 const segments = glob.split('/').reverse();
24 let regex: string = '';
25 while (segments.length > 0) {
26 const segment = segments.pop()!;
27 if (segment === '**') {
28 if (segments.length > 0) {
29 regex += WILD_OPEN;
30 } else {
31 regex += '.*';
32 }
33 } else {
34 const processed = toEscape.reduce(
35 (segment, escape) => segment.replace(escape.replace, escape.with),
36 segment,
37 );
38 regex += processed;
39 if (segments.length > 0) {
40 regex += '\\/';
41 }
42 }
43 }
44 return regex;
45}

Callers 2

globListToMatcherFunction · 0.90
urlToRegexFunction · 0.90

Calls 4

reverseMethod · 0.80
popMethod · 0.80
reduceMethod · 0.80
replaceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…