Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

References in Map #18

Open
EmileSonneveld opened this issue Aug 1, 2020 · 2 comments
Open

References in Map #18

EmileSonneveld opened this issue Aug 1, 2020 · 2 comments

Comments

@EmileSonneveld
Copy link

Hi! This seems like a really promising library for debugging complex JS applications :) Or serialising complex JS objects.

In our code, we often use Map, but references in there get copied by value. Is there a way to generate JS code that will construct the full JS object?

var serialize = require('serialize-to-js')

var fooBar = {foo:"bar"}

let m = new Map()
m.set('key1', fooBar)
m.set('key2', fooBar)

var obj = {
  ref1: fooBar,
  ref2: fooBar,
  ref3: fooBar,
  m: m,
}

var opts = { reference: true }
var withoutRefs = serialize(obj, opts)
console.log(opts.references)

var refsCode = "var tmp = " + withoutRefs + ";\n"
for(var i = 0; i < opts.references.length; i+= 1){
    var entry = opts.references[i];
    refsCode += "tmp" + entry[0] + " = tmp" + entry[1] + ";\n"
}
console.log(refsCode)
// var tmp = {ref1: {foo: "bar"}}
// tmp.ref2 = tmp.ref1;
// tmp.ref3 = tmp.ref1;
// The refs in the map are missing :(
@commenthol
Copy link
Owner

Unfortunately I'm having troubles to correctly attribute the "references" within the Map.
With objects the path naming conventions are clear using dots or brackets.
With Map there is no such thing. Any thoughts?

@EmileSonneveld
Copy link
Author

For me saving a complete JS object is the main goal.
For me the ideal output of serialize(obj, opts) would be:

var obj = {
  ref1: {foo:"bar"},
  m: new Map(),
}
obj.ref2 = obj.ref1;
obj.ref3 = obj.ref1;
obj.m.set('key1', obj.ref1);
obj.m.set('key2', obj.ref2);

Only the first time an object is encountered, it is shown in the big tree. All other references are added latere on with separate statements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants