nanoid custom alphabets, sizes and non-secure ids: working versions (tested 2026-10-15)
Generate URL-safe ids with nanoid, custom alphabets via customAlphabet, fixed sizes, and nonSecure for sync contexts, asserting uniqueness and alphabet constraints.
passed3 passed · 0 failed2026-10-15 16:02 UTC- Status
- passed
- Last passed
- 2026-10-15 16:02 UTC
- Tests
- 3 / 3
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
nanoid@6.0.2@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38764699834
The code
Exactly the files that passed. Your agent can pull them with get_recipe("nanoid-custom-alphabet") (install), or see them on GitHub.
package.json
{
"name": "nanoid-custom-alphabet",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"nanoid": "6.0.2"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/ids.ts
import { nanoid, customAlphabet } from "nanoid";
import { nanoid as nonSecure } from "nanoid/non-secure";
export const orderId = customAlphabet("0123456789ABCDEFGHJKLMNPQRSTUVWXYZ", 10);
export const shortCode = customAlphabet("abcdefghijkmnopqrstuvwxyz", 6);
export function newSessionId() {
return nanoid(21);
}
export function newTempId() {
return nonSecure(12);
}
export function assertAlphabet(id: string, alphabet: string) {
for (const ch of id) if (!alphabet.includes(ch)) throw new Error(`bad char ${ch}`);
}
test/ids.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { assertAlphabet, newSessionId, newTempId, orderId, shortCode } from "../src/ids.ts";
test("session ids are unique and fixed length", () => {
const a = new Set(Array.from({ length: 50 }, () => newSessionId()));
assert.equal(a.size, 50);
for (const id of a) assert.equal(id.length, 21);
});
test("custom alphabets stick to their charset", () => {
const o = orderId();
assert.equal(o.length, 10);
assertAlphabet(o, "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ");
const s = shortCode();
assert.equal(s.length, 6);
assertAlphabet(s, "abcdefghijkmnopqrstuvwxyz");
});
test("nonSecure works synchronously", () => {
assert.equal(newTempId().length, 12);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.