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

backpack-node

v0.4.0

Published

Class library of useful things for Node.js

Downloads

51

Readme

Backpack (MIT)

Backpack

Class library of useful things for Node.js

System

Serializer

True serialization have brought to Node.js!

Features:

  • Pure JS
  • Serializes to JSON or string
  • Handles circular objects
  • Deals with class (prototype) hierarchies
  • No effort required to make an object serializable, it just works
  • Serializes instance methods as well

It's implementation based on the awesome work of Zihua Li: node-serialize

Usage:

var Serializer = require("backpack-node").system.Serializer;
var s = new Serializer();
var obj = { a: "foo", b: "boo" };
obj.c = obj;

var str = s.stringify(obj);

var obj2 = s.parse(str);

// obj and obj2 will be identical yet separate instances

Constructor:

  • Serializer(options)

    options:

    • ignoreNativeFunc (Boolean):
      • true means objects with native function members won't be serialized (exception will be thrown)
      • false means objects with native function members will be serialized, and exception will be thrown by unserialized native functions when called.

Methods:

  • registerKnownType (typeName, type)

    Registers a type as a known, therefore serializable type. Argument 'typeName' have to be an unique name of that type, the 'type' argument is the constructor (see the example below for clarification).

    Remarks:

    Instances by having type of deep inheritance hierarchies can be serialized as well, but it is required to register it's type in the serializer to handle it properly. Deserialization won't call the constructor, only restores instance fields (even if they are functions), and restores the prototype.

    Example:

    var Serializer = require("../../").system.Serializer;
    var util = require("util");
      
    function Animal() {
        this.voice = null;
    }
    
    Animal.prototype.makeSound = function() {
        if (this.voice) {
            return "I say: " + this.voice + ".";
        }
    }
    
    function Cat() {
        Animal.call(this);
        this.voice = "meow";
    }
    
    util.inherits(Cat, Animal);
    
    function Dog() {
        Animal.call(this);
        this.voice = "bark";
    }
    
    util.inherits(Dog, Animal);
    
    function SpaceDog() {
        Dog.call(this);
        this.voice = "42";
    }
    
    util.inherits(SpaceDog, Dog);
    
    var cat = new Cat();
    var dog = new Dog();
    var spaceDog = new SpaceDog();
    // Add an instance-only function:
    spaceDog.futureStuff = function() {
        return "I think, therefore I am.";
    }
    
    test.equals(cat.makeSound(), "I say: meow.");
    test.equals(dog.makeSound(), "I say: bark.");
    test.equals(spaceDog.makeSound(), "I say: 42.");
    test.equals(spaceDog.futureStuff(), "I think, therefore I am.");
    test.ok(cat instanceof Animal);
    test.ok(cat instanceof Cat);
    test.ok(dog instanceof Animal);
    test.ok(dog instanceof Dog);
    test.ok(spaceDog instanceof Animal);
    test.ok(spaceDog instanceof Dog);
    test.ok(spaceDog instanceof SpaceDog);
    
    var ser = new Serializer();
    ser.registerKnownType("Cat", Cat);
    ser.registerKnownType("Dog", Dog);
    ser.registerKnownType("SpaceDog", SpaceDog);
    
    var data = ser.stringify(
        {
            cat: cat,
            dog: dog,
            spaceDog: spaceDog
        });
    
    test.ok(typeof data === "string");
    
    var deserialized = ser.parse(data);
    
    test.equals(deserialized.cat.makeSound(), "I say: meow.");
    test.equals(deserialized.dog.makeSound(), "I say: bark.");
    test.equals(deserialized.spaceDog.makeSound(), "I say: 42.");
    test.equals(deserialized.spaceDog.futureStuff(), "I think, therefore I am.");
    test.ok(deserialized.cat instanceof Animal);
    test.ok(deserialized.cat instanceof Cat);
    test.ok(deserialized.dog instanceof Animal);
    test.ok(deserialized.dog instanceof Dog);
    test.ok(deserialized.spaceDog instanceof Animal);
    test.ok(deserialized.spaceDog instanceof Dog);
    test.ok(deserialized.spaceDog instanceof SpaceDog);
    
    test.done();
  • stringify (obj)

    Serializes the object into a string.

  • toJSON (obj)

    Serializes the object into a plain old JSON object which is safe to stringify or put into MongoDb as is.

  • parse (str)

    Deserializes the object from it's string representation.

  • fromJSON (json)

    Deserializes the object from it's JSON representation.

Collections

Bag

[TODO to write docs later]