levtor
Version:
Levtor — a smart, fuzzy, AI-powered chat command handler and guardian for wallets and AI integration.
30 lines (26 loc) • 1.04 kB
text/typescript
// Utils: arg parser and fuzzy match
export function parseArgs(input: string): { positional: string[]; named: Record<string, string | boolean> } {
const positional: string[] = [];
const named: Record<string, string | boolean> = {};
const regex = /"([^"]+)"|'([^']+)'|(\S+)/g;
let match: RegExpExecArray | null = regex.exec(input);
while (match !== null) {
const token: string | undefined = match[1] || match[2] || match[3];
if (token?.includes('=')) {
const [key, val] = token.split('=');
if (key) named[key.toLowerCase()] = val ?? true;
} else if (token) {
positional.push(token);
}
match = regex.exec(input);
}
return { positional, named };
}
export function fuzzyMatch(input: string, options: string[]): string | null {
const lowerInput = input.toLowerCase();
// Exact match
if (options.includes(lowerInput)) return lowerInput;
// Simple substring match
const found = options.find(opt => opt.startsWith(lowerInput) || opt.includes(lowerInput));
return found || null;
}