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

identifiable

v0.15.0

Published

Generate unique ids for things and look them up on hashes

Downloads

73

Readme

If you don't need a query engine or persistence, identifiable can help you store a set of objects referenced by unique IDs:

var identifiable = require("identifiable")
var expect = require("chai").expect

var erik = {
  name: "Erik",
  likes: ["rollerblading", "cooking from scratch", "chickens"]
}

var people = {}

var get = identifiable.getFrom(people, "person")

var id = identifiable.assignId(people)
people[id] = erik
var result = get(id)

expect(result).to.equal(erik)

It does almost nothing. It generates unique IDs. It throws an error when it can't find an item with that id.

Allowing getters to return undefined

If you pass true to a getter, it will just return undefined if the record isn't found. Otherwise an error gets thrown.

var maybeErik = get(id, true)

Providing a pre-existing ID

Sometimes, if you're maybe reseeding a store of items that already have IDs assigned and being used in the wild, you want to make sure you can keep using those same ids.

This works just fine, assignId will take an id:

var id = identifiable.assignId(people, "gfa0")

This will throw an error if the ID is already in use, but otherwise it will use the provided ID. If you pass something null-like, it will assign a new, unused ID.

Giving IDs a prefix

It can be nice to be able to tell different IDs for different purposes apart at a glance. The third parameter to assignId can be a prefix:

var id = identifiable.assignId(people, null, "ppl")
// id is ppl-fjst

Why

A database is overkill if you just need a hash of items. Not everything needs a persistence AI backing it up.

This module is basically boilerplate, but it's boilerplate that you pretty much always need when you're indexing something by ID. So it's kind of not worth copy/pasting.

IDs are kind of always a security concern. In the future, perhaps this module can provide some assurances about the entropy in an ID.