export interface ParsedInput { itemName: string; price?: number; splitBetween: Set; } export interface Props { input: string; } export function ParsedInputDisplay({ input }: Props) { const parsed = parseInput(input); const formatter = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", }); return (
{parsed.itemName} {parsed.price !== undefined && <>({formatter.format(parsed.price)})}
{parsed.splitBetween.size > 0 && ( <> Split between: )}
); } export default function parseInput(line: string): ParsedInput { const words = line.split(" "); let price = undefined; const splitBetween = new Set(); const final = []; for (let word of words) { if (word.startsWith("$") && word.length > 1) { price = parseFloat(word.slice(1)); continue; } if (word.startsWith("@") && word.length > 1) { splitBetween.add(word.slice(1)); continue; } final.push(word); } const itemName = final.join(" "); return { itemName, price, splitBetween }; }