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

replace JSON.parseImmutable with links to the companion proposal #341

Merged
merged 1 commit into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,7 @@ JSON.stringify(#[true, #{ a: #[1, 2, 3] }]); // '[true,{"a":[1,2,3]}]'

## JSON.parseImmutable

We propose to add `JSON.parseImmutable` so we can extract a Record/Tuple type out of a JSON string instead of an Object/Array.

The signature of `JSON.parseImmutable` is identical to `JSON.parse` with the only change being in the return type that is now a Record or a Tuple.
Please see https://github.com/tc39/proposal-json-parseimmutable

## `Tuple.prototype`

Expand Down
19 changes: 12 additions & 7 deletions cookbook/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,7 @@ console.log(a === c); // false

### Parse

```js
const user = JSON.parseImmutable('{ "name": "danny", "admin": false, "score": 42 }');
console.log(user === #{ name: "danny", admin: false, score: 42 });
```
Please see https://github.com/tc39/proposal-json-parseimmutable

> Note: at the time of writing this, the playground does not support `JSON.parseImmutable`.

Expand All @@ -186,7 +183,17 @@ console.log(jsonUser); // {"admin":false,"name":"danny","score":42}

```js
function recursiveRT(thing) {
return JSON.parseImmutable(JSON.stringify(thing));
function reviver(value) {
if (typeof value === 'object' && value !== null) {
if (Array.isArray(value)) {
return Tuple.from(value);
} else {
return Record(value);
}
}
return value;
}
return JSON.parse(JSON.stringify(thing), reviver);
}

const user = { name: "danny", stats: {
Expand All @@ -200,8 +207,6 @@ console.log(fixedUser === #{ name: "danny", stats: #{
} }); // true
```

> Note: at the time of writing this, the playground does not support `JSON.parseImmutable`.

## Array-like manipulations with Tuple by copy

Using spread operations, `toReversed()`, `toSorted()`, `toSpliced()` or `with()` methods you can mutate tuples by copy:
Expand Down
1 change: 0 additions & 1 deletion spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
<emu-import href="spec/expression.html"></emu-import>
<emu-import href="spec/immutable-data-structures.html"></emu-import>
<emu-import href="spec/fundamental-objects.html"></emu-import>
<emu-import href="spec/structured-data.html"></emu-import>
<emu-import href="spec/purely-editorial.html"></emu-import>
179 changes: 0 additions & 179 deletions spec/structured-data.html

This file was deleted.