npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hash-code-string

v1.0.0

Published

Convert long strings into short, url-safe, hash-like strings. Limited to 32 bits of uniqueness.

Downloads

9

Readme

hash-code-string

Overview

hash-code-string is a small utility package to convert long strings into shorter, hash-like strings. It provides functions to create hashes, encode them in a URL-safe base64 format, and decode them back into bytes, or a 32 bit number if desired. Limited to 32 bits of uniqueness. No dependencies, the hashing code is 5 lines. Assumes environment has Uint8Array, Math.imul, and btoa.

Installation

npm install hash-code-string
pnpm install hash-code-string

Usage

import { hashString, hashToNum32 } from 'hash-code-string';

// Example usage
const longString = "This is a long string that needs to be hashed";
const hash = hashString(longString);
console.log(`Hash: ${hash}`); // Hash: g3g2qw

const decodedHash = hashToNum32(hash);
console.log(`Decoded Hash: ${decodedHash}`); // Decoded Hash: -1422493565

API

hashCode(str: string): number

Creates a hash code number from a string similar to Java's String.hashCode method.

Parameters:

  • str: The input string to hash.

Returns:

A 32-bit integer hash code.

num32toBytes(num: number): Uint8Array

Splits a 32-bit number into an array of 4 bytes.

Parameters:

  • num: The 32-bit integer to split.

Returns:

A Uint8Array containing 4 bytes.

codesToString(bytes: Uint8Array): string

Converts an array of byte values to a string.

Parameters:

  • bytes: An array of bytes.

Returns:

A string representing the byte values.

encode64(str: string): string

Encodes a string into a URL-safe base64 format.

Parameters:

  • str: The input string to encode.

Returns:

A URL-safe base64 encoded string.

hashString(str: string): string

Creates a hash of the input string and encodes it in URL-safe base64 format.

Parameters:

  • str: The input string to hash and encode.

Returns:

A URL-safe base64 encoded hash string.

decode64(str: string): string

Decodes a URL-safe base64 string.

Parameters:

  • str: The URL-safe base64 string to decode.

Returns:

The decoded string.

stringToCodes(str: string): number[]

Converts a string to an array of character codes.

Parameters:

  • str: The input string.

Returns:

An array of character codes.

bytesToNum32(bytes: Uint8Array): number

Converts the first 4 elements of a byte array back to a 32-bit number.

Parameters:

  • bytes: An array of bytes.

Returns:

A 32-bit integer.

Example

const str = "example";
const hash = hashString(str);
console.log(hash); // Output: URL-safe base64 encoded hash

const decoded = decode64(hash);
console.log(decoded); // Output: decoded hash string

const charCodes = stringToCodes(decoded);
console.log(charCodes); // Output: Array of character codes

const num = bytesToNum32(charCodes);
console.log(num); // Output: 32-bit number

Contributing

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

License

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