blakeembrey / blakeembrey/javascript-stringify
Add support for getters & setters in objects
- Lenguaje dominante
- TypeScript
- Estrellas
- 146
- Forks
- 16
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
Currently, objects with [getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get) and [setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set) are not handled correctly:
```ts
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](https://github.com/blakeembrey/javascript-stringify/blob/master/src/object.ts#L8) that checks for the given descriptors:
```ts
const descriptor = Object.getOwnPropertyDescriptor(obj, key);
if (descriptor && (descriptor.get || descriptor.set)) { ... }
```
I made a demo in a forked repo with [this commit](https://github.com/Etheryte/javascript-stringify/commit/f1c0c7b834fc7d0ef2e1f0cfd4dd8273557f4563) 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.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Evaluación
Este issue todavía no se ha evaluado.