TypeScript: Process a log of typed events

Parse a log into a discriminated union of events and work out what happened.

What to build

A program that replays login, logout, purchase and error events and prints a summary.

Requirements

  • Parse each line into an Event union: login USER, logout USER, buy USER AMOUNT, error CODE.
  • A buy counts only if the user is logged in at that moment; otherwise print ignored: USER is not logged in.
  • At the end, for every user who ever logged in, in alphabetical order, print USER: spent TOTAL, logged in or USER: spent TOTAL, logged out (their state at the end).
  • Then print errors: CODE, CODE, ... in the order they happened, or errors: none.

Input

Line 1: n. Then n events.

Output

The ignored purchases, the users, then the errors.

Constraints

1 ≤ n ≤ 100; amounts are whole numbers; all lines are valid events.

Example

Input:

6
login ada
buy ada 30
buy alan 10
login alan
logout ada
error E42

Output:

ignored: alan is not logged in
ada: spent 30, logged out
alan: spent 0, logged in
errors: E42

Why it works

Each kind of event carries exactly its own fields, so the replay is one switch on type with a branch per kind, and TypeScript checks that a purchase's amount is read only from a buy event.

Examples

Example 1 · Input
6
login ada
buy ada 30
buy alan 10
login alan
logout ada
error E42
Expected output
ignored: alan is not logged in
ada: spent 30, logged out
alan: spent 0, logged in
errors: E42
Example 2 · Input
2
error E1
error E2
Expected output
errors: E1, E2

Hints

Hint 1

type Event = { type: "login"; user: string } | { type: "logout"; user: string } | { type: "buy"; user: string; amount: number } | { type: "error"; code: string }.

Hint 2

Keep who is logged in in a Set<string> and the totals in a Map<string, number>.

Solution

Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";

type Event =
  | { type: "login"; user: string }
  | { type: "logout"; user: string }
  | { type: "buy"; user: string; amount: number }
  | { type: "error"; code: string };

function parse(line: string): Event {
  const [type, a, b] = line.split(" ");
  if (type === "login") return { type, user: a };
  if (type === "logout") return { type, user: a };
  if (type === "buy") return { type, user: a, amount: Number(b) };
  return { type: "error", code: a };
}

const online = new Set<string>();
const spent = new Map<string, number>();
const errors: string[] = [];
const n = Number(read());
for (let i = 0; i < n; i++) {
  const event = parse(read());
  switch (event.type) {
    case "login":
      online.add(event.user);
      if (!spent.has(event.user)) spent.set(event.user, 0);
      break;
    case "logout":
      online.delete(event.user);
      break;
    case "buy":
      if (online.has(event.user)) spent.set(event.user, (spent.get(event.user) ?? 0) + event.amount);
      else console.log("ignored: " + event.user + " is not logged in");
      break;
    case "error":
      errors.push(event.code);
      break;
  }
}
for (const user of [...spent.keys()].sort()) {
  console.log(user + ": spent " + spent.get(user) + ", " + (online.has(user) ? "logged in" : "logged out"));
}
console.log("errors: " + (errors.length ? errors.join(", ") : "none"));

Lesson: Type aliases, unions and intersections →