{"name":"replicator","version":"1.0.2","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"},"files":["index.js"],"homepage":"https://github.com/inikulin/replicator#readme","devDependencies":{"eslint":"^2.9.0","mocha":"^2.4.5","publish-please":"^2.1.0"},"gitHead":"bc375052ce12e7d5925adebeb0b681c5104b7f3e","_id":"replicator@1.0.2","_npmVersion":"5.6.0","_nodeVersion":"8.11.1","_npmUser":{"name":"inikulin","email":"ifaaan@gmail.com"},"dist":{"integrity":"sha512-y2yeUd+NDfSk6SHELNJfMwglmjDYxw65VKgDYMEYGLdJVnqz/sER0mFJbHrawqKfQoVRJ5vN3pSouL9ZLUVd8w==","shasum":"f21812f28f3b853a34795a7d52814581df652fe1","tarball":"http://registry-npm.ywwl.com/replicator/download/replicator-1.0.2.tgz","fileCount":4,"unpackedSize":21725,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJa45TPCRA9TVsSAnZWagAAkf0P/R3fk3cENM1Te4qCJ8pH\nMZkuqMAqTBZkUv2ya3SCcGNzakdG8AVbB7dHNXX1KeYyn2HR4TGmcj4kTcYV\nGy81J4j825p0ggONR5yDCKvyR6DDjhuokhAQ+tqg6pVFwLi4LT87knDcZJXo\nTjElQcPIRjWklDP+At6mseJvHV7XT7FizoW52z/dVFvT8p6HHPMP1GbkW9e4\nZ/3qe1qMuTex/FUOmkjtmXFMlposAswxvpE9Qcv31at7ktIBy+AmjZJsMRAn\n2Ox02ctZQaK9QS/x3+Xaj/vJR8ezJLEpYFCNPgTv5K24CCd/1BUDf+trjqh4\nHqXDzsyUBn8buJhp5Xx6/Em5VIOszBtMDeXKYymNXw7AlYpbvB39No1XolRh\ncjusEgObrXD1xAoJgjTrtsBPoegJvPKpZc8ffPJzizHuTb3fhO89Mmgk3TpR\nIYzdXxCfeEswxmzunG74N31F1Dwe6eJMZN7vQ+QXmGAs3ambpMCPQfw0QrZI\nCRkRWupGPSS21BhNUQmaTpt5ltMieQPOFxm9yWxWQIHkPVUI3+3p6lAwHykf\nb40JmEurk0r/C9OwZ8nPedi62zwpUupIjlcgtjyTKDIuiPj7xJM0oTj7cEO4\nMSRdhyrj63cNdgepSZxUZomdIJ3g9UWDn3AQLaJlXCHEoKxLK5w5N7ewTbsC\ns+S3\r\n=OmR5\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDOTnMZJ2cBrBpiPBx+k2/pFA16mfKqSZguQEhyLaNXKQIgUQcd/cWu+ijbadnDABc2gXnj+UZjrkBhI0elAsl8EaQ="}],"size":6117,"noattachment":false,"key":"/replicator/-/replicator-1.0.2.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.2_1524864205408_0.5413511931373594"},"_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":1524864205478,"_cnpm_publish_time":1524864205478,"dist-tags":{"latest":"1.0.5"}}