Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

stable-hash

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stable-hash - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

dist/index.d.ts

2

dist/index.js

@@ -1,1 +0,1 @@

var y=Object.defineProperty;var u=t=>y(t,"__esModule",{value:!0});var d=typeof require!="undefined"?require:t=>{throw new Error('Dynamic require of "'+t+'" is not supported')};var p=(t,o)=>{u(t);for(var s in o)y(t,s,{get:o[s],enumerable:!0})};p(exports,{default:()=>c});const i=new WeakMap;let b=0;function c(t){const o=typeof t,s=t&&t.constructor,f=s==Date;let e,n;if(Object(t)===t&&!f&&s!=RegExp){if(e=i.get(t),e)return e;if(e=++b+"~",i.set(t,e),s==Array){for(e="@",n=0;n<t.length;n++)e+=c(t[n])+",";i.set(t,e)}if(s==Object){e="#";const l=Object.keys(t).sort();for(;typeof(n=l.pop())!="undefined";)typeof t[n]!="undefined"&&(e+=n+":"+c(t[n])+",");i.set(t,e)}}else e=f?t.toJSON():o=="symbol"?t.toString():o=="string"?JSON.stringify(t):""+t;return e}
var u=Object.defineProperty;var r=t=>u(t,"__esModule",{value:!0});var p=typeof require!="undefined"?require:t=>{throw new Error('Dynamic require of "'+t+'" is not supported')};var y=(t,i)=>{r(t);for(var s in i)u(t,s,{get:i[s],enumerable:!0})};y(exports,{default:()=>c});const o=new WeakMap;let b=0;function c(t){const i=typeof t,s=t&&t.constructor,f=s==Date;if(Object(t)===t&&!f&&s!=RegExp){let e=o.get(t);if(e)return e;e=++b+"~",o.set(t,e);let n;if(s==Array){for(e="@",n=0;n<t.length;n++)e+=c(t[n])+",";o.set(t,e)}else if(s==Object){e="#";const l=Object.keys(t).sort();for(;(n=l.pop())!==void 0;)t[n]!==void 0&&(e+=n+":"+c(t[n])+",");o.set(t,e)}return e}return f?t.toJSON():i=="symbol"?t.toString():i=="string"?JSON.stringify(t):""+t}
{
"name": "stable-hash",
"version": "0.0.2",
"version": "0.0.3",
"description": "Stable JS value hash.",

@@ -10,2 +10,7 @@ "repository": "https://github.com/shuding/stable-hash",

"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"files": [

@@ -17,6 +22,18 @@ "dist/**"

"build:cjs": "esbuild src/index.ts --minify --target=es6 --outdir=dist --format=cjs",
"build": "yarn build:mjs && yarn build:cjs"
"build:types": "tsc --emitDeclarationOnly --declaration -p tsconfig.build.json",
"build": "yarn build:mjs && yarn build:cjs && yarn build:types",
"test": "jest"
},
"devDependencies": {
"esbuild": "^0.12.28"
"@types/jest": "^28.1.3",
"base64-url": "^2.3.3",
"esbuild": "^0.12.28",
"flattie": "^1.1.0",
"hash-obj": "^4.0.0",
"jest": "^28.1.1",
"json-stringify-deterministic": "^1.0.7",
"nanobench": "^2.1.1",
"prettier": "^2.7.1",
"ts-jest": "^28.0.5",
"typescript": "^4.7.4"
},

