MCPcopy Index your code
hub / github.com/Effect-TS/effect / hash

Function hash

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

Source from the content-addressed store, hash-verified

31 * @category hashing
32 */
33export const hash: <A>(self: A) => number = <A>(self: A) => {
34 if (structuralRegionState.enabled === true) {
35 return 0
36 }
37
38 switch (typeof self) {
39 case "number":
40 return number(self)
41 case "bigint":
42 return string(self.toString(10))
43 case "boolean":
44 return string(String(self))
45 case "symbol":
46 return string(String(self))
47 case "string":
48 return string(self)
49 case "undefined":
50 return string("undefined")
51 case "function":
52 case "object": {
53 if (self === null) {
54 return string("null")
55 } else if (self instanceof Date) {
56 if (Number.isNaN(self.getTime())) {
57 return string("Invalid Date")
58 }
59 return hash(self.toISOString())
60 } else if (self instanceof URL) {
61 return hash(self.href)
62 } else if (isHash(self)) {
63 return self[symbol]()
64 } else {
65 return random(self)
66 }
67 }
68 default:
69 throw new Error(
70 `BUG: unhandled typeof ${typeof self} - please report an issue at https://github.com/Effect-TS/effect/issues`
71 )
72 }
73}
74
75/**
76 * @since 2.0.0

Callers 3

structureKeysFunction · 0.70
arrayFunction · 0.70

Calls 6

StringInterface · 0.85
isHashFunction · 0.85
randomFunction · 0.85
numberFunction · 0.70
stringFunction · 0.70
toStringMethod · 0.65

Tested by

no test coverage detected