Skip to content

Commit

Permalink
replace JSON.parseImmutable with links to seperate proposal (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
acutmore authored Aug 6, 2022
1 parent c57bf32 commit 3331d6e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 190 deletions.
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.

0 comments on commit 3331d6e

Please sign in to comment.