source: frontend/node_modules/@ungap/structured-clone/esm/serialize.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.3 KB
Line 
1import {
2 VOID, PRIMITIVE,
3 ARRAY, OBJECT,
4 DATE, REGEXP, MAP, SET,
5 ERROR, BIGINT
6} from './types.js';
7
8const EMPTY = '';
9
10const {toString} = {};
11const {keys} = Object;
12
13const typeOf = value => {
14 const type = typeof value;
15 if (type !== 'object' || !value)
16 return [PRIMITIVE, type];
17
18 const asString = toString.call(value).slice(8, -1);
19 switch (asString) {
20 case 'Array':
21 return [ARRAY, EMPTY];
22 case 'Object':
23 return [OBJECT, EMPTY];
24 case 'Date':
25 return [DATE, EMPTY];
26 case 'RegExp':
27 return [REGEXP, EMPTY];
28 case 'Map':
29 return [MAP, EMPTY];
30 case 'Set':
31 return [SET, EMPTY];
32 case 'DataView':
33 return [ARRAY, asString];
34 }
35
36 if (asString.includes('Array'))
37 return [ARRAY, asString];
38
39 if (asString.includes('Error'))
40 return [ERROR, asString];
41
42 return [OBJECT, asString];
43};
44
45const shouldSkip = ([TYPE, type]) => (
46 TYPE === PRIMITIVE &&
47 (type === 'function' || type === 'symbol')
48);
49
50const serializer = (strict, json, $, _) => {
51
52 const as = (out, value) => {
53 const index = _.push(out) - 1;
54 $.set(value, index);
55 return index;
56 };
57
58 const pair = value => {
59 if ($.has(value))
60 return $.get(value);
61
62 let [TYPE, type] = typeOf(value);
63 switch (TYPE) {
64 case PRIMITIVE: {
65 let entry = value;
66 switch (type) {
67 case 'bigint':
68 TYPE = BIGINT;
69 entry = value.toString();
70 break;
71 case 'function':
72 case 'symbol':
73 if (strict)
74 throw new TypeError('unable to serialize ' + type);
75 entry = null;
76 break;
77 case 'undefined':
78 return as([VOID], value);
79 }
80 return as([TYPE, entry], value);
81 }
82 case ARRAY: {
83 if (type) {
84 let spread = value;
85 if (type === 'DataView') {
86 spread = new Uint8Array(value.buffer);
87 }
88 else if (type === 'ArrayBuffer') {
89 spread = new Uint8Array(value);
90 }
91 return as([type, [...spread]], value);
92 }
93
94 const arr = [];
95 const index = as([TYPE, arr], value);
96 for (const entry of value)
97 arr.push(pair(entry));
98 return index;
99 }
100 case OBJECT: {
101 if (type) {
102 switch (type) {
103 case 'BigInt':
104 return as([type, value.toString()], value);
105 case 'Boolean':
106 case 'Number':
107 case 'String':
108 return as([type, value.valueOf()], value);
109 }
110 }
111
112 if (json && ('toJSON' in value))
113 return pair(value.toJSON());
114
115 const entries = [];
116 const index = as([TYPE, entries], value);
117 for (const key of keys(value)) {
118 if (strict || !shouldSkip(typeOf(value[key])))
119 entries.push([pair(key), pair(value[key])]);
120 }
121 return index;
122 }
123 case DATE:
124 return as([TYPE, value.toISOString()], value);
125 case REGEXP: {
126 const {source, flags} = value;
127 return as([TYPE, {source, flags}], value);
128 }
129 case MAP: {
130 const entries = [];
131 const index = as([TYPE, entries], value);
132 for (const [key, entry] of value) {
133 if (strict || !(shouldSkip(typeOf(key)) || shouldSkip(typeOf(entry))))
134 entries.push([pair(key), pair(entry)]);
135 }
136 return index;
137 }
138 case SET: {
139 const entries = [];
140 const index = as([TYPE, entries], value);
141 for (const entry of value) {
142 if (strict || !shouldSkip(typeOf(entry)))
143 entries.push(pair(entry));
144 }
145 return index;
146 }
147 }
148
149 const {message} = value;
150 return as([TYPE, {name: type, message}], value);
151 };
152
153 return pair;
154};
155
156/**
157 * @typedef {Array<string,any>} Record a type representation
158 */
159
160/**
161 * Returns an array of serialized Records.
162 * @param {any} value a serializable value.
163 * @param {{json?: boolean, lossy?: boolean}?} options an object with a `lossy` or `json` property that,
164 * if `true`, will not throw errors on incompatible types, and behave more
165 * like JSON stringify would behave. Symbol and Function will be discarded.
166 * @returns {Record[]}
167 */
168 export const serialize = (value, {json, lossy} = {}) => {
169 const _ = [];
170 return serializer(!(json || lossy), !!json, new Map, _)(value), _;
171};
Note: See TracBrowser for help on using the repository browser.