MCPcopy Create free account
hub / github.com/Effect-TS/effect / hash

Function hash

packages/effect/src/Hash.ts:120–185  ·  view source on GitHub ↗
(self: A)

Source from the content-addressed store, hash-verified

118 * @since 2.0.0
119 */
120export const hash: <A>(self: A) => number = <A>(self: A) => {
121 switch (typeof self) {
122 case "number":
123 return number(self)
124 case "bigint":
125 return string(self.toString(10))
126 case "string":
127 return string(self)
128 case "function":
129 case "object": {
130 if (self === null) {
131 break
132 }
133 const kind = objectKind(self)
134 if (kind === 1) {
135 if (Number.isNaN((self as any as Date).getTime())) {
136 return string("Invalid Date")
137 }
138 return string((self as any as Date).toISOString())
139 } else if (kind === 2) {
140 return string((self as any as RegExp).toString())
141 } else {
142 if (byReferenceInstances.has(self)) {
143 return random(self)
144 }
145 const cached = hashCache.get(self)
146 if (cached !== undefined) {
147 return cached
148 }
149 if (visitedObjects.has(self)) {
150 addBackEdge()
151 return string("[Circular]")
152 }
153 visitedObjects.add(self)
154 const seen = backEdges
155 let h: number
156 try {
157 if (isHash(self)) {
158 h = (self as Hash)[symbol]()
159 } else if (typeof self === "function") {
160 h = random(self)
161 } else if (kind >= 7) {
162 const bytes = toBytes(self as any, kind)
163 h = bytes === undefined ? random(self) : hashIndexed(bytes)
164 } else if (kind >= 5) {
165 h = hashIndexed(self as any)
166 } else if (kind === 3) {
167 h = hashUnordered(mapSeed, self as any as Map<unknown, unknown>, hashMapEntry)
168 } else if (kind === 4) {
169 h = hashUnordered(setSeed, self as any as Set<unknown>, hashSetElement)
170 } else {
171 h = isDomReference(self) ? random(self) : structure(self)
172 }
173 } finally {
174 visitedObjects.delete(self)
175 }
176 // Hashes containing a back-edge depend on the entry point.
177 if (seen === backEdges) {

Callers 5

structureKeysFunction · 0.70
arrayFunction · 0.70
hashIndexedFunction · 0.70
hashMapEntryFunction · 0.70
hashSetElementFunction · 0.70

Calls 15

objectKindFunction · 0.90
addBackEdgeFunction · 0.90
toBytesFunction · 0.90
isDomReferenceFunction · 0.90
mixFunction · 0.90
isHashFunction · 0.85
hashIndexedFunction · 0.85
hashUnorderedFunction · 0.85
optimizeFunction · 0.85
getTimeMethod · 0.80
numberFunction · 0.70
stringFunction · 0.70

Tested by

no test coverage detected