blakeembrey / blakeembrey/javascript-stringify
Add support for getters & setters in objects
- Linguagem predominante
- TypeScript
- Estrelas
- 146
- Forks
- 16
- Métricas de merge de PRs
- Nenhum PR com merge em 30d
Descrição
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.
Guia de contribuição
Nenhum guia de contribuição indexado para este repositório
Direção de pesquisa
Start with the object stringification logic in src/object.ts and compare it with the linked demo commit. Trace how property descriptors and the lexer logic produce the object output, then verify that the getter/setter example serializes as "{_foo:null,foo:null}" without breaking ordinary properties.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- javascript, typescript
- Domínio
- tooling
- Tipo de issue
- Funcionalidade
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 38/100