recipes

Short and checked

Count words

const words = "one two three".split(" ").length;

De-duplicate a list

const unique = [...new Set([1, 2, 2, 3])];

Safe dictionary read

const value = menu[key] ?? "unknown";

Group by a field

const items = [
  { kind: "tcp", port: 22 },
  { kind: "udp", port: 53 },
  { kind: "tcp", port: 443 }
];

const grouped = items.reduce((acc, item) => {
  (acc[item.kind] ??= []).push(item.port);
  return acc;
}, {});

Retry with a limit

async function retry(fn, times) {
  let last;
  for (let i = 0; i < times; i++) {
    try { return await fn(); }
    catch (err) { last = err; }
  }
  throw last;
}

No network call was made while checking these The logic was run with local promises only

End

Gave me framing Neither gets to speak instead of me

Posted