JSPM

shorthashd

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q35272F
  • License MIT

A string hashing utility based on Daniel J. Bernstein's popular 'times 33' hash algorithm.

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (shorthashd) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    Shorthashd

    npm version

    Shorthashd is a mini library for generating short hashes from strings. It is based on Daniel J. Bernstein's popular 'times 33' hash algorithm. A fork of this shortHash function with the possibility to add base, born out of the necessity to update and make this utility zero dependency.

    Thanks to @KonkenBonken and @joakimbeng

    Installation

    You can install shorthashd via npm:

    npm install shorthashd

    Usage

    Importing the Library

    To use the library in your TypeScript or JavaScript project, you can import it as follows:

    import { shortHash } from 'shorthashd';

    Generating a Short Hash

    You can generate a short hash by calling the shortHash function with a string and an optional base (default is 16):

    const myHash = shortHash("your-string-here");
    console.log(myHash); // Outputs the hash in the specified base (default is hexadecimal)

    Example

    import { shortHash } from 'shorthashd';
    
    const myString = "Hello, World!";
    const hashBase16 = shortHash(myString); // Default base 16 (hexadecimal)
    const hashBase36 = shortHash(myString, 36); // Base 36
    
    console.log(`Base 16: ${hashBase16}`);
    console.log(`Base 36: ${hashBase36}`);

    API

    shortHash(str: string, base?: number): string

    Generates a short hash for the given string.

    • str (string): The string to hash.
    • base (number, optional): The base for the resulting hash string (must be between 2 and 36). Default is 16.

    Returns

    • (string): The resulting hash string.

    How it Works

    Shorthashd uses a hashing function based on Daniel J. Bernstein's popular 'times 33' hash algorithm. The hash is then converted to a string in the specified base.

    Hash Function

    function hash(text: string): number {
      let hash = 5381,
        index = text.length;
    
      while (index) {
        hash = (hash * 33) ^ text.charCodeAt(--index);
      }
    
      return hash >>> 0;
    }

    Short Hash Function

    export function shortHash(str: string, base: number = 16): string {
      if (base < 2 || base > 36) {
        throw new RangeError(
          "short-hash: base must be an integer between 2 and 36"
        );
      }
    
      return hash(str).toString(base);
    }

    Contributing

    Contributions are welcome! Please open an issue or submit a pull request on GitHub.

    License

    This project is licensed under the MIT License. See the LICENSE file for details.