MCPcopy Create free account
hub / github.com/TanStack/db / getId

Method getId

packages/db-ivm/src/utils.ts:94–112  ·  view source on GitHub ↗

* Get a unique identifier for any value. * - Objects: Uses WeakMap for reference-based identity * - Primitives: Uses consistent string-based hashing

(value: any)

Source from the content-addressed store, hash-verified

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.

Callers 1

getStringIdMethod · 0.95

Calls 3

hasMethod · 0.45
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected