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

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

21 Aug 2023 12:51PM UTC coverage: 82.586% (+0.8%) from 81.799%
#1229

push

Mikolaj
chore(types): add react types package

286 of 355 branches covered (80.56%)

Branch coverage included in aggregate %.

506 of 604 relevant lines covered (83.77%)

44.62 hits per line

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

67.82
/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 = {};
1✔
8

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

18✔
19
    get = (rjvId, name, key, default_value) => {
20
        if (
21
            this.objects[rjvId] === undefined ||
1✔
22
            this.objects[rjvId][name] === undefined ||
149✔
23
            this.objects[rjvId][name][key] == undefined
214✔
24
        ) {
25
            return default_value;
26
        }
27
        return this.objects[rjvId][name][key];
142✔
28
    };
29

7✔
30
    handleAction = action => {
31
        const { rjvId, data, name } = action;
32
        switch (name) {
1✔
33
            case 'RESET':
4✔
34
                this.emit('reset-' + rjvId);
4!
35
                break;
36
            case 'VARIABLE_UPDATED':
3✔
37
                action.data.updated_src = this.updateSrc(rjvId, data);
3✔
38
                this.set(rjvId, 'action', 'variable-update', {
39
                    ...data,
1✔
40
                    type: 'variable-edited'
1✔
41
                });
42
                this.emit('variable-update-' + rjvId);
43
                break;
44
            case 'VARIABLE_REMOVED':
1✔
45
                action.data.updated_src = this.updateSrc(rjvId, data);
1✔
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) => {
68
        let {
69
            name,
1✔
70
            namespace,
71
            new_value,
72
            existing_value,
73
            variable_removed
74
        } = request;
75

76
        namespace.shift();
1✔
77

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

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

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

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

106
        return updated_src;
1✔
107
    };
108

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

125
const attributeStore = new ObjectAttributes();
126
dispatcher.register(attributeStore.handleAction.bind(attributeStore));
127
export default attributeStore;
1✔
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

© 2026 Coveralls, Inc