mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-31 22:32:28 +00:00
29 lines
824 B
TypeScript
29 lines
824 B
TypeScript
import { sortBy, pipe } from "remeda"
|
|
|
|
export namespace Wildcard {
|
|
export function match(str: string, pattern: string) {
|
|
const regex = new RegExp(
|
|
"^" +
|
|
pattern
|
|
.replace(/[.+^${}()|[\]\\]/g, "\\$&") // escape special regex chars
|
|
.replace(/\*/g, ".*") // * becomes .*
|
|
.replace(/\?/g, ".") + // ? becomes .
|
|
"$",
|
|
"s", // s flag enables multiline matching
|
|
)
|
|
return regex.test(str)
|
|
}
|
|
|
|
export function all(input: string, patterns: Record<string, any>) {
|
|
const sorted = pipe(patterns, Object.entries, sortBy([([key]) => key.length, "asc"], [([key]) => key, "asc"]))
|
|
let result = undefined
|
|
for (const [pattern, value] of sorted) {
|
|
if (match(input, pattern)) {
|
|
result = value
|
|
continue
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
}
|