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.
anyDeepHasherHasher instance that hashes any value, and traverses into an object or array to hash its elements.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.
anyFlatHasherHasher instance that hashes any value, but never traverses into an object or array to hash its elements. In those cases it will use toString.anyJsonStringHasher
Returns a Hasher instance that hashes any value by hashing the string resulting from applying JSON.stringify to the value.
anyJsonStringHasherHasher instance that hashes any value by hashing the string resulting from applying JSON.stringify to the value.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.
anyShallowHasherHasher 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.anyToStringHasher
Returns a Hasher instance that hashes the string representation of any value.
anyToStringHasherHasher instance that hashes the string representation of any value.arrayHasher
Returns a Hasher that hashes arrays of elements by sampling the array and using the given itemHasher to hash the sampled elements.
arrayHasherHasher 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
| 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.
bigintHasherHasher instance that hashes bigints.booleanHasher
Returns a Hasher instance that hashes booleans.
booleanHasherHasher instance that hashes booleans.createValueOfHasher
Returns a Hasher instance that hashes the .valueOf value of the given object using the given valueHasher for instances of the given cls class.
createValueOfHasherHasher instance that hashes the .valueOf value of the given object using the given valueHasher for instances of the 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 constructor 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 Date values.
dateHasherHasher instance that hashes Date values.defaultHasher
Returns the default Hasher instance used by hashed collections.
defaultHasherHasher instance used by hashed collections.numberHasher
Returns a Hasher instance that hashes numbers, including 'special' values like NaN and infinities.
numberHasherHasher instance that hashes numbers, including 'special' values like NaN and infinities.objectDeepHasher
Returns a Hasher instance that hashes objects of key type K and value type V. If a value is an object or array, it will recursively hash its values.
objectDeepHasherHasher instance that hashes objects of key type K and value type V. If a value is 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
| 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.
objectHasherHasher 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
| 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 is an object or array, it will convert those values to a string.
objectShallowHasherHasher instance that hashes objects of key type K and value type V. If a value is an object or array, it will convert those values to a string.streamSourceHasher
Returns a Hasher instance that hashes any StreamSource limited to a certain amount of elements to prevent hanging on infinite streams.
streamSourceHasherHasher instance that hashes any StreamSource limited to a certain amount of elements to prevent hanging on infinite streams.Definition
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
Returns a Hasher instance for case-insensitive string values.
stringCaseInsensitiveHasherHasher instance for case-insensitive string values.stringHasher
Returns a Hasher instance for string values.
stringHasherHasher instance for string values.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.
tupleSymmetricHasher 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
| 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']))