Skip to main content

interface Hasher<UK>

Interface used to hash objects for hashed collections.

Companion namespace: Hasher

Type parameters

NameDescription
UKthe upper type limit for which the hasher is assumed to be valid
note

A hashcode is a 32-bit JS integer and therefore signed. You can obtain the 32-bit version of any JS number by applying a bitwise operator to it, e.g. if x is a number, use x | 0.

Methods

hash

Returns the 32-bit hash code for the given value.

Definition

hash(value: UK): number;

Parameters

NameTypeDescription
valueUKthe value to hash
note

it is assumed that the caller has verified that the given object is valid, either by knowing the types up front, or by using the isValid function.

example
const h = Hasher.anyHasher()
h.hash([1, 3, 2])

isValid

Returns true if this hasher can be applied to the given obj object.

Definition

isValid(obj: unknown): obj is UK;

Parameters

NameTypeDescription
objunknownthe object to check
example
const h = Hasher.numberHasher()
console.log(h.isValid(5))
// => true
console.log(h.isValid('a'))
// => false