{"id":13447637,"url":"https://github.com/inikulin/replicator","last_synced_at":"2025-05-07T15:22:14.343Z","repository":{"id":51094550,"uuid":"56719170","full_name":"inikulin/replicator","owner":"inikulin","description":"Advanced JavaScript objects serialization.","archived":false,"fork":false,"pushed_at":"2023-08-28T15:00:22.000Z","size":89,"stargazers_count":25,"open_issues_count":7,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-27T07:19:08.611Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/inikulin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-04-20T20:35:46.000Z","updated_at":"2022-08-15T12:53:40.000Z","dependencies_parsed_at":"2024-01-18T15:59:02.149Z","dependency_job_id":"30baff74-c056-4a23-93d6-660d357f2156","html_url":"https://github.com/inikulin/replicator","commit_stats":{"total_commits":43,"total_committers":6,"mean_commits":7.166666666666667,"dds":0.5116279069767442,"last_synced_commit":"c0ebc6e7c307ff1789ff531abdd925e2cf565c16"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inikulin%2Freplicator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inikulin%2Freplicator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inikulin%2Freplicator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inikulin%2Freplicator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inikulin","download_url":"https://codeload.github.com/inikulin/replicator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252902851,"owners_count":21822324,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T05:01:23.130Z","updated_at":"2025-05-07T15:22:14.323Z","avatar_url":"https://github.com/inikulin.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/inikulin/replicator\"\u003e\n        \u003cimg src=\"https://raw.github.com/inikulin/replicator/master/media/logo.png\" alt=\"replicator\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n\u003ci\u003eAdvanced JavaScript objects serialization\u003c/i\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/inikulin/replicator/commits/master\"\u003e\u003cimg alt=\"GitHub branch checks state\" src=\"https://img.shields.io/github/checks-status/inikulin/replicator/master?label=tests\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/replicator\"\u003e\u003cimg alt=\"NPM Version\" src=\"https://img.shields.io/npm/v/replicator.svg\"\u003e\u003c/a\u003e\n\u003c/p\u003e\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`\u003csup\u003e[1](#note1)\u003c/sup\u003e\n  - `Map`\u003csup\u003e[2](#note2)\u003c/sup\u003e\n  - `Set`\u003csup\u003e[3](#note3)\u003c/sup\u003e\n  - `ArrayBuffer`\u003csup\u003e[3](#note3)\u003c/sup\u003e\n  - Typed arrays\u003csup\u003e[3](#note3)\u003c/sup\u003e\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\u003ca name=\"note1\"\u003e1\u003c/a\u003e: If decoding target platform doesn't support encoded error type, it will fallback to `Error` constructor.\u003cbr\u003e\n\u003ca name=\"note2\"\u003e2\u003c/a\u003e: If decoding target platform doesn't support `Map`, it will be decoded as array of `[key, value]`.\u003cbr\u003e\n\u003ca name=\"note3\"\u003e3\u003c/a\u003e: If decoding target platform doesn't support `Set`, `ArrayBuffer` or typed arrays, they will be decoded as array. \u003cbr\u003e\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' \u0026\u0026 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' \u0026\u0026 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// \u003e [ { 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// \u003e \u003cBuffer\u003e\n```\n\n## Author\n[Ivan Nikulin](https://github.com/inikulin) (ifaaan@gmail.com)\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finikulin%2Freplicator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finikulin%2Freplicator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finikulin%2Freplicator/lists"}