• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mac-s-g / react-json-view / #1260

30 Jun 2023 08:12PM CUT coverage: 83.298%. Remained the same
#1260

push

web-flow
deprecation notice

284 of 349 branches covered (81.38%)

Branch coverage included in aggregate %.

504 of 597 relevant lines covered (84.42%)

90.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.44
/src/js/stores/ObjectAttributes.js
1
import { EventEmitter } from 'events';
2
import dispatcher from './../helpers/dispatcher';
3
import { toType } from './../helpers/util';
4

5
//store persistent display attributes for objects and arrays
6
class ObjectAttributes extends EventEmitter {
7
    objects = {};
2✔
8

9
    set = (rjvId, name, key, value) => {
2✔
10
        if (this.objects[rjvId] === undefined) {
36✔
11
            this.objects[rjvId] = {};
12✔
12
        }
13
        if (this.objects[rjvId][name] === undefined) {
36✔
14
            this.objects[rjvId][name] = {};
18✔
15
        }
16
        this.objects[rjvId][name][key] = value;
36✔
17
    };
18

19
    get = (rjvId, name, key, default_value) => {
2✔
20
        if (
298✔
21
            this.objects[rjvId] === undefined ||
428✔
22
            this.objects[rjvId][name] === undefined ||
23
            this.objects[rjvId][name][key] == undefined
24
        ) {
25
            return default_value;
284✔
26
        }
27
        return this.objects[rjvId][name][key];
14✔
28
    };
29

30
    handleAction = action => {
2✔
31
        const { rjvId, data, name } = action;
8✔
32
        switch (name) {
8!
33
            case 'RESET':
34
                this.emit('reset-' + rjvId);
6✔
35
                break;
6✔
36
            case 'VARIABLE_UPDATED':
37
                action.data.updated_src = this.updateSrc(rjvId, data);
2✔
38
                this.set(rjvId, 'action', 'variable-update', {
2✔
39
                    ...data,
40
                    type: 'variable-edited'
41
                });
42
                this.emit('variable-update-' + rjvId);
2✔
43
                break;
2✔
44
            case 'VARIABLE_REMOVED':
45
                action.data.updated_src = this.updateSrc(rjvId, data);
×
46
                this.set(rjvId, 'action', 'variable-update', {
×
47
                    ...data,
48
                    type: 'variable-removed'
49
                });
50
                this.emit('variable-update-' + rjvId);
×
51
                break;
×
52
            case 'VARIABLE_ADDED':
53
                action.data.updated_src = this.updateSrc(rjvId, data);
×
54
                this.set(rjvId, 'action', 'variable-update', {
×
55
                    ...data,
56
                    type: 'variable-added'
57
                });
58
                this.emit('variable-update-' + rjvId);
×
59
                break;
×
60
            case 'ADD_VARIABLE_KEY_REQUEST':
61
                this.set(rjvId, 'action', 'new-key-request', data);
×
62
                this.emit('add-key-request-' + rjvId);
×
63
                break;
×
64
        }
65
    };
66

67
    updateSrc = (rjvId, request) => {
2✔
68
        let {
69
            name,
70
            namespace,
71
            new_value,
72
            existing_value,
73
            variable_removed
74
        } = request;
2✔
75

76
        namespace.shift();
2✔
77

78
        //deepy copy src
79
        let src = this.get(rjvId, 'global', 'src');
2✔
80
        //deep copy of src variable
81
        let updated_src = this.deepCopy(src, [...namespace]);
2✔
82

83
        //point at current index
84
        let walk = updated_src;
2✔
85
        for (const idx of namespace) {
2✔
86
            walk = walk[idx];
×
87
        }
88

89
        if (variable_removed) {
2!
90
            if (toType(walk) == 'array') {
×
91
                walk.splice(name, 1);
×
92
            } else {
93
                delete walk[name];
×
94
            }
95
        } else {
96
            //update copied variable at specified namespace
97
            if (name !== null) {
2!
98
                walk[name] = new_value;
2✔
99
            } else {
100
                updated_src = new_value;
×
101
            }
102
        }
103

104
        this.set(rjvId, 'global', 'src', updated_src);
2✔
105

106
        return updated_src;
2✔
107
    };
108

109
    deepCopy = (src, copy_namespace) => {
2✔
110
        const type = toType(src);
2✔
111
        let result;
112
        let idx = copy_namespace.shift();
2✔
113
        if (type == 'array') {
2!
114
            result = [...src];
×
115
        } else if (type == 'object') {
2!
116
            result = { ...src };
2✔
117
        }
118
        if (idx !== undefined) {
2!
119
            result[idx] = this.deepCopy(src[idx], copy_namespace);
×
120
        }
121
        return result;
2✔
122
    };
123
}
124

125
const attributeStore = new ObjectAttributes();
2✔
126
dispatcher.register(attributeStore.handleAction.bind(attributeStore));
2✔
127
export default attributeStore;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc