(o: object, keys: Iterable<PropertyKey>): numberComputes a hash value for an object using only the specified keys.
When to use
Use to hash an object by a selected set of property keys.
Details
This function allows you to hash an object by considering only specific keys, which is useful when you want to create a hash based on a subset of an object's properties.
Example (Hashing selected object keys)
import { Hash } from "effect"
const person = { name: "John", age: 30, city: "New York" }
// Hash only specific keys
const hash1 = Hash.structureKeys(person, ["name", "age"])
const hash2 = Hash.structureKeys(person, ["name", "city"])
console.log(hash1) // hash based on name and age
console.log(hash2) // hash based on name and city
// Same keys produce the same hash
const person2 = { name: "John", age: 30, city: "Boston" }
const hash3 = Hash.structureKeys(person2, ["name", "age"])
console.log(hash1 === hash3) // trueexport const const structureKeys: (
o: object,
keys: Iterable<PropertyKey>
) => number
Computes a hash value for an object using only the specified keys.
When to use
Use to hash an object by a selected set of property keys.
Details
This function allows you to hash an object by considering only specific keys,
which is useful when you want to create a hash based on a subset of an object's
properties.
Example (Hashing selected object keys)
import { Hash } from "effect"
const person = { name: "John", age: 30, city: "New York" }
// Hash only specific keys
const hash1 = Hash.structureKeys(person, ["name", "age"])
const hash2 = Hash.structureKeys(person, ["name", "city"])
console.log(hash1) // hash based on name and age
console.log(hash2) // hash based on name and city
// Same keys produce the same hash
const person2 = { name: "John", age: 30, city: "Boston" }
const hash3 = Hash.structureKeys(person2, ["name", "age"])
console.log(hash1 === hash3) // true
structureKeys = (o: objecto: object, keys: Iterable<PropertyKey>keys: interface Iterable<T, TReturn = any, TNext = any>Iterable<type PropertyKey =
| string
| number
| symbol
PropertyKey>) => {
let let h: numberh = 12289
for (const const key: PropertyKeykey of keys: Iterable<PropertyKey>keys) {
let h: numberh ^= const combine: (self: number, b: number) => number (+1 overload)combine(const hash: <PropertyKey>(
self: PropertyKey
) => number
Computes 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(const key: PropertyKeykey), const hash: <any>(self: any) => 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((o: objecto as any)[const key: PropertyKeykey]))
}
return const optimize: (n: number) => numberApplies bit manipulation techniques to optimize a hash value.
When to use
Use to improve the bit distribution of a raw numeric hash value.
Details
This function takes a hash value and applies bitwise operations to improve
the distribution of hash values, reducing the likelihood of collisions.
Example (Optimizing a hash value)
import { Hash } from "effect"
const rawHash = 1234567890
const optimizedHash = Hash.optimize(rawHash)
console.log(optimizedHash) // optimized hash value
// Often used internally by other hash functions
const stringHash = Hash.optimize(Hash.string("hello"))
optimize(let h: numberh)
}