summaryrefslogtreecommitdiff
path: root/src/store/denormalizer.js
blob: e6583ae7503fecaf2998e47b1b08607feb4a7d04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const EXCLUDED_IDENTIFIERS = [
    "objectId",
    "googleId",
];

export function denormalize(state, objectType, id) {
    return denormalizeWithRecursionCheck(state, objectType, id, undefined);
}

function denormalizeWithRecursionCheck(state, objectType, id, previousType) {
    const object = Object.assign({}, state.objects[objectType][id]);

    for (let prop in object) {
        if (prop.indexOf(previousType) !== -1) {
            continue;
        }

        if (prop.endsWith("Id") && EXCLUDED_IDENTIFIERS.indexOf(prop) === -1) {
            const propType = prop.replace("Id", "");
            object[propType] = denormalizeWithRecursionCheck(state, propType, object[prop], objectType);
        }

        if (prop.endsWith("Ids")) {
            const propType = prop.replace("Ids", "");
            object[propType + "s"] = object[prop].map(id => denormalizeWithRecursionCheck(state, propType, id, objectType));
        }
    }

    return object;
}