GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — opencode/packages/opencode/src/util/wildcard.ts at dev · kasbah/opencode Expand file tree/ Copy pathwildcard.tsMore file actions 54 lines (50 loc) · 1.72 KB / Copy pathwildcard.tsFile metadata and controls54 lines (50 loc) · 1.72 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 } export function allStructured(input: { head: string; tail: 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) { const parts = pattern.split(/\s+/) if (!match(input.head, parts[0])) continue if (parts.length === 1 || matchSequence(input.tail, parts.slice(1))) { result = value continue } } return result } function matchSequence(items: string[], patterns: string[]): boolean { if (patterns.length === 0) return true const [pattern, ...rest] = patterns if (pattern === "*") return matchSequence(items, rest) for (let i = 0; i < items.length; i++) { if (match(items[i], pattern) && matchSequence(items.slice(i + 1), rest)) { return true } } return false } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |