javascript-stringify icon indicating copy to clipboard operation
javascript-stringify copied to clipboard

Add support for getters & setters in objects

Open Etheryte opened this issue 4 years ago • 0 comments

Currently, objects with getters and setters are not handled correctly:

const input = {
    _foo: null,
    get foo() {
        return this._foo;
    },
    set foo(value) {
        this._foo = value;
    }
};

const output = "{_foo:null,foo:null}";

This problem can be solved by adding an extra case to the object stringification logic that checks for the given descriptors:

const descriptor = Object.getOwnPropertyDescriptor(obj, key);
if (descriptor && (descriptor.get || descriptor.set)) { ... }

I made a demo in a forked repo with this commit demonstrating the approach.
Sadly, I didn't have time to figure out all of the lexer logic on the go so it isn't clean enough for a flat out PR.

Etheryte avatar Oct 08 '19 17:10 Etheryte