effect-io-ai

Package: effect
Module: Hash

Hash.string

Computes 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

Signature

declare const string: (str: string) => number

Source

Since v2.0.0