Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

src/guest-names.ts gives each anonymous viewer a tasteful colour-and-animal name, stable per browser, never held by two at once - module and test only #311

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

smith/arm:smith/290-src-guest-names-ts-gives-each-anonymous → main 1 commit fork 1 thread · 1 unresolved

  • Merged. smith/290-src-guest-names-ts-gives-each-anonymous was merged into main as 30708a9.

Review

Sign in to review this change.
2 files changed +88 −0
Software/so101/src/guest-names.test.ts added +42
@@ -0,0 +1,42 @@
1 import { describe, expect, test } from "bun:test";
2 import { ANIMALS, COLOURS, nameFor } from "./guest-names.ts";
3
4 const colours: string[] = COLOURS.map(([c]) => c);
5 const animals: string[] = [...ANIMALS];
6
7 describe("nameFor", () => {
8 test("the same id gets the same name again", () => {
9 expect(nameFor("browser-1")).toEqual(nameFor("browser-1"));
10 });
11
12 test("two ids get different names when the first is in use", () => {
13 const a = nameFor("browser-1");
14 expect(nameFor("browser-1", [a.name]).name).not.toBe(a.name);
15 });
16
17 test("never returns a name in use, even for the whole grid but one", () => {
18 const all = colours.flatMap((c) => animals.map((a) => `${c} ${a}`));
19 const free = all[37]!;
20 expect(nameFor("x", all.filter((n) => n !== free)).name).toBe(free);
21 });
22
23 test("every name comes from the lists and the dot colour is a hex", () => {
24 const used: string[] = [];
25 for (let i = 0; i < 200; i++) {
26 const g = nameFor(`id-${i}`, used);
27 const [c, a] = g.name.split(" ");
28 expect(colours).toContain(c!);
29 expect(animals).toContain(a!);
30 expect(g.colour).toMatch(/^#[0-9a-f]{6}$/);
31 used.push(g.name);
32 }
33 expect(new Set(used).size).toBe(200);
34 });
35
36 test("a full grid numbers the name instead of repeating it", () => {
37 const all = colours.flatMap((c) => animals.map((a) => `${c} ${a}`));
38 const g = nameFor("x", all);
39 expect(all).not.toContain(g.name);
40 expect(g.name).toMatch(/ 2$/);
41 });
42 });
Software/so101/src/guest-names.ts added +46
@@ -0,0 +1,46 @@
1 /**
2 * Names for anonymous viewers: a colour and a nature noun ("Teal Heron"), stable per browser id and never
3 * held by two at once. The id picks a starting point in the colour x animal grid; if that name is taken the
4 * next free one is used. Only when all of the grid is taken does a number follow ("Teal Heron 2").
5 */
6
7 export const COLOURS = [
8 ["Amber", "#f59e0b"], ["Azure", "#3b82f6"], ["Coral", "#f87171"], ["Crimson", "#dc2626"],
9 ["Gold", "#eab308"], ["Indigo", "#6366f1"], ["Jade", "#10b981"], ["Lilac", "#c084fc"],
10 ["Olive", "#84cc16"], ["Rose", "#f472b6"], ["Rust", "#c2410c"], ["Sage", "#8fae8b"],
11 ["Slate", "#64748b"], ["Teal", "#14b8a6"], ["Violet", "#8b5cf6"], ["Willow", "#a3b18a"],
12 ] as const;
13
14 export const ANIMALS = [
15 "Badger", "Crane", "Egret", "Falcon", "Fox", "Heron", "Ibis", "Kestrel",
16 "Lynx", "Marten", "Newt", "Otter", "Owl", "Plover", "Swift", "Wren",
17 ] as const;
18
19 export interface GuestName {
20 name: string;
21 /** CSS colour for the dot beside the name. */
22 colour: string;
23 }
24
25 /** FNV-1a: small, deterministic, enough spread for picking a start. */
26 function hash(s: string): number {
27 let h = 0x811c9dc5;
28 for (let i = 0; i < s.length; i++) h = Math.imul(h ^ s.charCodeAt(i), 0x01000193);
29 return h >>> 0;
30 }
31
32 /** The name for this browser, skipping any name in `inUse`. The same id with the same `inUse` gets the same name. */
33 export function nameFor(browserId: string, inUse: Iterable<string> = []): GuestName {
34 const taken = new Set(inUse);
35 const total = COLOURS.length * ANIMALS.length;
36 const start = hash(browserId) % total;
37 for (let i = 0; i < total; i++) {
38 const n = (start + i) % total;
39 const [colour, hex] = COLOURS[n % COLOURS.length]!;
40 const name = `${colour} ${ANIMALS[Math.floor(n / COLOURS.length)]}`;
41 if (!taken.has(name)) return { name, colour: hex };
42 }
43 const [colour, hex] = COLOURS[start % COLOURS.length]!;
44 const base = `${colour} ${ANIMALS[Math.floor(start / COLOURS.length)]}`;
45 for (let k = 2; ; k++) if (!taken.has(`${base} ${k}`)) return { name: `${base} ${k}`, colour: hex };

The k loop is untested: with the whole grid and "base 2" taken it should return "base 3". Add one test line for it.

46 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs