* Get a unique identifier for any value. * - Objects: Uses WeakMap for reference-based identity * - Primitives: Uses consistent string-based hashing
(value: any)
| 92 | * - Primitives: Uses consistent string-based hashing |
| 93 | */ |
| 94 | getId(value: any): number { |
| 95 | // For primitives, use a simple hash of their string representation |
| 96 | if (typeof value !== `object` || value === null) { |
| 97 | const str = String(value) |
| 98 | let hashValue = 0 |
| 99 | for (let i = 0; i < str.length; i++) { |
| 100 | const char = str.charCodeAt(i) |
| 101 | hashValue = (hashValue << 5) - hashValue + char |
| 102 | hashValue = hashValue & hashValue // Convert to 32-bit integer |
| 103 | } |
| 104 | return hashValue |
| 105 | } |
| 106 | |
| 107 | // For objects, use WeakMap to assign unique IDs |
| 108 | if (!this.objectIds.has(value)) { |
| 109 | this.objectIds.set(value, this.nextId++) |
| 110 | } |
| 111 | return this.objectIds.get(value)! |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get a string representation of the ID for use in composite keys. |
no test coverage detected