{"name":"stable-hash","version":"0.0.1","author":{"name":"Shu Ding"},"license":"MIT","_id":"stable-hash@0.0.1","homepage":"https://github.com/shuding/stable-hash#readme","bugs":{"url":"https://github.com/shuding/stable-hash/issues"},"dist":{"shasum":"c7b079724ca04272dcf2ff314eb16a89aba25ca3","tarball":"http://registry-npm.ywwl.com/stable-hash/download/stable-hash-0.0.1.tgz","fileCount":3,"integrity":"sha512-HAUb9aBnn6t8qLKmZ+4roPtDfeLmJhorckQFnz8jsFBuHaII2CmsPU9pI0UXV18PUUcOaFEYCWYtlwQbfUvXWQ==","signatures":[{"sig":"MEQCIHaWLfAR4jPtoRd1nQETtQ2GBvmxLWS2ulB9Y06wz5xOAiBteNXDvioXqYIy3Y4CHAtPUzPlz//fJRu7bGRWA9w2yw==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":5630,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhQ9w/CRA9TVsSAnZWagAATScP/jZYmewaoJYcOKiTweHQ\nt+FLB4jUZ+9CpbTc56f3W+Yl0zdq2UXsKDrV0LTOYOojgG88WfY/CPbc+POc\nq+bv7Ll6pzkG31v/KfDpaAOPOE6z9RpB86z5uC47VRAmdUxTAETxnQFqctl/\njKf4Styjx6taKaBnHGdv9bg1TDvL5D1bZslCUATzZJ+isr7V+XviSpJVAPJE\n1Nu+RKFhkFsMSfmIknOsDFMQ2IatbSakrfj3m44IlxzJg/S0cIEMecaQTIes\npHS1es0IIzmLKzquWtFxfzxz1frb3rFVgaTY5rATiB71wNmDFe2qYFVbr/V6\nmLGeTiEaq2PwUA2RXGQwOt36KmchZXf7CnS8jxseZeumW+IoDS8GTpmJ8cuY\nDaf1kqEnz7dDRyf1t264Zvk9WTeIJ4H8AYLvWw+1RQzbSZaJCPDtQUwnBFSG\n4y0LVTi1MdXkY0OIPBRs2eRAl7C/aR69KpPpV5EdFrh2EQhVdeYRQ1TIG2li\nehCdPxWAT7WsDci8cfx/Ignrsbm72yHc1urQxDoQntWKE9HBIvGdeDlct3cI\nN2CB+tk2amqjYrvxTy3NSnCV0PIla/pYOGBrgr48jWHwe8KmDNR8v0+gcqF7\nTAH0RCYhwOnzbur97ZOkL22TFJ2jQh6+SSh7UOZU3JkIRtEkcKPzMdcknpjp\nmgCO\r\n=9lut\r\n-----END PGP SIGNATURE-----\r\n","size":2311,"noattachment":false,"key":"/stable-hash/-/stable-hash-0.0.1.tgz"},"main":"index.js","gitHead":"b2769b2943dc23f40aee2f8d470b0cb96160f4e3","scripts":{"build":"yarn build:mjs && yarn build:cjs","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"},"_npmUser":{"name":"quietshu","email":"ds303077135@gmail.com"},"prettier":{"semi":false},"repository":{"url":"git+https://github.com/shuding/stable-hash.git","type":"git"},"_npmVersion":"7.20.3","description":"Stable JS value hash.","directories":{},"_nodeVersion":"14.16.0","_hasShrinkwrap":false,"devDependencies":{"esbuild":"^0.12.28"},"_npmOperationalInternal":{"tmp":"tmp/stable-hash_0.0.1_1631837247742_0.31529162882147954","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":1631837247962,"maintainers":[{"name":"quietshu","email":"g@shud.in"}],"_cnpm_publish_time":1631837247962,"dist-tags":{"latest":"0.0.6"}}