|
1 | | -import y from "@ungap/structured-clone"; |
2 | | -function A(e, r = {}) { |
3 | | - const s = y(e); |
4 | | - r || (r = {}); |
5 | | - const n = s.included || []; |
6 | | - return Array.isArray(s.data) ? s.data.map((i) => f(i, n, !1)) : f( |
| 1 | +function y(e, t = {}) { |
| 2 | + const s = structuredClone(e); |
| 3 | + t || (t = {}); |
| 4 | + const o = s.included || []; |
| 5 | + return Array.isArray(s.data) ? s.data.map((i) => f(i, o, !1)) : f( |
7 | 6 | s.data, |
8 | | - n, |
| 7 | + o, |
9 | 8 | !1 |
10 | 9 | ); |
11 | 10 | } |
12 | | -function f(e, r, s, n) { |
13 | | - if (r.cached || (r.cached = {}), e.type in r.cached || (r.cached[e.type] = {}), s && e.id in r.cached[e.type]) |
14 | | - return r.cached[e.type][e.id]; |
15 | | - const t = e.attributes || {}; |
16 | | - if (t.id = e.id, r.cached[e.type][e.id] = t, e.relationships) |
| 11 | +function f(e, t, s, o) { |
| 12 | + if (t.cached || (t.cached = {}), e.type in t.cached || (t.cached[e.type] = {}), s && e.id in t.cached[e.type]) |
| 13 | + return t.cached[e.type][e.id]; |
| 14 | + const r = e.attributes || {}; |
| 15 | + if (r.id = e.id, t.cached[e.type][e.id] = r, e.relationships) |
17 | 16 | for (const c of Object.keys(e.relationships)) { |
18 | | - const o = e.relationships[c]; |
19 | | - if (Array.isArray(o.data)) { |
| 17 | + const n = e.relationships[c]; |
| 18 | + if (Array.isArray(n.data)) { |
20 | 19 | const p = []; |
21 | | - o.data.forEach((a) => { |
| 20 | + n.data.forEach((a) => { |
22 | 21 | const h = u( |
23 | | - r, |
| 22 | + t, |
24 | 23 | a.type, |
25 | 24 | a.id |
26 | 25 | ); |
27 | 26 | p.push(h); |
28 | | - }), t[c] = p; |
29 | | - } else o && o.data ? t[c] = u( |
30 | | - r, |
31 | | - o.data.type, |
32 | | - o.data.id |
33 | | - ) : t[c] = null; |
| 27 | + }), r[c] = p; |
| 28 | + } else n && n.data ? r[c] = u( |
| 29 | + t, |
| 30 | + n.data.type, |
| 31 | + n.data.id |
| 32 | + ) : r[c] = null; |
34 | 33 | } |
35 | | - return t; |
| 34 | + return r; |
36 | 35 | } |
37 | | -function u(e, r, s, n) { |
| 36 | +function u(e, t, s, o) { |
38 | 37 | let i = null; |
39 | | - return e.forEach((t) => { |
40 | | - t.type === r && t.id === s && (i = f(t, e, !0)); |
| 38 | + return e.forEach((r) => { |
| 39 | + r.type === t && r.id === s && (i = f(r, e, !0)); |
41 | 40 | }), i || (i = { id: s }), i; |
42 | 41 | } |
43 | 42 | export { |
44 | | - A as deserialize |
| 43 | + y as deserialize |
45 | 44 | }; |
0 commit comments