TypeScript: Apply profile updates

Apply a list of updates to a user profile, refusing changes the type does not allow.

What to build

A program that updates a profile and prints it.

Requirements

  • Declare Profile with a readonly id, a name, optional email and age, and tags: string[].
  • Updates are name=V, email=V, age=V, tags+=T (add T if not there) and tags-=T (remove T if there).
  • Print id is read-only for id=..., age must be a whole number if V is not a whole number (the age is not changed), and unknown field F for any other field name F.
  • Finally print id: ..., name: ..., email: ..., age: ... and tags: ... (tags joined by , ), with (none) for a missing email or age and for no tags.

Input

Line 1: the id and the name, separated by a space (the name may contain spaces). Line 2: m. Then m updates.

Output

The messages, then the five lines.

Constraints

0 ≤ m ≤ 50; an update has = in it.

Example

Input:

7 Ada Lovelace
4
email=ada@example.com
tags+=math
tags+=poetry
id=8

Output:

id is read-only
id: 7
name: Ada Lovelace
email: ada@example.com
age: (none)
tags: math, poetry

Why it works

The type documents which fields exist and which are fixed, so the update code has one branch per field, rejects the rest, and the readonly id is never assigned anywhere after the object is made.

Examples

Example 1 · Input
7 Ada Lovelace
4
email=ada@example.com
tags+=math
tags+=poetry
id=8
Expected output
id is read-only
id: 7
name: Ada Lovelace
email: ada@example.com
age: (none)
tags: math, poetry
Example 2 · Input
1 Alan
3
age=41
age=forty
phone=555
Expected output
age must be a whole number
unknown field phone
id: 1
name: Alan
email: (none)
age: 41
tags: (none)

Hints

Hint 1

interface Profile { readonly id: number; name: string; email?: string; age?: number; tags: string[] }.

Hint 2

Handle each field in its own branch; keep a fixed order when printing.

Solution

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

interface Profile {
  readonly id: number;
  name: string;
  email?: string;
  age?: number;
  tags: string[];
}

const first = read();
const space = first.indexOf(" ");
const profile: Profile = { id: Number(first.slice(0, space)), name: first.slice(space + 1), tags: [] };
const m = Number(read());
for (let i = 0; i < m; i++) {
  const line = read();
  const at = line.indexOf("=");
  const field = line.slice(0, at);
  const value = line.slice(at + 1);
  if (field === "name") profile.name = value;
  else if (field === "email") profile.email = value;
  else if (field === "age") {
    if (/^\d+$/.test(value)) profile.age = Number(value);
    else console.log("age must be a whole number");
  } else if (field === "tags+") {
    if (!profile.tags.includes(value)) profile.tags.push(value);
  } else if (field === "tags-") {
    profile.tags = profile.tags.filter((t) => t !== value);
  } else if (field === "id") console.log("id is read-only");
  else console.log("unknown field " + field);
}
console.log("id: " + profile.id);
console.log("name: " + profile.name);
console.log("email: " + (profile.email ?? "(none)"));
console.log("age: " + (profile.age ?? "(none)"));
console.log("tags: " + (profile.tags.length ? profile.tags.join(", ") : "(none)"));

Lesson: Object types and interfaces →