20 lines
656 B
TypeScript
20 lines
656 B
TypeScript
type ParsedInput =
|
|
| { type: "message"; content: string }
|
|
| { type: "command"; name: string; argument: string };
|
|
|
|
export function parseInput(value: string): ParsedInput | undefined {
|
|
const input = value.trim();
|
|
|
|
if (!input) return;
|
|
if (!input.startsWith("/")) return { type: "message", content: input };
|
|
if (input.startsWith("//")) {
|
|
return { type: "message", content: input.slice(1) };
|
|
}
|
|
|
|
const separator = input.search(/\s/);
|
|
const name = input.slice(1, separator < 0 ? undefined : separator).toLowerCase();
|
|
const argument = separator < 0 ? "" : input.slice(separator + 1).trim();
|
|
|
|
return { type: "command", name, argument };
|
|
}
|