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

resurrect-ts

v2.0.1

Published

ResurrectJS preserves object behavior (prototypes) and reference circularity with a special JSON encoding. Unlike flat JSON, it can also properly resurrect these types of values:

Downloads

10

Readme

ResurrectTS

ResurrectJS with type declarations, tweaked to be a CommonJS module for node and bundlers.

ResurrectJS preserves object behavior (prototypes) and reference circularity with a special JSON encoding. Unlike flat JSON, it can also properly resurrect these types of values:

  • Date
  • RegExp
  • DOM objects
  • undefined
  • NaN, Infinity, -Infinity

Supported Browsers:

  • Chrome
  • Firefox
  • Safari
  • Opera
  • IE9+

Read about how it works.

Examples

import {Resurrect} from 'resurrect-ts';
function Foo() {}
Foo.prototype.greet = function() { return "hello"; };

// Behavior is preserved:
var necromancer = new Resurrect();
var json = necromancer.stringify(new Foo());
var foo = necromancer.resurrect(json);
foo.greet();  // => "hello"

// References to the same object are preserved:
json = necromancer.stringify([foo, foo]);
var array = necromancer.resurrect(json);
array[0] === array[1];  // => true
array[1].greet();  // => "hello"

// Dates are restored properly
json = necromancer.stringify(new Date());
var date = necromancer.resurrect(json);
Object.prototype.toString.call(date);  // => "[object Date]"

Options

Options are provided to the constructor as an object with these properties:

  • prefix ("#"): A prefix string used for temporary properties added to objects during serialization and deserialization. It is important that you don't use any properties beginning with this string. This option must be consistent between both serialization and deserialization.

  • cleanup (false): Perform full property cleanup after both serialization and deserialization using the delete operator. This may cause performance penalties (i.e. breaking hidden classes in V8) on objects that ResurrectJS touches, so enable with care.

  • revive (true): Restore behavior (__proto__) to objects that have been resurrected. If this is set to false during serialization, resurrection information will not be encoded. You still get circularity and Date support.

  • resolver (NamespaceResolver): Converts between a name and a prototype. Create a custom resolver if your constructors are not stored in global variables. The resolver has two methods: getName(object) and getPrototype(string).

For example,

import {Resurrect} from 'resurrect-ts';
var necromancer = new Resurrect({
    prefix: '__#',
    cleanup: true
});

Methods

Only two methods are significant when using ResurrectJS.

  • .stringify(object[, replacer[, space]]): Serializes an arbitrary object or value into a string. The replacer and space arguments are the same as JSON.stringify, being passed through to this method. Note that the replacer will not be called for ResurrectJS's intrusive keys.

  • .resurrect(string): Deserializes an object stored in a string by a previous call to .stringify(). Circularity and, optionally, behavior (prototype chain) will be restored.

Restrictions

With the default resolver, all constructors must be named and stored in the global variable under that name. This is required so that the prototypes can be looked up and reconnected at resurrection time.

The wrapper objects Boolean, String, and Number will be unwrapped. This means extra properties added to these objects will not be preserved.

Functions cannot ever be serialized. Resurrect will throw an error if a function is found when traversing a data structure.

Custom Resolvers

There is a caveat with the provided resolver, NamespaceResolver: all constructors must be explicitly named when defined. For example, see the Foo constructor in this example,

import {Resurrect, NamespaceResolver} from 'resurrect-ts';
const ns = {
    Foo() {
        this.bar = true;
    }
};
const necromancer = new Resurrect({
    resolver: new NamespaceResolver(ns)
});

The constructor been assigned to the Foo property and the function itself has been given a matching name. This is how the resolver will find the name of the constructor in the namespace when given the constructor. Keep in mind that using this form will bind the variable Foo to the surrounding function within the body of Foo.

See Also