@@ -23,0 +40,0 @@ "prettier": {

# stable-hash
A small (496b) lib for stable hashing a JavaScript value. Originally created for [SWR](https://github.com/vercel/swr).
A tiny and fast (481b <sup>[unpkg](https://unpkg.com/stable-hash@0.0.3/dist/index.mjs)</sup>) lib for "stably hashing" a JavaScript value. Originally created for [SWR](https://github.com/vercel/swr).
It's similar to `JSON.stringify(value)`, but:
1. `value` can be any JavaScript value
2. It sorts object keys
1. Supports any JavaScript value (BigInt, NaN, Symbol, function, class, ...)
2. Sorts object keys (stable)
3. Supports circular objects

@@ -16,5 +17,5 @@ ## Use

```js
import stableHash from 'stable-hash'
import hash from 'stable-hash'
stableHash(anyJavaScriptValueHere) // returns a string
hash(anyJavaScriptValueHere) // returns a string
```

@@ -27,8 +28,8 @@

```js
stableHash(1)
stableHash('foo')
stableHash(true)
stableHash(undefined)
stableHash(null)
stableHash(NaN)
hash(1)
hash('foo')
hash(true)
hash(undefined)
hash(null)
hash(NaN)
```

@@ -39,4 +40,4 @@

```js
stableHash(1) === stableHash(1n)
stableHash(1) !== stableHash(2n)
hash(1) === hash(1n)
hash(1) !== hash(2n)
```

@@ -47,6 +48,6 @@

```js
stableHash(Symbol.for('foo')) === stableHash(Symbol.for('foo'))
stableHash(Symbol.for('foo')) === stableHash(Symbol('foo'))
stableHash(Symbol('foo')) === stableHash(Symbol('foo'))
stableHash(Symbol('foo')) !== stableHash(Symbol('bar'))
hash(Symbol.for('foo')) === hash(Symbol.for('foo'))
hash(Symbol.for('foo')) === hash(Symbol('foo'))
hash(Symbol('foo')) === hash(Symbol('foo'))
hash(Symbol('foo')) !== hash(Symbol('bar'))
```

@@ -59,4 +60,4 @@

```js
stableHash(/foo/) === stableHash(/foo/)
stableHash(/foo/) !== stableHash(/bar/)
hash(/foo/) === hash(/foo/)
hash(/foo/) !== hash(/bar/)
```

@@ -67,3 +68,3 @@

```js
stableHash(new Date(1)) === stableHash(new Date(1))
hash(new Date(1)) === hash(new Date(1))
```

@@ -74,4 +75,4 @@

```js
stableHash([1, '2', [new Date(3)]]) === stableHash([1, '2', [new Date(3)]])
stableHash([1, 2]) !== stableHash([2, 1])
hash([1, '2', [new Date(3)]]) === hash([1, '2', [new Date(3)]])
hash([1, 2]) !== hash([2, 1])
```

@@ -84,3 +85,3 @@

foo.push(foo)
stableHash(foo) === stableHash(foo)
hash(foo) === hash(foo)
```

@@ -91,4 +92,4 @@

```js
stableHash({ foo: 'bar' }) === stableHash({ foo: 'bar' })
stableHash({ foo: { bar: 1 } }) === stableHash({ foo: { bar: 1 } })
hash({ foo: 'bar' }) === hash({ foo: 'bar' })
hash({ foo: { bar: 1 } }) === hash({ foo: { bar: 1 } })
```

@@ -99,3 +100,3 @@

```js
stableHash({ a: 1, b: 2, c: 3 }) === stableHash({ c: 3, b: 2, a: 1 })
hash({ a: 1, b: 2, c: 3 }) === hash({ c: 3, b: 2, a: 1 })
```

@@ -108,3 +109,3 @@

foo.foo = foo
stableHash(foo) === stableHash(foo)
hash(foo) === hash(foo)
```

@@ -118,4 +119,4 @@

const foo = () => {}
stableHash(foo) === stableHash(foo)
stableHash(foo) !== stableHash(() => {})
hash(foo) === hash(foo)
hash(foo) !== hash(() => {})
```

@@ -125,4 +126,4 @@

class Foo {}
stableHash(Foo) === stableHash(Foo)
stableHash(Foo) !== stableHash(class {})
hash(Foo) === hash(Foo)
hash(Foo) !== hash(class {})
```

@@ -132,9 +133,9 @@

const foo = new Set([1])
stableHash(foo) === stableHash(foo)
stableHash(foo) !== stableHash(new Set([1]))
hash(foo) === hash(foo)
hash(foo) !== hash(new Set([1]))
```
## Notice
## Notes
This function does something similar to serialization. It doesn't generate a secure checksum or digest, which usually has a fixed length and is hard to be reversed. With `stable-hash` it's likely possible to get the original data. Also, the output might include any charaters, not just alphabets and numbers like other hash algorithm. So:
This function does something similar to `JSON.stringify`, but more than it. It doesn't generate a secure checksum, which usually has a fixed length and is hard to be reversed. With `stable-hash` it's still possible to get the original data. Also, the output might include any charaters, not just alphabets and numbers like other hash algorithms. So:

@@ -146,11 +147,13 @@ - Use another encoding layer on top of it if you want to display the output.

import crypto from 'crypto'
import stableHash from 'stable-hash'
import hash from 'stable-hash'
const hash = stableHash(anyJavaScriptValueHere)
const encodedHash = Buffer.from(hash).toString('base64')
const safeHash = crypto.createHash('MD5').update(hash).digest('hex')
const weakHash = hash(anyJavaScriptValueHere)
const encodedHash = Buffer.from(weakHash).toString('base64')
const safeHash = crypto.createHash('MD5').update(weakHash).digest('hex')
```
Also, the consistency of this lib is sometimes guaranteed by the singularity of the WeakMap instance. So it might not generate the consistent results when running in different runtimes, e.g. server/client or parent/worker scenarios.
## License
Created by Shu Ding. Released under the MIT License.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc