1 | const checkSerializableWithoutCircularReference = (o, seen, location) => {
|
2 | if (o === undefined || o === null || typeof o === 'boolean' || typeof o === 'number' || typeof o === 'string') {
|
3 | return {
|
4 | serializable: true
|
5 | };
|
6 | }
|
7 |
|
8 | if (Object.prototype.toString.call(o) !== '[object Object]' && !Array.isArray(o)) {
|
9 | return {
|
10 | serializable: false,
|
11 | location,
|
12 | reason: typeof o === 'function' ? 'Function' : String(o)
|
13 | };
|
14 | }
|
15 |
|
16 | if (seen.has(o)) {
|
17 | return {
|
18 | serializable: false,
|
19 | reason: 'Circular reference',
|
20 | location
|
21 | };
|
22 | }
|
23 |
|
24 | seen.add(o);
|
25 |
|
26 | if (Array.isArray(o)) {
|
27 | for (let i = 0; i < o.length; i++) {
|
28 | const childResult = checkSerializableWithoutCircularReference(o[i], new Set(seen), [...location, i]);
|
29 |
|
30 | if (!childResult.serializable) {
|
31 | return childResult;
|
32 | }
|
33 | }
|
34 | } else {
|
35 | for (const key in o) {
|
36 | const childResult = checkSerializableWithoutCircularReference(o[key], new Set(seen), [...location, key]);
|
37 |
|
38 | if (!childResult.serializable) {
|
39 | return childResult;
|
40 | }
|
41 | }
|
42 | }
|
43 |
|
44 | return {
|
45 | serializable: true
|
46 | };
|
47 | };
|
48 |
|
49 | export default function checkSerializable(o) {
|
50 | return checkSerializableWithoutCircularReference(o, new Set(), []);
|
51 | }
|
52 |
|
\ | No newline at end of file |