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

nanohash

v0.0.2

Published

Generate 64bits-based numeric ids readable as short strings too!

Downloads

97

Readme

nanohash

A tiny unique string ID generator including a matching 64-bit numeric value

Documentation

Check the doc: NanoHash

Why?

I created this lib to generate short codes youtube-alike (ex: PkZNo7MFNFg). Except that in my case I made it possible from 1 to 9 characters maximum. The size of max 9 characters being due to the 64-bit numeric limitation of Fauna Document Id which is a Long number (A 64-bit signed decimal integer number.).

You can create a document with a given ID and reverse it back and forth from an ID to a short-code.

const { nanohash } = require('nanohash')

// The default alphabet used to generate short-codes is a-zA-Z0-0
// If you wanna pick wisely the size of your codes regarding to the alphabet used I strongly recommand you to go and check https://zelark.github.io/nano-id-cc/.
// Also even if you were to create and already existing code you could simply create another one as a fallback (or already provide a list of several codes to pick from).

const nhash = nanohash({ size: 6 })

const id = nhash.generate()
// output: 1342859071901

const shortCode = nhash.dehash(id)
// output: ysX7j1

const backToId = nhash.hash(shortCode)
// output: 1342859071901
// same as id

It's then easy to create documents using the generated ids (knowing they have already corresponding reversible short-codes).

const fauna = require('faunadb')
const q = fauna.query
const client = new fauna.Client({ secret: 'your-secret' })

// create a user document
client.query(q.Create(q.Ref(q.Collection('users'), nhash.generate()), { data: { name: 'Joe' } }))

// create a note document
client.query(q.Create(q.Ref(q.Collection('notes'), nhash.generate()), { data: { owner: q.Ref(q.Collection('users'), '1342859071901') } }))

//...

// in your page url you can include the converted code
// for your user ID
const userCode = nhash.dehash('1342859071901')
// output: ysX7j1

// and your note ID
const noteCode = nhash.dehash('1304807441806')
// output: uM7Ii6

/*
  We could easily imagine then to have your routes in your front set as follows:
  myapp.com/users/<userCode>/notes/<noteCode> 
  ex: myapp.com/users/ysX7j1/notes/uM7Ii6 
*/

You can also play with bulk generation to create several documents or do retries if a creation fails for an existing ID.

const codes = nhash.bulk()
/* output: [
  '1342859071901',
  '1304807441806',
  '1160506510704',
  '1295220296107',
  '1312555266027',
  '1344940551137',
  '1090639062514',
  '1435957560823',
  '1135840001452',
  '1180647530040'
]
*/

Dehash an ID straight in a Fauna User-Defined Function

You can scaffold the creation of the functions required to dehash an ID wherever you want in your Fauna FQL queries by doing as follow.

const fauna = require('faunadb')
const q = fauna.query
const client = new fauna.Client({ secret: 'your-secret' })

const NanoHash = require('nanohash')

client.query(NanoHash.fauna.scaffold(q))
// Check fauna.scaffold() function to learn more about what's being created.

You can then use Call("nanohash.dehash",["1342859071901"]) to retrieve ysX7j1.

This is a draft.

I've been wanting to use a short string as unique ID for Fauna but couldn't because of the numeric only restriction. I'm not sure when (or if) it's gonne change on Fauna's end and I find it silly to have a generated ID by Fauna AND and another unique code in addition.

At least this lib will fill this gap for my use cases and maybe yours.