cuid2 collision-resistant ids: working versions (tested 2026-10-15)
Generate collision-resistant ids with @paralleldrive/cuid2: default createId, init with a custom length, isCuid validation and uniqueness across many ids offline.
passed5 passed · 0 failed2026-10-15 22:58 UTC- Status
- passed
- Last passed
- 2026-10-15 22:58 UTC
- Tests
- 5 / 5
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
@paralleldrive/cuid2@3.3.0@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 38820570375
The code
Exactly the files that passed. Your agent can pull them with get_recipe("cuid2-collision-resistant-ids") (install), or see them on GitHub.
package.json
{
"name": "cuid2-collision-resistant-ids",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"@paralleldrive/cuid2": "3.3.0"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/ids.ts
import { createId, init, isCuid } from "@paralleldrive/cuid2";
export { createId, isCuid };
export function createIdWithLength(length: number): () => string {
return init({ length });
}
export function generateMany(count: number, makeId: () => string = createId): string[] {
return Array.from({ length: count }, () => makeId());
}
test/ids.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { createId, createIdWithLength, generateMany, isCuid } from "../src/ids.ts";
test("createId returns a 24-character cuid", () => {
const id = createId();
assert.equal(typeof id, "string");
assert.equal(id.length, 24);
});
test("isCuid accepts generated ids", () => {
for (const id of generateMany(50)) {
assert.equal(isCuid(id), true);
}
});
test("isCuid rejects malformed strings", () => {
assert.equal(isCuid(""), false);
assert.equal(isCuid("UPPERCASE-NOT-ALLOWED"), false);
assert.equal(isCuid("has spaces in it"), false);
assert.equal(isCuid("!@#$%^&*()"), false);
assert.equal(isCuid("a".repeat(40)), false);
});
test("init generates ids with a custom length", () => {
const makeShortId = createIdWithLength(10);
const ids = generateMany(25, makeShortId);
for (const id of ids) {
assert.equal(id.length, 10);
}
});
test("ids are unique across many generations", () => {
const count = 10_000;
const ids = generateMany(count);
assert.equal(new Set(ids).size, count);
});
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.