Stringifying

APIUtilsStringifying

Source

utils.simplify(object, options)

Parameters:
  • object — Object
  • options — Objectoptional

Convert the given object into the most simplified format with no cyclic references and more.

Such object can be easily stringified later using standard JSON.stringify().

Use utils.assemble() function to restore the object into the initial structure.

Second parameter is an config object (all 'false' by default):

var obj = {};
obj.self = obj;
console.log(JSON.stringify(utils.simplify(obj)));

If protos is false and constructors is true, object will be recognized as an instance.

function Sample(){
  this.fromInstance = 1;
}
Sample.prototype.fromPrototype = 1;

var sample = new Sample;
var parts = utils.simplify(sample, {constructors: true});
var clone = utils.assemble(json);

console.log(clone instanceof Sample)
// it's true because 'protos' option is false and 'constructors' is true
// won't work for json, because functions are not stringified ...

Source

utils.assemble(object)

Parameters:
  • object — PlainObject

Backward utils.simplify() operation.

Source