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