source: imaps-frontend/node_modules/@ungap/structured-clone/cjs/deserialize.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[d565449]1'use strict';
2const {
3 VOID, PRIMITIVE, ARRAY, OBJECT, DATE, REGEXP, MAP, SET, ERROR, BIGINT
4} = require('./types.js');
5
6const env = typeof self === 'object' ? self : globalThis;
7
8const deserializer = ($, _) => {
9 const as = (out, index) => {
10 $.set(index, out);
11 return out;
12 };
13
14 const unpair = index => {
15 if ($.has(index))
16 return $.get(index);
17
18 const [type, value] = _[index];
19 switch (type) {
20 case PRIMITIVE:
21 case VOID:
22 return as(value, index);
23 case ARRAY: {
24 const arr = as([], index);
25 for (const index of value)
26 arr.push(unpair(index));
27 return arr;
28 }
29 case OBJECT: {
30 const object = as({}, index);
31 for (const [key, index] of value)
32 object[unpair(key)] = unpair(index);
33 return object;
34 }
35 case DATE:
36 return as(new Date(value), index);
37 case REGEXP: {
38 const {source, flags} = value;
39 return as(new RegExp(source, flags), index);
40 }
41 case MAP: {
42 const map = as(new Map, index);
43 for (const [key, index] of value)
44 map.set(unpair(key), unpair(index));
45 return map;
46 }
47 case SET: {
48 const set = as(new Set, index);
49 for (const index of value)
50 set.add(unpair(index));
51 return set;
52 }
53 case ERROR: {
54 const {name, message} = value;
55 return as(new env[name](message), index);
56 }
57 case BIGINT:
58 return as(BigInt(value), index);
59 case 'BigInt':
60 return as(Object(BigInt(value)), index);
61 }
62 return as(new env[type](value), index);
63 };
64
65 return unpair;
66};
67
68/**
69 * @typedef {Array<string,any>} Record a type representation
70 */
71
72/**
73 * Returns a deserialized value from a serialized array of Records.
74 * @param {Record[]} serialized a previously serialized value.
75 * @returns {any}
76 */
77const deserialize = serialized => deserializer(new Map, serialized)(0);
78exports.deserialize = deserialize;
Note: See TracBrowser for help on using the repository browser.