wisesplit/lib/parseInput.tsx

64 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-10-24 07:10:52 +00:00
export interface ParsedInput {
itemName: string;
price?: number;
splitBetween: Set<string>;
}
2022-10-25 18:26:48 +00:00
export interface Props {
input: string;
}
export function ParsedInputDisplay({ input }: Props) {
2022-10-24 07:10:52 +00:00
const parsed = parseInput(input);
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
return (
<div style={{ fontSize: ".8em" }}>
<div>
<b>{parsed.itemName}</b>
{parsed.price !== undefined && <>({formatter.format(parsed.price)})</>}
</div>
{parsed.splitBetween.size > 0 && (
<>
Split between:
<ul>
{[...parsed.splitBetween].map((personName) => (
<li key={personName}>{personName}</li>
))}
</ul>
</>
)}
</div>
);
}
export default function parseInput(line: string): ParsedInput {
const words = line.split(" ");
let price = undefined;
const splitBetween = new Set<string>();
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 };
}