Function
codeToRegex
(fn, includeSignature = false)
Source from the content-addressed store, hash-verified
| 366 | } |
| 367 | } |
| 368 | return str; |
| 369 | } |
| 370 | |
| 371 | function codeToRegex(fn, includeSignature = false) { |
| 372 | return new RegExp( |
| 373 | fn |
| 374 | .toString() |
| 375 | .split("\n") |
| 376 | .reduce((acc, line, index, array) => { |
| 377 | if (includeSignature || (index > 0 && index < array.length - 1)) { |
| 378 | acc.push(line.trim()); |
| 379 | } |
| 380 | return acc; |
| 381 | }, []) |
| 382 | .join("\n") |
| 383 | .replace(/\s+/g, "\\s*") |
| 384 | .replace(/\(/g, "\\(") |
| 385 | .replace(/\)/g, "\\)") |
| 386 | .replace(/\./g, "\\.") |
| 387 | .replace(/\?,/g, "\\?") |
| 388 | .replace(/\,/g, ",?") |
| 389 | .replace(/\$/g, "\\$") |
| 390 | .replace(/\{/g, "\\s*{") |
| 391 | .replace(/\}/g, "}\\s*") |
| 392 | .replace(/\|/g, "\\|"), |
| 393 | "g" |
| 394 | ); |
| 395 | } |
Tested by
no test coverage detected