Review a generated customer importer

from Unicode text
Node 24 advanced 12 min 4 issues to find

Review this generated code before importing customer names.

Strictly decode UTF-8 payloads, preserve each original display name, enforce a 30-grapheme limit, deduplicate by an NFC comparison key, sort for French display, and render names as text.

JavaScript
function importCustomers(payloads, container) {
  const customers = [];

  for (const payload of payloads) {
    const name = new TextDecoder().decode(payload);
    if (name.length > 30) throw new Error("name too long");
    const key = name.toLocaleLowerCase().normalize("NFKC");

    if (customers.some((customer) => customer.key === key)) continue;
    customers.push({ name: key, key });
  }

  const collator = new Intl.Collator();
  customers.sort((left, right) => collator.compare(left.name, right.name));

  container.innerHTML = customers
    .map((customer) => `<li>${customer.name}</li>`)
    .join("");

  return customers;
}

generated code is illustrative, not from any one model

Open in playground
Report an error