-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.js
More file actions
25 lines (21 loc) · 581 Bytes
/
JSON.js
File metadata and controls
25 lines (21 loc) · 581 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* global Record Tuple */
if (! (JSON.parseImmutable instanceof Function)) {
function getImmutable(thing) {
switch (typeof thing) {
case 'object':
if (thing === null) {
return null;
} else if (Array.isArray(thing)) {
return Tuple.from(thing.map(item => getImmutable(item)));
}
return Record.fromEntries(
Object.entries(thing).map(([key, val]) => [key, getImmutable(val)])
);
default:
return thing;
}
}
JSON.parseImmutable = function parseImmutable(str) {
return JSON.parse(str, (_, value) => getImmutable(value));
};
}