<A>(self: A): numberComputes a hash value for any given value.
When to use
Use to compute an Effect hash for primitives, collections, and hashable objects.
Details
This function can hash primitives (numbers, strings, booleans, etc.) as well as objects, arrays, and other complex data structures. It automatically handles different types and provides a consistent hash value for equivalent inputs.
Gotchas
Objects being hashed must be treated as immutable after their first hash
computation. Hash results are cached, so mutating an object after hashing will
lead to stale cached values and broken hash-based operations. For mutable
objects, implement a custom Hash interface that hashes the object reference
rather than its content.
Example (Hashing different values)
import { Hash } from "effect"
// Hash primitive values
console.log(Hash.hash(42)) // numeric hash
console.log(Hash.hash("hello")) // string hash
console.log(Hash.hash(true)) // boolean hash
// Hash objects and arrays
console.log(Hash.hash({ name: "John", age: 30 }))
console.log(Hash.hash([1, 2, 3]))
console.log(Hash.hash({ id: "user-1", roles: ["admin", "editor"] }))export const const hash: <A>(self: A) => numberComputes a hash value for any given value.
When to use
Use to compute an Effect hash for primitives, collections, and hashable
objects.
Details
This function can hash primitives (numbers, strings, booleans, etc.) as well as
objects, arrays, and other complex data structures. It automatically handles
different types and provides a consistent hash value for equivalent inputs.
Gotchas
Objects being hashed must be treated as immutable after their first hash
computation. Hash results are cached, so mutating an object after hashing will
lead to stale cached values and broken hash-based operations. For mutable
objects, implement a custom Hash interface that hashes the object reference
rather than its content.
Example (Hashing different values)
import { Hash } from "effect"
// Hash primitive values
console.log(Hash.hash(42)) // numeric hash
console.log(Hash.hash("hello")) // string hash
console.log(Hash.hash(true)) // boolean hash
// Hash objects and arrays
console.log(Hash.hash({ name: "John", age: 30 }))
console.log(Hash.hash([1, 2, 3]))
console.log(Hash.hash({ id: "user-1", roles: ["admin", "editor"] }))
hash: <function (type parameter) A in <A>(self: A): numberA>(self: Aself: function (type parameter) A in <A>(self: A): numberA) => number = <function (type parameter) A in <A>(self: A): numberA>(self: Aself: function (type parameter) A in <A>(self: A): numberA) => {
switch (typeof self: Aself) {
case "number":
return const number: (n: number) => numberComputes a hash value for a number.
When to use
Use to hash a JavaScript number with Effect's numeric hash semantics.
Details
This function creates a hash value for numeric inputs, handling special cases
like NaN, Infinity, and -Infinity with distinct hash values. It uses bitwise operations to ensure good distribution
of hash values across different numeric inputs.
Example (Hashing numbers)
import { Hash } from "effect"
console.log(Hash.number(42)) // hash of 42
console.log(Hash.number(3.14)) // hash of 3.14
console.log(Hash.number(NaN)) // hash of "NaN"
console.log(Hash.number(Infinity)) // 0 (special case)
// Same numbers produce the same hash
console.log(Hash.number(100) === Hash.number(100)) // true
number(self: A & numberself)
case "bigint":
return const string: (str: string) => numberComputes a hash value for a string using the djb2 algorithm.
When to use
Use when you need a string field to contribute to a custom structural hash
implementation.
Details
This function implements a variation of the djb2 hash algorithm, which is
known for its good distribution properties and speed. It processes each
character of the string to produce a consistent hash value.
Example (Hashing strings)
import { Hash } from "effect"
console.log(Hash.string("hello")) // hash of "hello"
console.log(Hash.string("world")) // hash of "world"
console.log(Hash.string("")) // hash of empty string
// Same strings produce the same hash
console.log(Hash.string("test") === Hash.string("test")) // true
string(self: A & bigintself.BigInt.toString(radix?: number): stringReturns a string representation of an object.
toString(10))
case "boolean":
return const string: (str: string) => numberComputes a hash value for a string using the djb2 algorithm.
When to use
Use when you need a string field to contribute to a custom structural hash
implementation.
Details
This function implements a variation of the djb2 hash algorithm, which is
known for its good distribution properties and speed. It processes each
character of the string to produce a consistent hash value.
Example (Hashing strings)
import { Hash } from "effect"
console.log(Hash.string("hello")) // hash of "hello"
console.log(Hash.string("world")) // hash of "world"
console.log(Hash.string("")) // hash of empty string
// Same strings produce the same hash
console.log(Hash.string("test") === Hash.string("test")) // true
string(var String: StringConstructor
;(value?: any) => string
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String(self: A & booleanself))
case "symbol":
return const string: (str: string) => numberComputes a hash value for a string using the djb2 algorithm.
When to use
Use when you need a string field to contribute to a custom structural hash
implementation.
Details
This function implements a variation of the djb2 hash algorithm, which is
known for its good distribution properties and speed. It processes each
character of the string to produce a consistent hash value.
Example (Hashing strings)
import { Hash } from "effect"
console.log(Hash.string("hello")) // hash of "hello"
console.log(Hash.string("world")) // hash of "world"
console.log(Hash.string("")) // hash of empty string
// Same strings produce the same hash
console.log(Hash.string("test") === Hash.string("test")) // true
string(var String: StringConstructor
;(value?: any) => string
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String(self: A & symbolself))
case "string":
return const string: (str: string) => numberComputes a hash value for a string using the djb2 algorithm.
When to use
Use when you need a string field to contribute to a custom structural hash
implementation.
Details
This function implements a variation of the djb2 hash algorithm, which is
known for its good distribution properties and speed. It processes each
character of the string to produce a consistent hash value.
Example (Hashing strings)
import { Hash } from "effect"
console.log(Hash.string("hello")) // hash of "hello"
console.log(Hash.string("world")) // hash of "world"
console.log(Hash.string("")) // hash of empty string
// Same strings produce the same hash
console.log(Hash.string("test") === Hash.string("test")) // true
string(self: A & stringself)
case "undefined":
return const string: (str: string) => numberComputes a hash value for a string using the djb2 algorithm.
When to use
Use when you need a string field to contribute to a custom structural hash
implementation.
Details
This function implements a variation of the djb2 hash algorithm, which is
known for its good distribution properties and speed. It processes each
character of the string to produce a consistent hash value.
Example (Hashing strings)
import { Hash } from "effect"
console.log(Hash.string("hello")) // hash of "hello"
console.log(Hash.string("world")) // hash of "world"
console.log(Hash.string("")) // hash of empty string
// Same strings produce the same hash
console.log(Hash.string("test") === Hash.string("test")) // true
string("undefined")
case "function":
case "object": {
if (self: | (A & Function)
| (A & object)
| (A & null)
self === null) {
return const string: (str: string) => numberComputes a hash value for a string using the djb2 algorithm.
When to use
Use when you need a string field to contribute to a custom structural hash
implementation.
Details
This function implements a variation of the djb2 hash algorithm, which is
known for its good distribution properties and speed. It processes each
character of the string to produce a consistent hash value.
Example (Hashing strings)
import { Hash } from "effect"
console.log(Hash.string("hello")) // hash of "hello"
console.log(Hash.string("world")) // hash of "world"
console.log(Hash.string("")) // hash of empty string
// Same strings produce the same hash
console.log(Hash.string("test") === Hash.string("test")) // true
string("null")
} else if (self: (A & Function) | (A & object)self instanceof var Date: DateConstructorEnables basic storage and retrieval of dates and times.
Date) {
return const string: (str: string) => numberComputes a hash value for a string using the djb2 algorithm.
When to use
Use when you need a string field to contribute to a custom structural hash
implementation.
Details
This function implements a variation of the djb2 hash algorithm, which is
known for its good distribution properties and speed. It processes each
character of the string to produce a consistent hash value.
Example (Hashing strings)
import { Hash } from "effect"
console.log(Hash.string("hello")) // hash of "hello"
console.log(Hash.string("world")) // hash of "world"
console.log(Hash.string("")) // hash of empty string
// Same strings produce the same hash
console.log(Hash.string("test") === Hash.string("test")) // true
string(self: ((A & Function) | (A & object)) & Dateself.Date.toISOString(): stringReturns a date as a string value in ISO format.
toISOString())
} else if (self: (A & Function) | (A & object)self instanceof var RegExp: RegExpConstructorRegExp) {
return const string: (str: string) => numberComputes a hash value for a string using the djb2 algorithm.
When to use
Use when you need a string field to contribute to a custom structural hash
implementation.
Details
This function implements a variation of the djb2 hash algorithm, which is
known for its good distribution properties and speed. It processes each
character of the string to produce a consistent hash value.
Example (Hashing strings)
import { Hash } from "effect"
console.log(Hash.string("hello")) // hash of "hello"
console.log(Hash.string("world")) // hash of "world"
console.log(Hash.string("")) // hash of empty string
// Same strings produce the same hash
console.log(Hash.string("test") === Hash.string("test")) // true
string(self: ((A & Function) | (A & object)) & RegExpself.function toString(): stringReturns a string representation of a function.
toString())
} else {
if (const byReferenceInstances: WeakSet<object>byReferenceInstances.WeakSet<object>.has(value: object): booleanhas(self: (A & Function) | (A & object)self)) {
return const random: <(A & Function) | (A & object)>(self: (A & Function) | (A & object)) => numberGenerates a random hash value for an object and caches it.
When to use
Use to hash an object by reference identity instead of structural content.
Details
This function creates a random hash value for objects that don't have their own
hash implementation. The hash value is cached using a WeakMap, so the same object
will always return the same hash value during its lifetime.
Example (Hashing objects by reference)
import { Hash } from "effect"
const obj1 = { a: 1 }
const obj2 = { a: 1 }
// Same object always returns the same hash
console.log(Hash.random(obj1) === Hash.random(obj1)) // true
// Different objects get different hashes
console.log(Hash.random(obj1) === Hash.random(obj2)) // false
random(self: (A & Function) | (A & object)self)
}
if (const hashCache: WeakMap<any, number>hashCache.WeakMap<any, number>.has(key: any): booleanhas(self: (A & Function) | (A & object)self)) {
return const hashCache: WeakMap<any, number>hashCache.WeakMap<any, number>.get(key: any): number | undefinedget(self: (A & Function) | (A & object)self)!
}
const const h: numberh = function withVisitedTracking<number>(
obj: object,
fn: () => number
): number
withVisitedTracking(self: (A & Function) | (A & object)self, () => {
if (const isHash: (u: unknown) => u is HashChecks whether a value implements the Hash interface.
When to use
Use to detect whether an unknown value provides a custom hash implementation.
Details
This function determines whether a given value has the Hash symbol property,
indicating that it can provide its own hash value implementation.
Example (Checking for Hash support)
import { Hash } from "effect"
class MyHashable implements Hash.Hash {
[Hash.symbol]() {
return 42
}
}
const obj = new MyHashable()
console.log(Hash.isHash(obj)) // true
console.log(Hash.isHash({})) // false
console.log(Hash.isHash("string")) // false
isHash(self: (A & Function) | (A & object)self)) {
return self: A & object & Hashself[const symbol: "~effect/interfaces/Hash"Defines the unique identifier used to identify objects that implement the Hash interface.
When to use
Use as the computed property key for the method that supplies a custom hash
value on a Hash implementor.
symbol]()
} else if (typeof self: (A & Function) | (A & object)self === "function") {
return const random: <A & Function>(self: A & Function) => numberGenerates a random hash value for an object and caches it.
When to use
Use to hash an object by reference identity instead of structural content.
Details
This function creates a random hash value for objects that don't have their own
hash implementation. The hash value is cached using a WeakMap, so the same object
will always return the same hash value during its lifetime.
Example (Hashing objects by reference)
import { Hash } from "effect"
const obj1 = { a: 1 }
const obj2 = { a: 1 }
// Same object always returns the same hash
console.log(Hash.random(obj1) === Hash.random(obj1)) // true
// Different objects get different hashes
console.log(Hash.random(obj1) === Hash.random(obj2)) // false
random(self: A & Functionself)
} else if (var Array: ArrayConstructorArray.ArrayConstructor.isArray(arg: any): arg is any[]isArray(self: A & objectself) || var ArrayBuffer: ArrayBufferConstructorRepresents a raw buffer of binary data, which is used to store data for the
different typed arrays. ArrayBuffers cannot be read from or written to directly,
but can be passed to a typed array or DataView Object to interpret the raw
buffer as needed.
ArrayBuffer.ArrayBufferConstructor.isView(arg: any): arg is ArrayBufferViewisView(self: A & objectself)) {
return const array: <unknown>(
arr: Iterable<unknown>
) => number
Computes a hash value for an iterable by hashing all of its elements.
When to use
Use to hash the values yielded by an iterable with Effect hash semantics.
Details
The implementation folds element hashes from the seed 6151 with XOR and
then optimizes the final hash.
Gotchas
A hash is not an equality proof. Because this implementation uses XOR,
reordered inputs can produce the same hash.
Example (Hashing arrays)
import { Hash } from "effect"
const arr1 = [1, 2, 3]
const arr2 = [1, 2, 3]
const arr3 = [3, 2, 1]
console.log(Hash.array(arr1)) // hash of [1, 2, 3]
console.log(Hash.array(arr2)) // same hash as arr1
console.log(Hash.array(arr3)) // may match reordered inputs
console.log(Hash.array(arr1) === Hash.array(arr2)) // true
console.log(Hash.array(arr1) === Hash.array(arr3)) // true
array(self: | (A & object & any[])
| (A &
object &
ArrayBufferView<ArrayBufferLike>)
self as any)
} else if (self: A & objectself instanceof var Map: MapConstructorMap) {
return const hashMap: <any, any>(
map: Iterable<readonly [any, any]>
) => number
hashMap(self: A & object & Map<any, any>self)
} else if (self: A & objectself instanceof var Set: SetConstructorSet) {
return const hashSet: <any>(
set: Iterable<any>
) => number
hashSet(self: A & object & Set<any>self)
}
return const structure: <A & object>(o: A & object) => numberComputes a structural hash for an object using Effect's object key collection.
When to use
Use to hash an object from all structural keys collected by Effect.
Details
The hash is based on the object's structural keys and their values, including
symbol keys and relevant prototype keys for non-plain objects.
Example (Hashing object structures)
import { Hash } from "effect"
const obj1 = { name: "John", age: 30 }
const obj2 = { name: "Jane", age: 25 }
const obj3 = { name: "John", age: 30 }
console.log(Hash.structure(obj1)) // hash of obj1
console.log(Hash.structure(obj2)) // different hash
console.log(Hash.structure(obj3)) // same as obj1
// Objects with same properties produce same hash
console.log(Hash.structure(obj1) === Hash.structure(obj3)) // true
structure(self: A & objectself)
})
const hashCache: WeakMap<any, number>hashCache.WeakMap<any, number>.set(key: any, value: number): WeakMap<any, number>Adds a new element with a specified key and value.
set(self: (A & Function) | (A & object)self, const h: numberh)
return const h: numberh
}
}
default:
throw new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error(
`BUG: unhandled typeof ${typeof self: Aself} - please report an issue at https://github.com/Effect-TS/effect/issues`
)
}
}