@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
39 lines (38 loc) • 1.49 kB
TypeScript
import type { Integer } from '../types.js';
/**
* Returns hashCode as hex (radix 16).
*
* All hash functions here are optimized for:
*
* 1. Performance
* 2. For non-cryptographic use (where accidental collision is not the end-of-the-world)
* 3. Compact size (32 bits max, versus 128 in md5; presented in smaller number of string json-safe characters)
*
* Basically, these functions are as simple as they can be, but still "random enough" for
* normal non-cryptographic use cases.
*
* To be run on the ClientSide, as in Node there are plenty of other hash options in `node:crypto`.
*
* Perf: it runs ~10 times faster than CryptoJS md5, and ~3 times smaller String hash size (for
* hashCode64).
*/
export declare function hashCode16(s: string): string;
/**
* Returns hashCode as "radix 36", using "Base36 alphabet".
* 36 is used, because it's the maximum "radix" that Number.toString() can take.
*
* See the hashCode16 for full description.
*/
export declare function hashCode36(s: string): string;
/**
* Returns hashCode as "radix 64", using Base64url alphabet.
* See the hashCode16 for full description.
*/
export declare function hashCode64(s: string): string;
/**
* Generates a stable integer hashCode for a given String.
* Matches Java implementation (they say), except it ensures a positive Integer
* by adding 2147483647 + 1 to the end result.
* Source: https://stackoverflow.com/a/33647870/4919972
*/
export declare function hashCode(s: string): Integer;