Hyperlinkv0.8.0-beta.28

Hash

Hash.structureKeysconsteffect/Hash.ts:430
(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
hashing
Source effect/Hash.ts:4307 lines
export const structureKeys = (o: object, keys: Iterable<PropertyKey>) => {
  let h = 12289
  for (const key of keys) {
    h ^= combine(hash(key), hash((o as any)[key]))
  }
  return optimize(h)
}
Referenced by 1 symbols