* Convert a PB `~`/`!~` LIKE pattern into an anchored RegExp, faithful to * PocketBase 0.22's semantics: the SQL is built as `LIKE ... ESCAPE '\'` * (pocketbase tools/search/filter.go), so a `\`-escaped `%`/`_` is a LITERAL * character while unescaped `%`/`_` are wildcards. The fakes must honor t
(pattern: string)
| 148 | * just the least-surprising fallback, not a verified PB contract. |
| 149 | */ |
| 150 | function likeToRegExp(pattern: string): RegExp { |
| 151 | const escapeRegExp = (ch: string): string => |
| 152 | ch.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 153 | let out = ""; |
| 154 | for (let i = 0; i < pattern.length; i++) { |
| 155 | const ch = pattern[i]; |
| 156 | if (ch === "\\" && i + 1 < pattern.length) { |
| 157 | out += escapeRegExp(pattern[i + 1]); |
| 158 | i += 1; |
| 159 | } else if (ch === "%") { |
| 160 | out += ".*"; |
| 161 | } else if (ch === "_") { |
| 162 | out += "."; |
| 163 | } else { |
| 164 | out += escapeRegExp(ch); |
| 165 | } |
| 166 | } |
| 167 | return new RegExp(`^${out}$`); |
| 168 | } |
| 169 | |
| 170 | /** The row shape the shared filter matcher evaluates. */ |
| 171 | interface FilterableRow { |
no test coverage detected
searching dependent graphs…