Line data Source code
1 : /**
2 : * FNV-1a hash function for creating short, deterministic keys from strings.
3 : *
4 : * Used to create 8-character hex keys from workflow paths for localStorage keys.
5 : * FNV-1a is chosen for its simplicity, speed, and good distribution properties.
6 : *
7 : * @param str - The string to hash (typically a workflow path)
8 : * @returns A 32-bit unsigned integer hash
9 : */
10 278588 : export function fnv1a(str: string): number {
11 278588 : let hash = 2166136261
12 278588 : for (let i = 0; i < str.length; i++) {
13 11353552 : hash ^= str.charCodeAt(i)
14 11353552 : hash = Math.imul(hash, 16777619)
15 : }
16 278588 : return hash >>> 0
17 : }
18 :
19 : /**
20 : * Creates an 8-character hex key from a workflow path using FNV-1a hash.
21 : *
22 : * @param path - The workflow path (e.g., "workflows/My Workflow.json")
23 : * @returns An 8-character hex string (e.g., "a1b2c3d4")
24 : *
25 : * @example
26 : * hashPath("workflows/Untitled.json") // "1a2b3c4d"
27 : */
28 278588 : export function hashPath(path: string): string {
29 278588 : return fnv1a(path).toString(16).padStart(8, '0')
30 : }
|