|
| 1 | +// Reference: https://github.com/gobwas/glob/blob/master/glob.go |
| 2 | +// |
| 3 | +// Compile creates Glob for given pattern and strings (if any present after pattern) as separators. |
| 4 | +// The pattern syntax is: |
| 5 | +// |
| 6 | +// pattern: |
| 7 | +// { term } |
| 8 | +// |
| 9 | +// term: |
| 10 | +// `*` matches any sequence of non-separator characters |
| 11 | +// `**` matches any sequence of characters |
| 12 | +// `?` matches any single non-separator character |
| 13 | +// `[` [ `!` ] { character-range } `]` |
| 14 | +// character class (must be non-empty) |
| 15 | +// `{` pattern-list `}` |
| 16 | +// pattern alternatives |
| 17 | +// c matches character c (c != `*`, `**`, `?`, `\`, `[`, `{`, `}`) |
| 18 | +// `\` c matches character c |
| 19 | +// |
| 20 | +// character-range: |
| 21 | +// c matches character c (c != `\\`, `-`, `]`) |
| 22 | +// `\` c matches character c |
| 23 | +// lo `-` hi matches character c for lo <= c <= hi |
| 24 | +// |
| 25 | +// pattern-list: |
| 26 | +// pattern { `,` pattern } |
| 27 | +// comma-separated (without spaces) patterns |
| 28 | +// |
| 29 | + |
| 30 | +class GlobCompiler { |
| 31 | + nonSeparatorChars: string; |
| 32 | + globPattern: string; |
| 33 | + regexpPattern: string; |
| 34 | + regexp: RegExp; |
| 35 | + pos: number = 0; |
| 36 | + |
| 37 | + #compileChars(): string { |
| 38 | + let result = ''; |
| 39 | + if (this.globPattern[this.pos] === '!') { |
| 40 | + this.pos++; |
| 41 | + result += '^'; |
| 42 | + } |
| 43 | + while (this.pos < this.globPattern.length) { |
| 44 | + const c = this.globPattern[this.pos]; |
| 45 | + this.pos++; |
| 46 | + if (c === ']') { |
| 47 | + return `[${result}]`; |
| 48 | + } |
| 49 | + if (c === '\\') { |
| 50 | + if (this.pos >= this.globPattern.length) { |
| 51 | + throw new Error('Unterminated character class escape'); |
| 52 | + } |
| 53 | + this.pos++; |
| 54 | + result += `\\${this.globPattern[this.pos]}`; |
| 55 | + } else { |
| 56 | + result += c; |
| 57 | + } |
| 58 | + } |
| 59 | + throw new Error('Unterminated character class'); |
| 60 | + } |
| 61 | + |
| 62 | + #compile(subPattern: boolean = false): string { |
| 63 | + let result = ''; |
| 64 | + let hasSubGroups = false; |
| 65 | + while (this.pos < this.globPattern.length) { |
| 66 | + const c = this.globPattern[this.pos]; |
| 67 | + this.pos++; |
| 68 | + if (subPattern && c === '}') { |
| 69 | + return result; |
| 70 | + } |
| 71 | + switch (c) { |
| 72 | + case '*': |
| 73 | + if (this.globPattern[this.pos] !== '*') { |
| 74 | + result += `${this.nonSeparatorChars}*`; // match any sequence of non-separator characters |
| 75 | + } else { |
| 76 | + this.pos++; |
| 77 | + result += '.*'; // match any sequence of characters |
| 78 | + } |
| 79 | + break; |
| 80 | + case '?': |
| 81 | + result += this.nonSeparatorChars; // match any single non-separator character |
| 82 | + break; |
| 83 | + case '[': |
| 84 | + result += this.#compileChars(); |
| 85 | + break; |
| 86 | + case '{': |
| 87 | + result += this.#compile(true); |
| 88 | + hasSubGroups = true; |
| 89 | + break; |
| 90 | + case ',': |
| 91 | + result += subPattern ? '|' : ','; |
| 92 | + break; |
| 93 | + case '\\': |
| 94 | + if (this.pos >= this.globPattern.length) { |
| 95 | + throw new Error('No character to escape'); |
| 96 | + } |
| 97 | + this.pos++; |
| 98 | + result += `\\${this.globPattern[this.pos]}`; |
| 99 | + break; |
| 100 | + case '.': case '+': case '^': case '$': case '(': case ')': case '|': |
| 101 | + result += `\\${c}`; // escape regexp special characters |
| 102 | + break; |
| 103 | + default: |
| 104 | + result += c; |
| 105 | + } |
| 106 | + } |
| 107 | + return hasSubGroups ? `^(${result})$` : `^${result}$`; |
| 108 | + } |
| 109 | + |
| 110 | + constructor(pattern: string, separators: string = '') { |
| 111 | + const escapedSeparators = separators.replaceAll(/[\^\]\-\\]/g, '\\$&'); |
| 112 | + this.nonSeparatorChars = escapedSeparators ? `[^${escapedSeparators}]` : '.'; |
| 113 | + this.globPattern = pattern; |
| 114 | + this.regexpPattern = this.#compile(); |
| 115 | + this.regexp = new RegExp(`^${this.regexpPattern}$`); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +export function globCompile(pattern: string, separators: string = ''): GlobCompiler { |
| 120 | + return new GlobCompiler(pattern, separators); |
| 121 | +} |
| 122 | + |
| 123 | +export function globMatch(str: string, pattern: string, separators: string = ''): boolean { |
| 124 | + try { |
| 125 | + return globCompile(pattern, separators).regexp.test(str); |
| 126 | + } catch { |
| 127 | + return false; |
| 128 | + } |
| 129 | +} |
0 commit comments