Total Complexity | 17 |
Complexity/F | 8.5 |
Lines of Code | 35 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import Field from "../../Store/Model/Field"; |
||
2 | import Relation from "../../Store/Model/Relation"; |
||
3 | import MorphTo from "../../Store/Model/Field/MorphTo"; |
||
4 | |||
5 | export default function (model, fromRelation = false) { |
||
6 | const json = {}; |
||
7 | |||
8 | for (let i = 0; i < model.originalFields.length; i++) { |
||
9 | const field = model.originalFields[i]; |
||
10 | if (field instanceof Relation && fromRelation) { |
||
11 | continue |
||
12 | } |
||
13 | |||
14 | if (field instanceof MorphTo && fromRelation) { |
||
15 | continue; |
||
16 | } |
||
17 | |||
18 | |||
19 | if (Array.isArray(json[field.name])) { |
||
20 | json[field.name] = arrayToObjects(json, field); |
||
21 | } else if (field instanceof Relation) { |
||
22 | json[field.name] = field?.value?.toObject?.(true) ?? null; |
||
23 | } else { |
||
24 | json[field.name] = field.value; |
||
25 | } |
||
26 | } |
||
27 | |||
28 | return {...json}; |
||
29 | } |
||
30 | |||
31 | function arrayToObjects(json: object, field: Field): Array<unknown> { |
||
32 | return [...json[field.name].map((value) => { |
||
33 | return (value?.toObject(true) ?? value) |
||
34 | }) ?? []]; |
||
35 | } |