{"name":"stable-hash","version":"0.0.4","author":{"name":"Shu Ding"},"license":"MIT","_id":"stable-hash@0.0.4","homepage":"https://github.com/shuding/stable-hash#readme","bugs":{"url":"https://github.com/shuding/stable-hash/issues"},"dist":{"shasum":"55ae7dadc13e4b3faed13601587cec41859b42f7","tarball":"http://registry-npm.ywwl.com/stable-hash/download/stable-hash-0.0.4.tgz","fileCount":5,"integrity":"sha512-LjdcbuBeLcdETCrPn9i8AYAZ1eCtu4ECAWtP7UleOiZ9LzVxRzzUZEoZ8zB24nhkQnDWyET0I+3sWokSDS3E7g==","signatures":[{"sig":"MEUCIQCSqLxFmXEwvIYovKVKjOLog+AKWlIr4Yc/QWRCs88TJgIgYbnV92ZpafxBw9O0ViQS7NaKI026S9VEKiEoUoWnsjQ=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":5463,"size":2445,"noattachment":false,"key":"/stable-hash/-/stable-hash-0.0.4.tgz"},"main":"./dist/index.js","_from":"file:stable-hash-0.0.4.tgz","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.js"},"scripts":{"test":"jest","build":"yarn build:mjs && yarn build:cjs && yarn build:types","build:cjs":"esbuild src/index.ts --minify --target=es6 --outdir=dist --format=cjs","build:mjs":"esbuild src/index.ts --minify --target=es6 --outdir=dist --out-extension:.js=.mjs","build:types":"tsc --emitDeclarationOnly --declaration -p tsconfig.build.json"},"_npmUser":{"name":"quietshu","email":"g@shud.in"},"prettier":{"semi":false},"_resolved":"/private/var/folders/zr/9p6mp0r13fn3s9fxzxp33_280000gn/T/efbcc7f038e7d3415427d1fb0c552b52/stable-hash-0.0.4.tgz","_integrity":"sha512-LjdcbuBeLcdETCrPn9i8AYAZ1eCtu4ECAWtP7UleOiZ9LzVxRzzUZEoZ8zB24nhkQnDWyET0I+3sWokSDS3E7g==","repository":{"url":"git+https://github.com/shuding/stable-hash.git","type":"git"},"_npmVersion":"9.6.6","description":"Stable JS value hash.","directories":{},"_nodeVersion":"20.2.0","_hasShrinkwrap":false,"devDependencies":{"jest":"^28.1.1","esbuild":"^0.12.28","flattie":"^1.1.0","ts-jest":"^28.0.5","hash-obj":"^4.0.0","prettier":"^2.7.1","nanobench":"^2.1.1","base64-url":"^2.3.3","typescript":"^4.7.4","@types/jest":"^28.1.3","json-stringify-deterministic":"^1.0.7"},"_npmOperationalInternal":{"tmp":"tmp/stable-hash_0.0.4_1707852273433_0.5170144516727231","host":"s3://npm-registry-packages"},"readme":"# stable-hash\n\nA 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).\n\nIt's similar to `JSON.stringify(value)`, but:\n1. Supports any JavaScript value (BigInt, NaN, Symbol, function, class, ...)\n2. Sorts object keys (stable)\n3. Supports circular objects\n\n## Use\n\n```bash\nyarn add stable-hash\n```\n\n```js\nimport hash from 'stable-hash'\n\nhash(anyJavaScriptValueHere) // returns a string\n```\n\n## Examples\n\n### Primitive Value\n\n```js\nhash(1)\nhash('foo')\nhash(true)\nhash(undefined)\nhash(null)\nhash(NaN)\n```\n\nBigInt:\n\n```js\nhash(1) === hash(1n)\nhash(1) !== hash(2n)\n```\n\nSymbol:\n\n```js\nhash(Symbol.for('foo')) === hash(Symbol.for('foo'))\nhash(Symbol.for('foo')) === hash(Symbol('foo'))\nhash(Symbol('foo')) === hash(Symbol('foo'))\nhash(Symbol('foo')) !== hash(Symbol('bar'))\n```\n\n_Since Symbols cannot be serialized, stable-hash simply uses its description as the hash._\n\n### Regex\n\n```js\nhash(/foo/) === hash(/foo/)\nhash(/foo/) !== hash(/bar/)\n```\n\n### Date\n\n```js\nhash(new Date(1)) === hash(new Date(1))\n```\n\n### Array\n\n```js\nhash([1, '2', [new Date(3)]]) === hash([1, '2', [new Date(3)]])\nhash([1, 2]) !== hash([2, 1])\n```\n\nCircular:\n\n```js\nconst foo = []\nfoo.push(foo)\nhash(foo) === hash(foo)\n```\n\n### Object\n\n```js\nhash({ foo: 'bar' }) === hash({ foo: 'bar' })\nhash({ foo: { bar: 1 } }) === hash({ foo: { bar: 1 } })\n```\n\nStable:\n\n```js\nhash({ a: 1, b: 2, c: 3 }) === hash({ c: 3, b: 2, a: 1 })\n```\n\nCircular:\n\n```js\nconst foo = {}\nfoo.foo = foo\nhash(foo) === hash(foo)\n```\n\n### Function, Class, Set, Map, Buffer...\n\n`stable-hash` guarantees reference consistency (`===`) for objects that the constructor isn't `Object`.\n\n```js\nconst foo = () => {}\nhash(foo) === hash(foo)\nhash(foo) !== hash(() => {})\n```\n\n```js\nclass Foo {}\nhash(Foo) === hash(Foo)\nhash(Foo) !== hash(class {})\n```\n\n```js\nconst foo = new Set([1])\nhash(foo) === hash(foo)\nhash(foo) !== hash(new Set([1]))\n```\n\n## Notes\n\nThis 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:\n\n- Use another encoding layer on top of it if you want to display the output. \n- Use another crypto layer on top of it if you want to have a secure and fixed length hash.\n\n```js\nimport crypto from 'crypto'\nimport hash from 'stable-hash'\n\nconst weakHash = hash(anyJavaScriptValueHere)\nconst encodedHash = Buffer.from(weakHash).toString('base64')\nconst safeHash = crypto.createHash('MD5').update(weakHash).digest('hex')\n```\n\nAlso, 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.\n\n## License\n\nCreated by Shu Ding. Released under the MIT License.\n","publish_time":1707852273587,"maintainers":[{"name":"quietshu","email":"g@shud.in"}],"_cnpm_publish_time":1707852273587,"dist-tags":{"latest":"0.0.6"}}