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

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

09 Mar 2021 03:27AM CUT coverage: 83.298%. Remained the same
#1276

push

web-flow
Merge pull request #342 from mac-s-g/bump-patch

bump patch version

284 of 349 branches covered (81.38%)

Branch coverage included in aggregate %.

504 of 597 relevant lines covered (84.42%)

45.1 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 = {};
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

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

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

76
        namespace.shift();
1✔
77

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

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

89
        if (variable_removed) {
1!
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) {
1!
98
                walk[name] = new_value;
1✔
99
            } else {
100
                updated_src = new_value;
×
101
            }
102
        }
103

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

106
        return updated_src;
1✔
107
    };
108

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

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