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

@sullux/fp-light-hash

v0.0.2

Published

A lightweight functional library for functional composition and object composition.

Downloads

7

Readme

home

fp-light-hash

npm i @sullux/fp-light-hash source test

This is a quick hashing algorithm for use in hash maps and other implementations that demand deterministic but well-distributed values based on not-well-distributed input values.

WARNING: THIS ALGORITHM IS NOT SUITABLE FOR CRYPTOGRAPHIC OPERATIONS. A password hashed with this algorithm could be easily reverse-engineered. This algorithm is for distribution, not obfuscation. The benefit of this algorithm for distribution scenarios is that it has far better performance than cryptographic hashing.

hash

hash(outputSize: Number, input: Buffer): Buffer

This is the core implementation of hash. Given an output size (desired length of the output buffer) and an input buffer (data to be hashed), returns the hashed value as a buffer. All of the other functions are convenience functions for this core hash implementation.

hashFrom

hashFrom(seed: Buffer, input: Buffer): Buffer

WARNING: this function mutates the seed argument. This is the underlying unit of work for the core hash function which mutates as a performance optimization. It is surfaced to enable progresssive hashing use cases such as reducers or streaming. The return value is a reference to the seed buffer, which is mutated during hashing. A hash reducer might look like the following example:

const hashBigArray = (outputSize, bigArray) =>
  bigArray.reduce((seed, value) =>
    seed
      ? hashFrom(seed, JSON.stringify(value))
      : hashAny(outputSize, value))

hashAny

hashAny(outputSize: Number, input: any): Buffer

Like hash, but will internally create an input buffer from the supplied input value.

hashToString

hashToString(outputSize: Number, input: any): String

Like hashAny, but outputs a hex-encoded string instead of a buffer. Because hex encoding is twice as long as the number of bytes in the output buffer, the actual output buffer is half the given outputSize. Odd numbered output sizes are handled gracefully.

hashToDouble

hashToDouble(outputSize: Number, input: any): Number

Like hashAny, but outputs a floating point number instead of a buffer.

hashToInt

hashToInt(outputSize: Number, input: any): Number

Like hashAny, but outputs a 32-bit integer instead of a buffer. This would be useful in creating a custom hashmap implementation as in the following (incomplete) example:

const { hashToInt } = require('@sullux/fp-light-hash')

const hashMap = (from = [], capacity = 128) => {
  const indexOf = key => hashToInt(key) % capacity
  const values = Array(capacity)

  from.map(indexValues =>
    indexValues.map(([key, value]) =>
      values[indexOf(key)] = value)

  return {
    get: (key) => {
      const index = indexOf(key)
      const indexValues = values[index]
      return indexValues && indexValues.reduce((result, [entryKey, value]) =>
        entryKey === key
          ? value
          : result)
    },
    set: (key, value) => {
      const index = indexOf(key)
      const indexValues = values[index]
      return (indexValues
        ? indexValues.push([key, value])
        : values[index] = [[key, value]]) && value
    },
  }
}