Skip to main content

namespace Hasher

Interface used to hash objects for hashed collections.

Companion interface: Hasher<UK>

Functions

anyDeepHasher

Returns a Hasher instance that hashes any value, and traverses into an object or array to hash its elements.

Definition

function anyDeepHasher(): Hasher<any>;

example
const h = Hasher.anyDeepHasher()
console.log(h.hash({ a: 1, b: 2 }) === h.hash({ b: 2, a: 1 }))
// => true
console.log(h.hash([{ a: 1, b: 2 }]) === h.hash([{ b: 2, a: 1 }]))
// => true

anyFlatHasher

Returns a Hasher instance that hashes any value, but never traverses into an object or array to hash its elements. In those cases it will use toString.

Definition

function anyFlatHasher(): Hasher<any>;

example
const h = Hasher.anyFlatHasher()
console.log(h.hash({ a: 1, b: 2 }) === h.hash({ b: 2, a: 1 }))
// => false

anyJsonStringHasher

Returns a Hasher instance that hashes any value by hashing the string resulting from applying JSON.stringify to the value.

Definition

function anyJsonStringHasher(): Hasher<any>;

example
const h = Hasher.anyJsonStringHasher()
console.log(h.hash({ a: 1, b: 2 }) === h.hash({ b: 2, a: 1 }))
// => false

anyShallowHasher

Returns a Hasher instance that hashes any value, but only traverses into an object or array to hash its elements one level deep. After one level, it will use toString.

Definition

function anyShallowHasher(): Hasher<any>;

example
const h = Hasher.anyShallowHasher()
console.log(h.hash({ a: 1, b: 2 }) === h.hash({ b: 2, a: 1 }))
// => true
console.log(h.hash([{ a: 1, b: 2 }]) === h.hash([{ b: 2, a: 1 }]))
// => false

anyToStringHasher

Returns a Hasher instance that hashes the string representation of any value

Definition

function anyToStringHasher(maxStepBits?: number): Hasher<any>;

Parameters

NameTypeDescription
maxStepBitsnumberthe maximum amount of samples to take from the string
example
const h = Hasher.anyToStringHasher()
h.hash([1, 3, 'a'])

arrayHasher

Returns a Hasher that hashes arrays of elements by sampling the array and using the given itemHasher to hash the sampled elements.

Definition

function arrayHasher<T = any>(options?: {
    itemHasher?: Hasher<T>;
    maxStepBits?: number;
  }): Hasher<readonly T[]>;

Type parameters
NameDescription
Tthe array element type

Parameters

NameTypeDescription
options{
    itemHasher?: Hasher<T>;
    maxStepBits?: number;
  }
(optional) an object containing the following items:
- itemHasher: (optional) a Hasher instance used to hash elements in the array
- maxStepBits: (optional) the amount of bits to determine the maximum amount of array elements to process
example
const h = Hasher.arrayHasher()
console.log(h.hash([1, 2, 3] === h.hash([1, 3, 2])))
// => false

bigintHasher

Returns a Hasher instance that hashes bigints.

Definition

function bigintHasher(): Hasher<bigint>;

example
const h = Hasher.bigintHasher()
console.log(h.hash(BigInt(5)) === h.hash(BigInt(10)))
// => false

booleanHasher

Returns a Hasher instance that hashes booleans.

Definition

function booleanHasher(): Hasher<boolean>;

example
const h = Hasher.booleanHasher()
console.log(h.hash(true) === h.hash(false))
// => false

createValueOfHasher

Returns a Hasher instance that hashes the .valueOf value of the given object using the given valueHasher for instances of given cls class.

Definition

function createValueOfHasher<T extends {
    valueOf(): V;
  }, V>(cls: {
    new (): T;
  }, valueHasher?: Hasher<V>): Hasher<T>;

Type parameters
NameDescription
Tthe input object type
Vthe .valueOf property type

Parameters

NameTypeDescription
cls{
    new (): T;
  }
the class containing the contructur to check for validity of a given object
valueHasherHasher<V>the Hasher instance to use for the .valueOf values
example
const h = Hasher.createValueOfHasher(Date)
console.log(h.isValid(new Boolean(true)))
// => false
const d1 = new Date()
const d2 = new Date(d1)
console.log(h.hash(d1) === h.hash(d2))
// => true

