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

skeleton-key

v3.0.0

Published

Cache key generator

Downloads

1,229

Readme

Skeleton Key

A simple object hash generator

Build Status Coverage Status

This module will allow you to generate object hashes from a collection of values. The values can be inserted in any order and as long as they are equal a matching cache key can be generated.

A common use case is cache key generation, where user input may dictate the conditions for the cached data. For example, a HTTP request to:

/users?filter[name]=sally&filter[occupation]=engineer

will yeild the same result as

/users?filter[occupation]=engineer&filter[name]=sally

but simply caching the request URI would result in two separate entries.

Usage

const SkeletonKey = require('skeleton-key');

route.use((req, res, next) => {
  // Generate a cache key composed of the path and the filter.
  const key = new SkeletonKey();
  key.setNotch('path', req.path);
  key.setNotch('filter', req.query.filter);
  req.cacheKey = key.cut();
  next();
});

Now variance in the order of the filter query won't affect the cache key.

You can also sort an array of values on add if the values are more important than the order to ensure consistency between keys, e.g.

key.setNotch('fields', ['name', 'occupation', 'birthday'], true);

would generate the same notch as

key.setNotch('fields', ['occupation', 'name', 'birthday'], true);

API

  • constructor(algo = 'sha1')
    • algo – the hashing algorithm to use when creating the key, defaults to sha1. Possible options: sha1, MD5.
  • resetKey() - Removes all notches from the key.
  • setNotch(id, notch[, sort, [comparator]]) - Adds a notch to the key or replaces an existing notch.
    • id - The notch ID to set or replace.
    • notch - The notch to add to the key.
    • sort - A flag on whether or not the notch value should be sorted before saving it to the key. This can be used to guarantee consistency between keys when the order is not important but the values are.
    • comparator - An optional comparison function to use when sorting arrays.
  • getNotch - Gets the value of a given notch in the key.
    • id - The notch ID to get.
  • removeNotch - Removes a notch from the key.
    • id - The notch ID to remove from the key.
  • cut - Cuts the key, generating a MD5 hash of the map.