{"name":"replicator","version":"1.0.3","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":"^5.2.0","publish-please":"^5.4.3"},"gitHead":"563f73eefa5555b15a3eb94be6ee6c64f92b34fc","_id":"replicator@1.0.3","_npmVersion":"6.4.1","_nodeVersion":"11.4.0","_npmUser":{"name":"belym.a.2105","email":"belym.a.2105@gmail.com"},"dist":{"integrity":"sha512-WsKsraaM0x0QHy5CtzdgFXUxyowoBhyNkmPqmZShW6h+rOWnyT6Od3zRdTX9r616rAA6kDC9MKQGnSM/CJKfVQ==","shasum":"c0b3ea31e749015bae5d52273f2ae35d541b87ef","tarball":"http://registry-npm.ywwl.com/replicator/download/replicator-1.0.3.tgz","fileCount":4,"unpackedSize":22612,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcF5N2CRA9TVsSAnZWagAA3wEP/1/5bdgPnY7sqCmRnWtp\nZA49DqjmvDWnjmodrRgueRwahzdoLTYSG8cYXXVv6l63UoKdSQaS7MzGzLCX\nuSzJtnAAqoeub+zAASrsGJWs5U7KptO39VtAj11ZZJ1a+gd+uFbe0C4qHXQf\nOkAo5nA9fbjlzuhBGpN/1Xz4OKxi7S+5rP6BYid/m7JnsXiXNlBvFMULih2b\n1zgg2RMQ9cRNt5A4cXTzJlTVH5RguVA9NKqkrATx2xThL92a9BU1kvCXnXK+\naFj3oqt99FsjWbwoe41huh2O0bimnaeWa55m6+Q+9tMkdNVaO/vD8AZ7cMnh\nN8+VVmtlM6bJqmGVLmxaiHszvhETlySVQIcInSbTKsA9rcmupnoSQlXEcoD+\nKIAnWR1GGli3E5UCYZSZ8BRwaCRTAmriHy2xmi8UkcvBpQwZ/uzZtS7zjnuv\n/6/L7pUpPyaoHWAueaY8oup0zAOitzDn8Fi9FZ4oGqyW1Hk/F38kPSFliCuG\nf+xS1xonF3LdiXmzgwxcGRqoW096gj5LDX2Xy+IC5nxpphddJ5vZZ+EUQdMy\nJ2p0cD1c+AD0y7jD5CJhFtOnHKKSw4H0kSCmkLph+HOCi46IMGh4+7iyDPzv\nGjKzwT+8AgB8k6SlyYgipVchXS/p5NYLhFYRbGf451/B+9uhZS8K2ipp9VOT\nW9xi\r\n=sH+G\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBMgabbyQvqAmXfgfn7CJ8j1FCwJe3szLvR45VFB900lAiA2pabBvpYFUK5baOpIxMiuEaReWM380N66Mqul8E796g=="}],"size":6169,"noattachment":false,"key":"/replicator/-/replicator-1.0.3.tgz"},"maintainers":[{"name":"amoskovkin","email":"alexander.moskovkin@devexpress.com"},{"name":"inikulin","email":"ifaaan@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/replicator_1.0.3_1545048949779_0.003553090082782262"},"_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":1545048949909,"_cnpm_publish_time":1545048949909,"dist-tags":{"latest":"1.0.5"}}