@baseworks/core 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ // src/id.ts
2
+ function generateId() {
3
+ const now = BigInt(Date.now());
4
+ const bytes = crypto.getRandomValues(new Uint8Array(16));
5
+ bytes[0] = Number(now >> 40n & 0xffn);
6
+ bytes[1] = Number(now >> 32n & 0xffn);
7
+ bytes[2] = Number(now >> 24n & 0xffn);
8
+ bytes[3] = Number(now >> 16n & 0xffn);
9
+ bytes[4] = Number(now >> 8n & 0xffn);
10
+ bytes[5] = Number(now & 0xffn);
11
+ bytes[6] = bytes[6] & 15 | 112;
12
+ bytes[8] = bytes[8] & 63 | 128;
13
+ const h = (s) => Array.from(s, (b) => b.toString(16).padStart(2, "0")).join("");
14
+ return [
15
+ h(bytes.slice(0, 4)),
16
+ h(bytes.slice(4, 6)),
17
+ h(bytes.slice(6, 8)),
18
+ h(bytes.slice(8, 10)),
19
+ h(bytes.slice(10))
20
+ ].join("-");
21
+ }
22
+ function generateSlug(name) {
23
+ return name.toLowerCase().normalize("NFD").replace(/[̀-ͯ]/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").replace(/-{2,}/g, "-").slice(0, 48) || "untitled";
24
+ }
25
+ var SHORT_ID_ALPHABET = "bcdfghjkmnpqrstvwxyz2345678";
26
+ var SHORT_ID_LENGTH = 6;
27
+ function generateShortId() {
28
+ const bytes = crypto.getRandomValues(new Uint8Array(SHORT_ID_LENGTH));
29
+ return Array.from(bytes, (b) => SHORT_ID_ALPHABET[b % SHORT_ID_ALPHABET.length]).join("");
30
+ }
31
+
32
+ export {
33
+ generateId,
34
+ generateSlug,
35
+ generateShortId
36
+ };
package/dist/id.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * ID generation utilities.
3
+ * Runtime-agnostic — works in Cloudflare Workers, Node 20+, browser.
4
+ */
5
+ declare function generateId(): string;
6
+ declare function generateSlug(name: string): string;
7
+ declare function generateShortId(): string;
8
+
9
+ export { generateId, generateShortId, generateSlug };
package/dist/id.js ADDED
@@ -0,0 +1,10 @@
1
+ import {
2
+ generateId,
3
+ generateShortId,
4
+ generateSlug
5
+ } from "./chunk-PRA7F4WX.js";
6
+ export {
7
+ generateId,
8
+ generateShortId,
9
+ generateSlug
10
+ };
@@ -0,0 +1 @@
1
+ export { generateId, generateShortId, generateSlug } from './id.js';
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ import {
2
+ generateId,
3
+ generateShortId,
4
+ generateSlug
5
+ } from "./chunk-PRA7F4WX.js";
6
+ export {
7
+ generateId,
8
+ generateShortId,
9
+ generateSlug
10
+ };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@baseworks/core",
3
+ "version": "0.2.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./dist/index.js",
7
+ "./id": "./dist/id.js"
8
+ },
9
+ "files": ["dist", "src"],
10
+ "scripts": {
11
+ "build": "tsup",
12
+ "typecheck": "tsc --noEmit"
13
+ },
14
+ "devDependencies": {
15
+ "@baseworks/tsconfig": "workspace:*",
16
+ "tsup": "^8.5.1",
17
+ "typescript": "^5.6.0"
18
+ }
19
+ }
package/src/id.ts ADDED
@@ -0,0 +1,57 @@
1
+ /**
2
+ * ID generation utilities.
3
+ * Runtime-agnostic — works in Cloudflare Workers, Node 20+, browser.
4
+ */
5
+
6
+ // UUIDv7: 48-bit Unix ms timestamp prefix + random bits.
7
+ // Time-sortable, DB-index friendly, RFC 9562 compliant.
8
+ // Default for all primary keys unless a project overrides.
9
+ export function generateId(): string {
10
+ const now = BigInt(Date.now());
11
+ const bytes = crypto.getRandomValues(new Uint8Array(16));
12
+
13
+ bytes[0] = Number((now >> 40n) & 0xffn);
14
+ bytes[1] = Number((now >> 32n) & 0xffn);
15
+ bytes[2] = Number((now >> 24n) & 0xffn);
16
+ bytes[3] = Number((now >> 16n) & 0xffn);
17
+ bytes[4] = Number((now >> 8n) & 0xffn);
18
+ bytes[5] = Number(now & 0xffn);
19
+ bytes[6] = (bytes[6]! & 0x0f) | 0x70; // version 7
20
+ bytes[8] = (bytes[8]! & 0x3f) | 0x80; // variant 10xx
21
+
22
+ const h = (s: Uint8Array) =>
23
+ Array.from(s, (b: number) => b.toString(16).padStart(2, '0')).join('');
24
+
25
+ return [
26
+ h(bytes.slice(0, 4)),
27
+ h(bytes.slice(4, 6)),
28
+ h(bytes.slice(6, 8)),
29
+ h(bytes.slice(8, 10)),
30
+ h(bytes.slice(10)),
31
+ ].join('-');
32
+ }
33
+
34
+ // Converts a display name to a URL-safe slug.
35
+ // e.g. "My Project 2" → "my-project-2"
36
+ export function generateSlug(name: string): string {
37
+ return name
38
+ .toLowerCase()
39
+ .normalize('NFD') // decompose accented chars
40
+ .replace(/[̀-ͯ]/g, '') // strip combining marks
41
+ .replace(/[^a-z0-9]+/g, '-') // non-alphanumeric → dash
42
+ .replace(/^-+|-+$/g, '') // trim leading/trailing dashes
43
+ .replace(/-{2,}/g, '-') // collapse consecutive dashes
44
+ .slice(0, 48) // max length
45
+ || 'untitled';
46
+ }
47
+
48
+ // 6-char short ID for human-readable identifiers (slugs, labels, URL segments).
49
+ // Alphabet: lowercase + digits, ambiguous chars removed (0, 1, i, l, o).
50
+ // 27^6 ≈ 387M combinations — collision-safe for org/workspace/project counts.
51
+ const SHORT_ID_ALPHABET = 'bcdfghjkmnpqrstvwxyz2345678';
52
+ const SHORT_ID_LENGTH = 6;
53
+
54
+ export function generateShortId(): string {
55
+ const bytes = crypto.getRandomValues(new Uint8Array(SHORT_ID_LENGTH));
56
+ return Array.from(bytes, (b: number) => SHORT_ID_ALPHABET[b % SHORT_ID_ALPHABET.length]).join('');
57
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { generateId, generateShortId, generateSlug } from './id';