dateHasher

Returns a Hasher instance that hashes Dates.

Definition

function dateHasher(): Hasher<Date>;

example
const h = Hasher.dateHasher()
const d1 = new Date()
const d2 = new Date(d1)
console.log(h.hash(d1) === h.hash(d2))
// => true

defaultHasher

undocumented

Definition

function defaultHasher(): Hasher<any>;

numberHasher

Returns a Hasher instance that hashes numbers, including 'special' values like NaN and infinities.

Definition

function numberHasher(): Hasher<number>;

example
const h = Hasher.numberHasher()
console.log(h.hash(Number.POSITIVE_INFINITY) === h.hash(Number.NEGATIVE_INFINITY))
// => false
console.log(h.hash(Number.NaN) === h.hash(Number.NaN))
// => true

objectDeepHasher

Returns a Hasher instance that hashes objects of key type K and value type V. If a value if an object or array, it will recursively hash its values.

Definition

function objectDeepHasher<K extends string |number|symbol, V = any>():Hasher<Record<K, V>>;

Type parameters
NameDescription
Kthe key type
Vthe value type
note

be careful with circular structures, they can cause an infinite loop

example
const h = Hasher.objectDeepHasher()
console.log(h.hash({ a: 1, b: 2 }) === h.hash({ b: 2, a: 1 }))
// => true

objectHasher

Returns a Hasher instance that hashes objects of key type K and value type V.

Definition

function objectHasher<K extends string |number| symbol, V = any>(options?: {
    keyHasher: Hasher<K>;
    valueHasher: Hasher<V>;
  }): Hasher<Record<K, V>>;

Type parameters
NameDescription
Kthe key type
Vthe value type

Parameters

NameTypeDescription
options{
    keyHasher: Hasher<K>;
    valueHasher: Hasher<V>;
  }
(optional) an object containing:
- keyHasher: (optional) a Hasher instance that is used to hash object keys
- valueHasher: (optional) a Hasher instance that is used to hash object values
example
const h = Hasher.objectHasher()
console.log(h.hash({ a: 1, b: 2 }) === h.hash({ b: 2, a: 1 }))
// => true

objectShallowHasher

Returns a Hasher instance that hashes objects of key type K and value type V. If a value if an object or array, it will convert those values to a string.

Definition

function objectShallowHasher<K extends string |number|symbol, V = any>():Hasher<Record<K, V>>;

Type parameters
NameDescription
Kthe key type
Vthe value type
example
const h = Hasher.objectShallowHasher()
console.log(h.hash({ a: 1, b: 2 }) === h.hash({ b: 2, a: 1 }))
// => true

streamSourceHasher

Returns a Hasher instance that hashes any StreamSource limited to a certain amount of elements to prevent haning on infinite streams.

Definition

function streamSourceHasher<T = any>(options?: {
    itemHasher?: Hasher<T>;
    maxStepBits?: number;
  }): Hasher<StreamSource<T>>;

Type parameters
NameDescription
Tthe StreamSource element type

Parameters

NameTypeDescription
options{
    itemHasher?: Hasher<T>;
    maxStepBits?: number;
  }
(optional) an object containing the following items:
- itemHasher: (optional) a Hasher instance used to hash elements in the array
- maxStepBits: (optional) the amount of bits to determine the maximum amount of array elements to process
example
const h = Hasher.streamSourceHasher()
h.hash(Stream.random())
// infinite stream but will not hang due to the max step limit

stringCaseInsensitiveHasher

undocumented

Definition

function stringCaseInsensitiveHasher(): Hasher<string>;

stringHasher

Returns a Hasher instance for string values.

Definition

function stringHasher(): Hasher<string>;

example
const h = Hasher.stringHasher()
h.hash('abc')

tupleSymmetric

Returns a Hasher that will return equal hash values for values in a tuple regardless of their order, and uses the given hasher function to hash the tuple elements.

Definition

function tupleSymmetric<T>(hasher?: Hasher<T>): Hasher<readonly [T, T]>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
hasherHasher<T>the Hasher instance to use for tuple elements
example
const h = Hasher.tupleSymmetric()
console.log(h.hash(['abc', 'def']) === h.hash(['def', 'abc']))