iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / internal-packages / installer / worker / domain-names.jsmain at aacbc74

// domain-names.js — THE NAMES THE WIZARD OFFERS A PERSON WHO HAS NO DOMAIN YET, from their name
// and a few words (`POST /api/suggest` in index.js asks Cloudflare Registrar which of them can be
// bought, and for how much).

/** The most names that one question to the Registrar takes. */
const MOST = 20;

/** Lower case, no accents, letters, digits and spaces alone. */
const clean = (text) =>
  text
    .toLowerCase()
    .normalize("NFKD")
    .replace(/[^a-z0-9 ]/g, "")
    .trim();

/** A word made to end in "erate", as "iterate" does: one final a, e, i, o, u or y goes first,
 *  when more than that letter is there (misha gives misherate, jonas gives jonaserate). */
export const erateOf = (word) => `${word.length > 1 ? word.replace(/[aeiouy]$/, "") : word}erate`;

/** Names a person might like, from their name and a few words, `MOST` at most. The "erate" names
 *  of their first and last name come first, as .com and .dev; then the other names, .com first. */
export function candidatesOf(name, words) {
  const parts = clean(name).split(/\s+/).filter(Boolean);
  const extra = clean(words).split(/\s+/).filter(Boolean);
  const [first, last] = [parts[0], parts.at(-1) !== parts[0] ? parts.at(-1) : undefined];
  const erate = [first, last].filter(Boolean).map(erateOf);
  const stems = new Set();
  if (first && last)
    [`${first}${last}`, `${first}-${last}`, `${first}${last[0]}`].forEach((s) => stems.add(s));
  if (last) stems.add(last);
  if (first) [`${first}s`, `hey${first}`, `${first}hq`].forEach((s) => stems.add(s));
  for (const w of extra) {
    stems.add(w);
    if (first) stems.add(`${first}${w}`);
  }
  const out = [];
  for (const stem of stems)
    for (const tld of ["com", "dev", "net", "xyz"]) out.push(`${stem}.${tld}`);
  return [
    ...new Set([
      ...["com", "dev"].flatMap((tld) => erate.map((stem) => `${stem}.${tld}`)),
      ...out.filter((d) => d.endsWith(".com")),
      ...out.filter((d) => !d.endsWith(".com")),
    ]),
  ].slice(0, MOST);
}