Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions infrastructure/w3id/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "vitest",
"dev": "tsc --watch",
"check-format": "prettier --check \"src/**/*.ts\"",
"format": "npx @biomejs/biome format --write ./src",
Expand All @@ -22,7 +22,10 @@
"uuid": "^11.1.0"
},
"devDependencies": {
"typescript": "^5.8.2"
"@ngneat/falso": "^7.3.0",
"@types/node": "^22.13.10",
"typescript": "^5.8.2",
"vitest": "^3.0.9"
},
"main": "./dist/node/index.js",
"module": "./dist/browser/index.js",
Expand Down
17 changes: 17 additions & 0 deletions infrastructure/w3id/src/utils/uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { v4 as uuidv4, v5 as uuidv5 } from "uuid";

/**
* Generates a UUIDv5 taking namespace from a random UUIDv4 and taking `name`
* part as entropy from the creator of the identifier
*
* @param {String} entropy
* @param {String} namespace - uuid namespace
* @returns string
*/

export function generateUuid(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function name makes it seems like the function has a broad scope, but doc above mentions just v5, its just a nitpick but i think this can be named a little better

entropy: string,
namespace: string = uuidv4(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think having this default behavior would just lead to mistakes

): string {
return uuidv5(entropy, namespace);
}
18 changes: 18 additions & 0 deletions infrastructure/w3id/tests/utils/uuid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { generateUuid } from "../../src/utils/uuid";
import falso from "@ngneat/falso";
import { describe, test, expect } from "vitest";

describe("UUIDv5 Generation", () => {
test("Create UUID", () => {
const id = generateUuid(falso.randText());
expect(id).toBeDefined();
});

test("UUID is deterministic", () => {
const namespace = falso.randUuid();
const entropy = falso.randText();
const id = generateUuid(entropy, namespace);
const id2 = generateUuid(entropy, namespace);
expect(id).toEqual(id2);
});
});
Loading