blakeembrey / blakeembrey/javascript-stringify

Add support for getters & setters in objects

Đang mở
#26 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
TypeScript
Star
146
Fork
16
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

Bắt đầu với logic chuyển đối tượng thành chuỗi trong src/object.ts và so sánh nó với commit demo được liên kết. Theo dõi cách các property descriptor và logic lexer tạo ra đầu ra của đối tượng, sau đó xác minh rằng ví dụ getter/setter được serialize thành "{_foo:null,foo:null}" mà không làm hỏng các thuộc tính thông thường.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, typescript
Lĩnh vực
tooling
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
38/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.