{"name":"replicator","version":"1.0.5","description":"Advanced JavaScript objects serialization.","main":"index.js","scripts":{"test":"eslint index.js test && mocha","publish-please":"publish-please","prepublish":"publish-please guard"},"repository":{"type":"git","url":"git+https://github.com/inikulin/replicator.git"},"keywords":["JSON","serialization","deserialization","circular","circularJSON","structured-clone","structured","clone"],"author":{"name":"Ivan Nikulin","url":"ifaaan@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/inikulin/replicator/issues"},"homepage":"https://github.com/inikulin/replicator#readme","devDependencies":{"eslint":"^2.9.0","mocha":"^8.4.0","publish-please":"^5.4.3"},"gitHead":"c0ebc6e7c307ff1789ff531abdd925e2cf565c16","_id":"replicator@1.0.5","_nodeVersion":"14.16.1","_npmVersion":"6.14.12","dist":{"integrity":"sha512-saxS4y7NFkLMa92BR4bPHR41GD+f/qoDAwD2xZmN+MpDXgibkxwLO2qk7dCHYtskSkd/bWS8Jy6kC5MZUkg1tw==","shasum":"f1e56df7e276a62afe80c2248b8ac03896f4708f","tarball":"http://registry-npm.ywwl.com/replicator/download/replicator-1.0.5.tgz","fileCount":4,"unpackedSize":22978,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgq1nrCRA9TVsSAnZWagAAVKwP/iG6PVICzyxtzYtwm9xm\nmCMgfFlJRdBs/W5gYnSDvyP6/+RbBWPejErJwxadWHViF22A4KO2qCWh6teV\nLMm9FjKe1okLOFP8j68uKz1loEi2REVnFlALOKoQrbikeLfRX7WQp8PhmxgL\nCevGXBIdmIDL2QEkJXUetfG1I3S+0DzW1/bGidms5vC2xowVNMPeLO9tGoEq\n0koDn4fmF11fXUmAG8dzyvwP8elMMmp4LxOvRC1Ot8LKm0JIMoAUgeEpSJk1\nbF6+dzzkLnP600olmkA+uwQ2YUuY02bFR1klEnGIWQ40mwKQCGPan/IcVSW7\nioUHnTYeQ6BIWjisvTLF+MjlJI05e1f00VF+/A2IRXYBSKSsqKEufzIevqzH\nneo355Q2DbvtZgHV06pqqAe9UIvYjmbQLIw8ugAP65erv6/5zxQs5atxUEud\nL4zAwtOw0KmzFqvzx962yMurOQ/pmgf+ecdXeB7BM6AddEJetwUd8u9Hvqeh\nZpF83AROAkvYBiQyAdU2SUWtkJ0XNEvf3NW0ILzaH7WvxE4Usawt6cyUGjW4\n1hEr4jLTe7dIcGioR2SrbM4RSa1Ab5tEa8LPq+NNfIxJokH6qO/e/uid4Jo2\np3CmQV8DGuK4ihmnDezYYBihs6NQfq35FiaMOQ7jwJQ8BZmG5PecevrLWvgw\nvzbB\r\n=B7L+\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDFJC0sYwmhIztJ17hTWzie0iUwv9KEtB9YWNP9JyM/OgIhAIURd287qZWuIbJ4ldt7XJeK5iH199YGPbonyG/5kKTR"}],"size":6300,"noattachment":false,"key":"/replicator/-/replicator-1.0.5.tgz"},"_npmUser":{"name":"belym.a.2105","email":"belym.a.2105@gmail.com"},"directories":{},"maintainers":[{"name":"inikulin","email":"ifaaan@gmail.com"},{"name":"amoskovkin","email":"henry71rus@gmail.com"},{"name":"belym.a.2105","email":"belym.a.2105@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/replicator_1.0.5_1621842410589_0.030243448736596346"},"_hasShrinkwrap":false,"readme":"<p align=\"center\">\n    <a href=\"https://github.com/inikulin/replicator\">\n        <img src=\"https://raw.github.com/inikulin/replicator/master/media/logo.png\" alt=\"replicator\" />\n    </a>\n</p>\n<p align=\"center\">\n<i>Advanced JavaScript objects serialization</i>\n</p>\n<p align=\"center\">\n  <a href=\"https://github.com/inikulin/replicator/commits/master\"><img alt=\"GitHub branch checks state\" src=\"https://img.shields.io/github/checks-status/inikulin/replicator/master?label=tests\"></a>\n  <a href=\"https://www.npmjs.com/package/replicator\"><img alt=\"NPM Version\" src=\"https://img.shields.io/npm/v/replicator.svg\"></a>\n</p>\n\n- Can serialize circular references\n- In addition to JSON-serializable types can serialize:\n  - `undefined`\n  - `NaN`\n  - `Date`\n  - `RegExp`\n  - `Error`<sup>[1](#note1)</sup>\n  - `Map`<sup>[2](#note2)</sup>\n  - `Set`<sup>[3](#note3)</sup>\n  - `ArrayBuffer`<sup>[3](#note3)</sup>\n  - Typed arrays<sup>[3](#note3)</sup>\n- [Can be extended with custom type transforms](#adding-custom-types-support)\n- [Can use any target serializer under the hood](#changing-serialization-format) (JSON, BSON, protobuf, etc.)\n\n----\n<a name=\"note1\">1</a>: If decoding target platform doesn't support encoded error type, it will fallback to `Error` constructor.<br>\n<a name=\"note2\">2</a>: If decoding target platform doesn't support `Map`, it will be decoded as array of `[key, value]`.<br>\n<a name=\"note3\">3</a>: If decoding target platform doesn't support `Set`, `ArrayBuffer` or typed arrays, they will be decoded as array. <br>\n\n## Install\n```shell\nnpm install replicator\n```\n\n## Usage\n```js\nconst Replicator = require('replicator');\n\nconst replicator = new Replicator();\n\nconst a = {};\na.b = a;\n\nconst str = replicator.encode({\n    key1: new Set([1, 2, 3]),\n    key2: /\\s+/ig,\n    key3: a\n});\n\nconst obj = replicator.decode(str);\n```\n\n\n## Adding custom types support\nYou can extend `replicator` with custom type transform which will describe how to serialize/deserialize objects. You can\nadd transforms using `.addTransforms(transforms)` method. And remove them using `.removeTransforms(transforms)` method.\nBoth methods are chainable and accept single transform or array of transforms. You should add transforms to both encoding\nand decoding instances of `replicator`.\n\nLet's create transform which will encode `NodeList` of elements and decode it as array of objects with `tagName` property:\n```js\nconst Replicator = require('replicator');\n\nconst replicator = new Replicator();\n\nreplicator.addTransforms([\n    {\n        type: 'NodeList',\n\n        shouldTransform (type, val) {\n            return typeof NodeList === 'function' && val instanceof NodeList;\n        },\n\n        toSerializable (nodeList) {\n            // We should transform NodeList to primitive serializable object.\n            // It's an array of HTMLElement in our case.\n            // Note that it's not required to transform each element in\n            // NodeList. We can add HTMLElement transform which\n            // will transform NodeList items and individual elements as well.\n            return Array.prototype.slice.call(nodeList);\n        },\n\n        fromSerializable (val){\n            // Now we should describe how to restore NodeList from serializable object.\n            // In our case we just need an array so we'll return it as is.\n            // If you want to restore it as NodeList you can create document fragment, append\n            // array contents to it and return result of `fragment.querySelectorAll('*')` .\n            return val;\n        }\n    },\n\n    {\n        type: 'Element',\n\n        shouldTransform (type, val){\n            return typeof HTMLElement === 'function' && val instanceof HTMLElement;\n        },\n\n        toSerializable (element) {\n            return element.tagName;\n        },\n\n        fromSerializable (val) {\n            return { tagName: val };\n        }\n    }\n]);\n\nvar str = replicator.encode(document.querySelectorAll('div'));\n\nconsole.log(replicator.decode(str));\n// > [ { tagName: 'div'}, { tagName: 'div'}, { tagName: 'div'}]\n```\n\nBuilt-in types support implemented using transforms, so you can take a look on `replicator` source code for more examples.\n\n## Changing serialization format\nBy default `replicator` uses JSON under the hood. But you can use any serializer by passing serializer adapter to `Replicator`\nconstructor. E.g., let's use [BSON](https://www.npmjs.com/package/bson) as serializer:\n```js\nconst Replicator = require('replicator');\nconst BSON       = require('bson');\n\nconst replicator = new Replicator({\n    serialize (val) {\n        return BSON.serialize(val, false, true, false);\n    },\n\n    deserialize: BSON.deserialize\n});\n\nreplicator.encode(['yo', 42]);\n// > <Buffer>\n```\n\n## Author\n[Ivan Nikulin](https://github.com/inikulin) (ifaaan@gmail.com)\n","publish_time":1621842410709,"_cnpm_publish_time":1621842410709,"dist-tags":{"latest":"1.0.5"}}