Socket
Socket
Sign inDemoInstall

stable-id

Package Overview
Dependencies
2
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    stable-id

Generate 128bit hex id for stable JSON strigified object or string


Version published
Weekly downloads
28
increased by460%
Maintainers
1
Install size
38.4 kB
Created
Weekly downloads
 

Readme

Source

stable-id

Stable ID generates a 128-bit hex string id that is the first 128 bits of an SHA-256 hash of the input data.

Objects are serialized using json-stable-stringify.

Errors will be thrown on objects that include circular references.

As of version 1.0.0 this string is lower case where previously it was upper case.

Stable id usage

const stableId = require('stable-id')

stableId(foo)

Object stable id

// 7a38bf81f383f69433ad6e900d35b3e2
stableId({foo: 'bar'})

String stable id

// 2c26b46b68ffc68ff99b453c1d304134
stableId('foo')

Number stable id

// 5feceb66ffc86f38d952786c6d696c79
stableId(0)

// 5feceb66ffc86f38d952786c6d696c79
stableId('0')

Numbers are converted to strings so the stable id for a number will be the same as the stable id of a string representation of the same number.

NaN and Infinity are not supported and return undefined.

Boolean stable id

// b5bea41b6c623f7c09f1bf24dcae58eb
stableId(true)

// b5bea41b6c623f7c09f1bf24dcae58eb
stableId('true')

Booleans are converted to strings so the string true and the boolean true will yield the same id.

Infinity stable id

// 78d9ce976067aaa5aa9024c17a726c9b
stableId(Infinity)

// 78d9ce976067aaa5aa9024c17a726c9b
stableId('∞')

The number Infinity is converted to the U+221E unicode infinity character.

Undefined id

// undefined
stableId(undefined)

// undefined
stableId(null)

// undefined
stableId(NaN)

undefined, null, and NaN all return an undefined id.

Stable id with data

var id = stableId({foo: 'bar'}, true)

id.id   // 7a38bf81f383f69433ad6e900d35b3e2
id.data // {"foo":"bar"}

When called with the second argument === true an object will be returned with an id property which is the hex id and a data property which is the exact string that was used to generate that id.

When storing data this method should be used to insure that the data stored exactly matches the data that was used to create the id.

FAQs

Last updated on 18 Nov 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc