Generator: make several server names at once
A "How many" field (1-20) next to Generate gives that many different names, none already used by a server and none twice; a list with a Copy button per name and a "Copy all" appears when more than one is made. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7eee807d9e
commit
75f060049f
5 files changed
+135
-19
No files matched your search
@@ -229,7 +229,9 @@ that carries the tag.
|
||||
passwords, which are made in your browser and never stored). Server names are picked from
|
||||
name lists: Swedish girl and boy names, Disney and Pixar characters, Norse mythology and
|
||||
Astrid Lindgren to start with, or "Mixed" for all of them together. A name already used
|
||||
by a server isn't suggested. Admins edit the lists under Settings → Names — add and remove
|
||||
by a server isn't suggested. "How many" makes up to 20 different names at once (handy when
|
||||
setting up several machines), each with its own Copy button and a "Copy all" that puts
|
||||
them one per line on the clipboard. Admins edit the lists under Settings → Names — add and remove
|
||||
names, rename, delete or create lists, put a built-in list back as it was — and can
|
||||
import the most common Swedish names from Skatteverket's open name statistics for
|
||||
girls or boys over the latest one to five years. The import is shown to you first and only
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Server names from the Generator: one at a time, or several different ones in one go. The pure picker lives in the web's own source file and is run here against it.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { join } from "node:path";
|
||||
|
||||
const web = (file: string) => pathToFileURL(join(import.meta.dirname, "../../web/src/utils", file)).href;
|
||||
const { generateServerName, generateServerNames, MAX_SERVER_NAMES } = (await import(web("generators.ts"))) as typeof import("../../web/src/utils/generators");
|
||||
|
||||
const pool = ["freja", "elsa", "alice", "maja", "wilma", "alma", "ebba", "lilly", "ella", "saga"];
|
||||
|
||||
describe("generateServerNames", () => {
|
||||
test("gives the number asked for, all different, all from the pool", () => {
|
||||
for (let round = 0; round < 50; round++) {
|
||||
const names = generateServerNames(pool, [], 6);
|
||||
assert.equal(names.length, 6);
|
||||
assert.equal(new Set(names).size, 6);
|
||||
for (const n of names) assert.ok(pool.includes(n));
|
||||
}
|
||||
});
|
||||
|
||||
test("leaves out the names a server already has, whatever the case", () => {
|
||||
for (let round = 0; round < 50; round++) {
|
||||
const names = generateServerNames(pool, ["Freja", "ELSA", "alice"], 7);
|
||||
assert.equal(names.length, 7, "exactly the seven that are left");
|
||||
assert.deepEqual([...names].sort(), ["alma", "ebba", "ella", "lilly", "maja", "saga", "wilma"]);
|
||||
}
|
||||
});
|
||||
|
||||
test("numbers the rest once the list has run out, still all different and none taken", () => {
|
||||
const names = generateServerNames(["maja", "saga"], ["maja2"], 6);
|
||||
assert.equal(names.length, 6);
|
||||
assert.equal(new Set(names.map((n) => n.toLowerCase())).size, 6);
|
||||
assert.ok(names.includes("maja") && names.includes("saga"), "the plain ones come first");
|
||||
assert.ok(!names.includes("maja2"), "maja2 belongs to a server");
|
||||
for (const n of names.slice(2)) assert.match(n, /^(maja|saga)\d+$/);
|
||||
});
|
||||
|
||||
test("a name written twice in the pool is still given once", () => {
|
||||
const names = generateServerNames(["maja", "Maja", "saga"], [], 2);
|
||||
assert.deepEqual([...names].map((n) => n.toLowerCase()).sort(), ["maja", "saga"]);
|
||||
});
|
||||
|
||||
test("asks for too many, too few or nonsense: the count is held to 1–20", () => {
|
||||
assert.equal(generateServerNames(pool, [], 500).length, MAX_SERVER_NAMES);
|
||||
assert.equal(generateServerNames(pool, [], 0).length, 1);
|
||||
assert.equal(generateServerNames(pool, [], -3).length, 1);
|
||||
assert.equal(generateServerNames(pool, [], Number.NaN).length, 1);
|
||||
assert.equal(generateServerNames(pool, [], 2.9).length, 2);
|
||||
});
|
||||
|
||||
test("an empty pool gives nothing, and the one-name function still works", () => {
|
||||
assert.deepEqual(generateServerNames([], [], 5), []);
|
||||
assert.equal(generateServerName([], []), "");
|
||||
assert.ok(pool.includes(generateServerName(pool, [])));
|
||||
assert.equal(generateServerName(["maja"], ["maja"]), "maja2");
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useState } from "react";
|
||||
|
||||
export default function CopyButton({ text }: { text: string }) {
|
||||
export default function CopyButton({ text, label = "Copy" }: { text: string; label?: string }) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
|
||||
async function handleCopy() {
|
||||
@@ -15,7 +15,7 @@ export default function CopyButton({ text }: { text: string }) {
|
||||
|
||||
return (
|
||||
<button type="button" className="btn btn-sm btn-outline-secondary" onClick={handleCopy}>
|
||||
{copied ? "Copied!" : "Copy"}
|
||||
{copied ? "Copied!" : label}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
@@ -4,7 +4,8 @@ import { api, type CurrentUser, type NameTheme } from "../api/client";
|
||||
import CopyButton from "../components/CopyButton";
|
||||
import { readableError } from "../utils/errors";
|
||||
import {
|
||||
generateServerName,
|
||||
MAX_SERVER_NAMES,
|
||||
generateServerNames,
|
||||
generateUsername,
|
||||
generatePassword,
|
||||
passwordEntropyBits,
|
||||
@@ -22,7 +23,8 @@ export default function Generator({ user }: { user: CurrentUser }) {
|
||||
const [themes, setThemes] = useState<NameTheme[] | null>(null);
|
||||
const [themesError, setThemesError] = useState<string | null>(null);
|
||||
const [theme, setTheme] = useState<string>(MIXED);
|
||||
const [serverName, setServerName] = useState("");
|
||||
const [serverNames, setServerNames] = useState<string[]>([]);
|
||||
const [nameCount, setNameCount] = useState(1);
|
||||
|
||||
useEffect(() => {
|
||||
api.servers
|
||||
@@ -48,7 +50,7 @@ export default function Generator({ user }: { user: CurrentUser }) {
|
||||
}, [themes, theme]);
|
||||
|
||||
function rollServerName() {
|
||||
setServerName(generateServerName(pool, existingServerNames));
|
||||
setServerNames(generateServerNames(pool, existingServerNames, nameCount));
|
||||
}
|
||||
|
||||
// ─── Username ────────────────────────────────────────────────────────
|
||||
@@ -115,15 +117,43 @@ export default function Generator({ user }: { user: CurrentUser }) {
|
||||
))}
|
||||
</select>
|
||||
<div className="form-hint mb-3">{themes ? `${pool.length} name${pool.length === 1 ? "" : "s"} to pick from.` : "Loading the lists…"}</div>
|
||||
<div className="input-group">
|
||||
<input type="text" className="form-control" readOnly value={serverName} placeholder="Click Generate…" aria-label="Generated server name" />
|
||||
{serverName && <CopyButton text={serverName} />}
|
||||
</div>
|
||||
{serverNames.length > 1 ? (
|
||||
<ul className="list-group">
|
||||
{serverNames.map((n) => (
|
||||
<li key={n} className="list-group-item d-flex align-items-center justify-content-between py-1">
|
||||
<span className="font-monospace">{n}</span>
|
||||
<CopyButton text={n} />
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : (
|
||||
<div className="input-group">
|
||||
<input type="text" className="form-control" readOnly value={serverNames[0] ?? ""} placeholder="Click Generate…" aria-label="Generated server name" />
|
||||
{serverNames[0] && <CopyButton text={serverNames[0]} />}
|
||||
</div>
|
||||
)}
|
||||
{serverNames.length > 1 && (
|
||||
<div className="mt-2">
|
||||
<CopyButton text={serverNames.join("\n")} label="Copy all" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="card-footer">
|
||||
<div className="card-footer d-flex flex-wrap align-items-center gap-2">
|
||||
<button className="btn btn-primary" onClick={rollServerName} disabled={pool.length === 0}>
|
||||
Generate
|
||||
</button>
|
||||
<label className="d-flex align-items-center gap-2 mb-0 ms-auto">
|
||||
<span className="text-secondary">How many</span>
|
||||
<input
|
||||
type="number"
|
||||
className="form-control"
|
||||
style={{ width: 80 }}
|
||||
min={1}
|
||||
max={MAX_SERVER_NAMES}
|
||||
value={nameCount}
|
||||
onChange={(e) => setNameCount(Math.min(MAX_SERVER_NAMES, Math.max(1, Math.floor(Number(e.target.value)) || 1)))}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -21,16 +21,38 @@ function pick<T>(list: T[]): T {
|
||||
* fall back to numbering one — maja2, maja3, …. The pools themselves are the name lists under Settings → Names.
|
||||
*/
|
||||
export function generateServerName(pool: string[], existing: string[] = []): string {
|
||||
if (pool.length === 0) return "";
|
||||
return generateServerNames(pool, existing, 1)[0] ?? "";
|
||||
}
|
||||
|
||||
/** Most names the Generator makes in one go. */
|
||||
export const MAX_SERVER_NAMES = 20;
|
||||
|
||||
/**
|
||||
* `count` different names, none already in `existing` and none twice (case-insensitively). They're drawn from the pool without
|
||||
* putting one back; once the pool is used up the rest are numbered ones — maja2, maja3, ….
|
||||
*/
|
||||
export function generateServerNames(pool: string[], existing: string[] = [], count = 1): string[] {
|
||||
if (pool.length === 0) return [];
|
||||
const wanted = Math.min(MAX_SERVER_NAMES, Math.max(1, Math.floor(count) || 1));
|
||||
const used = new Set(existing.map((n) => n.toLowerCase()));
|
||||
const available = pool.filter((n) => !used.has(n.toLowerCase()));
|
||||
if (available.length > 0) return pick(available);
|
||||
const base = pick(pool);
|
||||
for (let suffix = 2; suffix < 1000; suffix++) {
|
||||
const candidate = `${base}${suffix}`;
|
||||
if (!used.has(candidate.toLowerCase())) return candidate;
|
||||
const available = pool.filter((n, i) => !used.has(n.toLowerCase()) && pool.findIndex((m) => m.toLowerCase() === n.toLowerCase()) === i);
|
||||
const names: string[] = [];
|
||||
while (names.length < wanted) {
|
||||
let name: string | undefined;
|
||||
if (available.length > 0) {
|
||||
name = available.splice(randomInt(available.length), 1)[0];
|
||||
} else {
|
||||
const base = pick(pool);
|
||||
for (let suffix = 2; suffix < 1000 && name === undefined; suffix++) {
|
||||
const candidate = `${base}${suffix}`;
|
||||
if (!used.has(candidate.toLowerCase())) name = candidate;
|
||||
}
|
||||
name ??= `${base}${randomInt(100000)}`;
|
||||
}
|
||||
used.add(name.toLowerCase());
|
||||
names.push(name);
|
||||
}
|
||||
return `${base}${randomInt(100000)}`;
|
||||
return names;
|
||||
}
|
||||
|
||||
// ─── Usernames ───────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user