UNPKG

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