Skip to content

Commit 8fcd64e

Browse files
authored
Feat/uuidv5 generation (#61)
* feat: setup uuidv5 * chore: add test for deterministic UUID
1 parent e9681af commit 8fcd64e

File tree

4 files changed

+427
-84
lines changed

4 files changed

+427
-84
lines changed

infrastructure/w3id/package.json

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
11
{
2-
"name": "w3id",
3-
"version": "1.0.0",
4-
"description": "",
5-
"scripts": {
6-
"test": "echo \"Error: no test specified\" && exit 1",
7-
"dev": "tsc --watch",
8-
"format": "npx @biomejs/biome format --write ./src",
9-
"check-format": "npx @biomejs/biome format ./src",
10-
"lint": "npx @biomejs/biome lint --write ./src",
11-
"check-lint": "npx @biomejs/biome lint ./src",
12-
"check": "npx @biomejs/biome check ./src",
13-
"check-types": "tsc --noEmit",
14-
"build": "npm run build:node && npm run build:browser",
15-
"build:node": "tsc -p tsconfig.node.json",
16-
"build:browser": "tsc -p tsconfig.browser.json",
17-
"postinstall": "npm run build"
18-
},
19-
"keywords": [],
20-
"author": "",
21-
"license": "ISC",
22-
"dependencies": {
23-
"@biomejs/biome": "^1.9.4",
24-
"uuid": "^11.1.0"
25-
},
26-
"devDependencies": {
27-
"typescript": "^5.8.2"
28-
},
29-
"main": "./dist/node/index.js",
30-
"module": "./dist/browser/index.js",
31-
"types": "./dist/types/index.d.ts",
32-
"exports": {
33-
".": {
34-
"require": "./dist/node/index.js",
35-
"import": "./dist/browser/index.js",
36-
"types": "./dist/types/index.d.ts"
37-
},
38-
"./package.json": "./package.json"
39-
},
40-
"files": ["dist/**/*"]
2+
"name": "w3id",
3+
"version": "1.0.0",
4+
"description": "",
5+
"scripts": {
6+
"test": "vitest",
7+
"dev": "tsc --watch",
8+
"check-format": "prettier --check \"src/**/*.ts\"",
9+
"format": "npx @biomejs/biome format --write ./src",
10+
"lint": "npx @biomejs/biome lint --write ./src",
11+
"check": "npx @biomejs/biome check --write ./src",
12+
"check-types": "tsc --noEmit",
13+
"build": "npm run build:node && npm run build:browser",
14+
"build:node": "tsc -p tsconfig.node.json",
15+
"build:browser": "tsc -p tsconfig.browser.json",
16+
"postinstall": "npm run build"
17+
},
18+
"keywords": [],
19+
"author": "",
20+
"license": "ISC",
21+
"dependencies": {
22+
"uuid": "^11.1.0"
23+
},
24+
"devDependencies": {
25+
"@ngneat/falso": "^7.3.0",
26+
"@types/node": "^22.13.10",
27+
"typescript": "^5.8.2",
28+
"vitest": "^3.0.9"
29+
},
30+
"main": "./dist/node/index.js",
31+
"module": "./dist/browser/index.js",
32+
"types": "./dist/types/index.d.ts",
33+
"exports": {
34+
".": {
35+
"require": "./dist/node/index.js",
36+
"import": "./dist/browser/index.js",
37+
"types": "./dist/types/index.d.ts"
38+
},
39+
"./package.json": "./package.json"
40+
},
41+
"files": [
42+
"dist/**/*"
43+
]
4144
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { v4 as uuidv4, v5 as uuidv5 } from "uuid";
2+
3+
/**
4+
* Generates a UUIDv5 taking namespace from a random UUIDv4 and taking `name`
5+
* part as entropy from the creator of the identifier
6+
*
7+
* @param {String} entropy
8+
* @param {String} namespace - uuid namespace
9+
* @returns string
10+
*/
11+
12+
export function generateUuid(
13+
entropy: string,
14+
namespace: string = uuidv4(),
15+
): string {
16+
return uuidv5(entropy, namespace);
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { generateUuid } from "../../src/utils/uuid";
2+
import falso from "@ngneat/falso";
3+
import { describe, test, expect } from "vitest";
4+
5+
describe("UUIDv5 Generation", () => {
6+
test("Create UUID", () => {
7+
const id = generateUuid(falso.randText());
8+
expect(id).toBeDefined();
9+
});
10+
11+
test("UUID is deterministic", () => {
12+
const namespace = falso.randUuid();
13+
const entropy = falso.randText();
14+
const id = generateUuid(entropy, namespace);
15+
const id2 = generateUuid(entropy, namespace);
16+
expect(id).toEqual(id2);
17+
});
18+
});

0 commit comments

Comments
 (0)