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.
anyDeepHasheranyFlatHasher
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.
anyFlatHasheranyJsonStringHasher
Returns a Hasher instance that hashes any value by hashing the string resulting from applying JSON.stringify to the value.
anyJsonStringHasheranyShallowHasher
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.
anyShallowHasheranyToStringHasher
Returns a Hasher instance that hashes the string representation of any value
anyToStringHasherarrayHasher
Returns a Hasher that hashes arrays of elements by sampling the array and using the given itemHasher to hash the sampled elements.
arrayHasheritemHasher to hash the sampled elements.Definition
function arrayHasher<T = any>(options?: {
itemHasher?: Hasher<T>;
maxStepBits?: number;
}): Hasher<readonly T[]>;
Type parameters
| Name | Description |
|---|---|
| T | the array element type |
Parameters
| Name | Type | Description |
|---|---|---|
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 |
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.
bigintHasherbooleanHasher
Returns a Hasher instance that hashes booleans.
booleanHashercreateValueOfHasher
Returns a Hasher instance that hashes the .valueOf value of the given object using the given valueHasher for instances of given cls class.
createValueOfHasher.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
| Name | Description |
|---|---|
| T | the input object type |
| V | the .valueOf property type |
Parameters
| Name | Type | Description |
|---|---|---|
cls | {new (): T;} | the class containing the contructur to check for validity of a given object |
valueHasher | Hasher<V> | the Hasher instance to use for the .valueOf values |
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.
dateHashernumberHasher
Returns a Hasher instance that hashes numbers, including 'special' values like NaN and infinities.
numberHasherobjectDeepHasher
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.
objectDeepHasherDefinition
function objectDeepHasher<K extends string |number|symbol, V = any>():Hasher<Record<K, V>>;
Type parameters
| Name | Description |
|---|---|
| K | the key type |
| V | the value type |
be careful with circular structures, they can cause an infinite loop
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.
objectHasherDefinition
function objectHasher<K extends string |number| symbol, V = any>(options?: {
keyHasher: Hasher<K>;
valueHasher: Hasher<V>;
}): Hasher<Record<K, V>>;
Type parameters
| Name | Description |
|---|---|
| K | the key type |
| V | the value type |
Parameters
| Name | Type | Description |
|---|---|---|
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 |
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.
objectShallowHasherstreamSourceHasher
Returns a Hasher instance that hashes any StreamSource limited to a certain amount of elements to prevent haning on infinite streams.
streamSourceHasherDefinition
function streamSourceHasher<T = any>(options?: {
itemHasher?: Hasher<T>;
maxStepBits?: number;
}): Hasher<StreamSource<T>>;
Type parameters
| Name | Description |
|---|---|
| T | the StreamSource element type |
Parameters
| Name | Type | Description |
|---|---|---|
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 |
const h = Hasher.streamSourceHasher()
h.hash(Stream.random())
// infinite stream but will not hang due to the max step limit
stringCaseInsensitiveHasher
undocumented
stringCaseInsensitiveHasherstringHasher
Returns a Hasher instance for string values.
stringHashertupleSymmetric
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.
tupleSymmetrichasher function to hash the tuple elements.Definition
function tupleSymmetric<T>(hasher?: Hasher<T>): Hasher<readonly [T, T]>;
Type parameters
| Name | Description |
|---|---|
| T |
Parameters
| Name | Type | Description |
|---|---|---|
hasher | Hasher<T> | the Hasher instance to use for tuple elements |
const h = Hasher.tupleSymmetric()
console.log(h.hash(['abc', 'def']) === h.hash(['def', 'abc']))