foldspander
Foldspander is a JavaScript utility to fold complex objects into simpler ones fit for serialization, and then expand them back into their originals.
Perfect for seamless object sharing between the client-side and the server-side!
Table of contents
Installation
Install the lastest version of foldspander
from npm by executing npm install foldspander
in your shell.
Usage
Usage is simple and consists of creating an instance of foldspander and assigning different types to it.
Basic example showcasing many of the features in examples/showcase.js
:
var Foldspander = ;var should = ; // Basic Class { thisnumerator = numerator; thisdenominator = denominator;}; var foldspand = // option that indicates that we want to have automatic support for some native Javascript types native: true ; // add Fraction as a foldspander typefoldspand; var objs = id: 1 value: 13 created_at: '2015-03-23' id: 2 value: 17 created_at: '2015-03-24'; // get a string of JSON from the list of objectsvar str = foldspand; // parse the string and instantiate the contained objectsvar exp = foldspand; // objs are equal to expexpshould; // more verbose example to show that complex objects have indeed been instantiatedexp0valueshouldbe;exp1created_atshouldbe;
Native objects
Foldspander provides support for dealing with some native Javascript objects. A Foldspander instance can be prepared to accept these types by passing the option native
to the constuctor, or using the instance method native
.
The currently supported objects are:
Date
RegExp
NaN
Infinity
With more to come!
Replacer and Revivers
JSON.stringify
and JSON.parse
take optional arguments named replacer
and reviver
respectively. An instance of Foldspander provides hooks for these arguments that will apply the rules of the instance. Example in examples/revive_and_replace.js
:
var Foldspander = ;var should = ; var foldspander = native: true;var obj = id: 123 created_at: ; var str = JSON;var exp = JSON; expshould;
As a convenience, a Foldspander instance also provides a wrapper around JSON.stringify
and JSON.parse
. Example in examples/stringify_and_parse.js
:
var Foldspander = ;var should = ; var foldspander = native: true;var obj = id: 123 created_at: ; var str = foldspander;var exp = foldspander; expshould;
Helpers and custom objects
It is simple to add your own custom objects. See examples/showcase.js
or look into the Usage section of this document.
In addition to the basic add
method on a Foldspander instance, some helpers are provided.
addClass
addClass
is a simple helper for basic types. It requires only a (named!) function (dubbed "class") as its first argument to work. It will add an object to a foldspander instance with the following properties:
name
- Name of the classmatcher
- Matches if an object is an instanceof the classfold
- Picks every enumerable property of the objectexpand
- Makes an instance of class and then copies every folded property into the instance
It works good for simple classes. Example in examples/add_class.js
:
var Foldspander = ;var should = ; { thisnumerator = numerator; thisdenominator = denominator;}; var foldspander = ; foldspander; var obj = 13; // 1) Every enumerable property was picked of objvar fld = foldspander; // 2) Fraction was instantiated with no arguments to the constructor// 3) Every property picked in 1) was copied into instance from 2)var exp = foldspander; expshouldandnot;
Nested objects
Nested objects may be serialized as well. The object walker will descend again into a folded object. Example involving a Collection of Model objects in examples/nested_objects.js
:
var Foldspander = ;var should = ; // a model class (loosely based on Backbone.Model) { thisattributes = attrs || {};} // a collection of models class (loosely based on Backbone.Collection) { thismodels = models || ;} var foldspander = native: true; foldspander; foldspander; var collection = id: 1 created_at: id: 2 created_at: ; var fld = foldspander;var exp = foldspander; // equal === strict equality (actually created a new object)expshouldandnot; // verbose example to show that models are actually createdexpmodels0shouldbeandnot;
A similar setup might be used when folding and expanding Backbone objects.
Tests
To run the mocha.js tests, go the the project folder and run:
npm install --devnode_modules/.bin/mocha
TODO
- Support more native types like WeakMap etc.
- Simpler functions for adding types that are instances of
- Client versions
Performance
Contributing
Highlighting issues or submitting pull requests on Github is most welcome.