src/Util/Obj/fromModel.ts   A
last analyzed

Complexity

Total Complexity 17
Complexity/F 8.5

Size

Lines of Code 35
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 17
mnd 15
bc 15
fnc 2
bpm 7.5
cpm 8.5
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
F fromModel.ts ➔ arrayToObjects 0 5 17
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
}