summaryrefslogtreecommitdiff
path: root/graphs/js/inspectAndFuse/inspectAndFuse.js
blob: 2072bb1ea27337c1955d4aeb40b3f25ff8b0be4b (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function getNumberFields(inputObject) {
    if (inputObject == null) {
        return new Array();
    }

    const res = new Array();

    for (const prop in inputObject) {
        if (typeof inputObject[prop] === "number") {
            res.push(prop);
        }
    }

    return res;
}
function incrementCounters(inputObject) {
    const reg = /counter/i;
    const fields = getNumberFields(inputObject);

    if (fields == null) {
        return;
    }

    fields.forEach((f) => {
        if (f.match(reg)) {
            inputObject[f]++;
        }
    });
}
function deleteUppercaseProperties(inputObject) {
    if (inputObject == null) {
        return;
    }

    const reg = /[a-z]/;

    for (const prop in inputObject) {
        if (!prop.match(reg)) {
            delete inputObject[prop];
        } else if (inputObject[prop] instanceof Object) {
            deleteUppercaseProperties(inputObject[prop]);
        }
    }
}
function fusion(...objs) {
    const res = {};

    for (const obj of objs) {
        for (const prop in obj) {
            if (!Object.hasOwn(res, prop)) {
                res[prop] = obj[prop];
            } else {
                if (
                    typeof res[prop] != typeof obj[prop] ||
                    typeof res[prop] === "boolean"
                ) {
                    res[prop] = obj[prop];
                } else if (obj[prop] instanceof Array) {
                    res[prop] = [...res[prop], ...obj[prop]];
                } else if (typeof prop === "object") {
                    res[prop] = fusion(res[prop], obj[prop]);
                } else {
                    res[prop] += obj[prop];
                }
            }
        }
    }

    return res;
}

module.exports = {
    fusion,
    incrementCounters,
    deleteUppercaseProperties,
    getNumberFields,
};