(main: string, sub: string)
| 461 | }; |
| 462 | |
| 463 | export const stringMatching = (main: string, sub: string): boolean => { |
| 464 | // If no wildcards, use simple includes check |
| 465 | if (!sub.includes("*") && !sub.includes("?")) { |
| 466 | return main.includes(sub); |
| 467 | } |
| 468 | |
| 469 | // Escape special regex characters except * and ? |
| 470 | const escapeRegex = (str: string) => str.replace(/[-[\]{}()+.,\\^$|#\s]/g, "\\$&"); |
| 471 | |
| 472 | // Convert glob pattern to regex |
| 473 | let pattern = escapeRegex(sub) |
| 474 | .replace(/\*/g, "\\S*") // * matches zero or more non-space characters |
| 475 | .replace(/\?/g, "\\S"); // ? matches exactly one non-space character |
| 476 | |
| 477 | // Anchor the pattern to match entire string |
| 478 | pattern = `\\b${pattern}\\b`; |
| 479 | |
| 480 | try { |
| 481 | // Create regex and test against main string |
| 482 | const regex = new RegExp(pattern); |
| 483 | return regex.test(main); |
| 484 | } catch (e) { |
| 485 | console.error(e); |
| 486 | // Handle invalid regex patterns |
| 487 | return false; |
| 488 | } |
| 489 | }; |
| 490 | |
| 491 | /** |
| 492 | * 将字节数转换为人类可读的格式(B, KB, MB, GB 等)。 |
no test coverage detected