npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

replicator

v1.0.5

Published

Advanced JavaScript objects serialization.

Downloads

932,911

Readme


1: If decoding target platform doesn't support encoded error type, it will fallback to Error constructor. 2: If decoding target platform doesn't support Map, it will be decoded as array of [key, value]. 3: If decoding target platform doesn't support Set, ArrayBuffer or typed arrays, they will be decoded as array.

Install

npm install replicator

Usage

const Replicator = require('replicator');

const replicator = new Replicator();

const a = {};
a.b = a;

const str = replicator.encode({
    key1: new Set([1, 2, 3]),
    key2: /\s+/ig,
    key3: a
});

const obj = replicator.decode(str);

Adding custom types support

You can extend replicator with custom type transform which will describe how to serialize/deserialize objects. You can add transforms using .addTransforms(transforms) method. And remove them using .removeTransforms(transforms) method. Both methods are chainable and accept single transform or array of transforms. You should add transforms to both encoding and decoding instances of replicator.

Let's create transform which will encode NodeList of elements and decode it as array of objects with tagName property:

const Replicator = require('replicator');

const replicator = new Replicator();

replicator.addTransforms([
    {
        type: 'NodeList',

        shouldTransform (type, val) {
            return typeof NodeList === 'function' && val instanceof NodeList;
        },

        toSerializable (nodeList) {
            // We should transform NodeList to primitive serializable object.
            // It's an array of HTMLElement in our case.
            // Note that it's not required to transform each element in
            // NodeList. We can add HTMLElement transform which
            // will transform NodeList items and individual elements as well.
            return Array.prototype.slice.call(nodeList);
        },

        fromSerializable (val){
            // Now we should describe how to restore NodeList from serializable object.
            // In our case we just need an array so we'll return it as is.
            // If you want to restore it as NodeList you can create document fragment, append
            // array contents to it and return result of `fragment.querySelectorAll('*')` .
            return val;
        }
    },

    {
        type: 'Element',

        shouldTransform (type, val){
            return typeof HTMLElement === 'function' && val instanceof HTMLElement;
        },

        toSerializable (element) {
            return element.tagName;
        },

        fromSerializable (val) {
            return { tagName: val };
        }
    }
]);

var str = replicator.encode(document.querySelectorAll('div'));

console.log(replicator.decode(str));
// > [ { tagName: 'div'}, { tagName: 'div'}, { tagName: 'div'}]

Built-in types support implemented using transforms, so you can take a look on replicator source code for more examples.

Changing serialization format

By default replicator uses JSON under the hood. But you can use any serializer by passing serializer adapter to Replicator constructor. E.g., let's use BSON as serializer:

const Replicator = require('replicator');
const BSON       = require('bson');

const replicator = new Replicator({
    serialize (val) {
        return BSON.serialize(val, false, true, false);
    },

    deserialize: BSON.deserialize
});

replicator.encode(['yo', 42]);
// > <Buffer>

Author

Ivan Nikulin ([email protected])