Skip to main content

Class: HashUtils

Defined in: utils/HashUtils.ts:2

Utility methods for non-cryptographic hashing

Constructors

Constructor

new HashUtils(): HashUtils

Returns

HashUtils

Methods

djb2()

static djb2(str, seed?): number

Defined in: utils/HashUtils.ts:11

Computes the djb2 hash of a string

Parameters

str

string

The string to hash

seed?

number = 5381

The seed to use for the hash

Returns

number

The hash, as a non-negative 32-bit integer

See

http://www.cse.yorku.ca/~oz/hash.html


djb2Pick()

static djb2Pick<T>(values, key, seed?): T

Defined in: utils/HashUtils.ts:28

Deterministically selects a value from an array based on the djb2 hash of a key string

Type Parameters

T

T

Parameters

values

T[]

The array of values to select from

key

string

The string key to hash and use for selection

seed?

number = 5381

The seed to use for the hash

Returns

T

The value corresponding to the hashed key

Throws

Error if the array is empty


lowbias32()

static lowbias32(value, seed?): number

Defined in: utils/HashUtils.ts:50

Computes the lowbias32 hash of a 32-bit integer

Unlike djb2 over the decimal string of a number, consecutive integers map to unrelated hashes, so the hash is suitable for scattering sequential IDs over a small range. Different seeds yield different hashes for the same value.

Parameters

value

number

The integer to hash; only its lower 32 bits are used

seed?

number = 0

The seed, XOR-ed into the value before hashing; only its lower 32 bits are used

Returns

number

The hash, as a non-negative 32-bit integer

See

https://github.com/skeeto/hash-prospector


lowbias32Pick()

static lowbias32Pick<T>(values, key, seed?): T

Defined in: utils/HashUtils.ts:69

Deterministically selects a value from an array based on the lowbias32 hash of an integer key

Type Parameters

T

T

Parameters

values

T[]

The array of values to select from

key

number

The integer key to hash and use for selection

seed?

number = 0

The seed (see lowbias32)

Returns

T

The value corresponding to the hashed key

Throws

Error if the array is empty