MCPcopy Create free account
hub / github.com/SethGammon/Citadel / globToRegex

Function globToRegex

hooks_src/organize-enforce.js:63–80  ·  view source on GitHub ↗

* Minimatch-lite: convert a simple glob to a regex. * Supports *, **, ?, and {a,b} brace expansion (one level). * Not a full glob engine -- covers the patterns used in placement rules.

(glob)

Source from the content-addressed store, hash-verified

61 * Not a full glob engine -- covers the patterns used in placement rules.
62 */
63function globToRegex(glob) {
64 // Expand braces: *.{ts,tsx} -> (.*\.ts|.*\.tsx)
65 const braceMatch = glob.match(/^(.*)\{([^}]+)\}(.*)$/);
66 if (braceMatch) {
67 const [, prefix, options, suffix] = braceMatch;
68 const alts = options.split(',').map(opt => globToRegex(prefix + opt + suffix).source);
69 return new RegExp('^(' + alts.join('|') + ')$');
70 }
71
72 let re = glob
73 .replace(/\./g, '\\.')
74 .replace(/\*\*/g, '__DOUBLESTAR__')
75 .replace(/\*/g, '[^/]*')
76 .replace(/__DOUBLESTAR__/g, '.*')
77 .replace(/\?/g, '[^/]');
78
79 return new RegExp('^' + re + '$');
80}
81
82/**
83 * Check if a file path is under a given root directory.

Callers 2

matchesPathPatternFunction · 0.70
mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected