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

node-opcua / node-opcua / 22636309940

03 Mar 2026 06:03PM UTC coverage: 92.756% (+1.9%) from 90.81%
22636309940

push

github

erossignon
test: disable ENOENT on overlapping trustCertificate invocations in test environments with concurrency tests

18684 of 22141 branches covered (84.39%)

23 of 37 new or added lines in 1 file covered. (62.16%)

5775 existing lines in 228 files now uncovered.

160668 of 173216 relevant lines covered (92.76%)

875869.8 hits per line

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

96.86
/packages/node-opcua-enum/source/enum.ts
1
/**
2✔
2
 * @module node-opcua-enum
2✔
3
 *
2✔
4
 */
2✔
5
// tslint:disable:no-bitwise
2✔
6
// tslint:disable:max-classes-per-file
2✔
7

2✔
8
/**
2✔
9
 * Represents an Item of an Enum.
2✔
10
 */
2✔
11
export class EnumItem {
176,760✔
12
    public key: string;
176,760✔
13
    public value: number;
176,760✔
14

176,760✔
15
    /**
176,760✔
16
     *
176,760✔
17
     * @param key the enum key
176,760✔
18
     * @param value the enum value
176,760✔
19
     */
176,760✔
20
    public constructor(key: string, value: number) {
176,760✔
21
        this.key = key;
176,760✔
22
        this.value = value;
176,760✔
23
    }
176,760✔
24

176,760✔
25
    /**
176,760✔
26
     * Checks if the EnumItem is the same as the passing object.
176,760✔
27
     * 
176,760✔
28
     * @param  {EnumItem | String | Number} item The object to check with.
176,760✔
29
     * @return {Boolean}                          The check result.
176,760✔
30
     */
176,760✔
31
    public is(item: EnumItem | string | number): boolean {
176,760✔
32
        if (item instanceof EnumItem) {
12✔
33
            return this.value === item.value;
4✔
34
        } else if (typeof item === "string") {
12✔
35
            return this.key === item;
4✔
36
        } else {
4✔
37
            return this.value === item;
4✔
38
        }
4✔
39
    }
12✔
40

176,760✔
41
    /**
176,760✔
42
     * Checks if the flagged EnumItem has the passing object.
176,760✔
43
     * 
176,760✔
44
     * @param  {EnumItem | String |Number} value The object to check with.
176,760✔
45
     * @return {Boolean}                            The check result.
176,760✔
46
     */
176,760✔
47
    public has(value: string | number | EnumItem): boolean {
176,760✔
48
        if (value instanceof EnumItem) {
18✔
49
            return (value.value & this.value) !== 0;
6✔
50
        } else if (typeof value === "string") {
18✔
51
            return this.key.indexOf(value) >= 0;
6✔
52
        } else {
6✔
53
            return (value & this.value) !== 0;
6✔
54
        }
6✔
55
    }
18✔
56

176,760✔
57
    /**
176,760✔
58
     * Returns String representation of this EnumItem.
176,760✔
59
     * 
176,760✔
60
     * @return {String} String representation of this EnumItem.
176,760✔
61
     */
176,760✔
62
    public toString(): string {
176,760✔
63
        return this.key;
4✔
64
    }
4✔
65

176,760✔
66
    /**
176,760✔
67
     * Returns JSON object representation of this EnumItem.
176,760✔
68
     * @return {String} JSON object representation of this EnumItem.
176,760✔
69
     */
176,760✔
70

176,760✔
71
    public toJSON(): any {
176,760✔
72
        return this.key;
4✔
73
    }
4✔
74

176,760✔
75
    /**
176,760✔
76
     * Returns the value to compare with.
176,760✔
77
     * @return {String} The value to compare with.
176,760✔
78
     */
176,760✔
79

176,760✔
80
    public valueOf(): number {
176,760✔
81
        return this.value;
4✔
82
    }
4✔
83
}
176,760✔
84

2✔
85
function powerOfTwo(n: number): boolean {
81,262✔
86
    return n && !(n & (n - 1)) ? true : false;
81,262✔
87
}
81,262✔
88
// check if enum is flaggable
2✔
89
function checkIsFlaggable(enums: EnumItem[]): boolean {
30,330✔
90
    for (const e of enums) {
30,330✔
91
        const value = +e.value;
131,734✔
92
        if (isNaN(value)) {
131,734!
93
            continue; // skipping none number value
×
UNCOV
94
        }
×
95
        if (value !== 0 && value !== 1 && !powerOfTwo(value)) {
131,734✔
96
            return false;
19,946✔
97
        }
19,946✔
98
    }
131,734✔
99
    return true;
10,384✔
100
}
10,384✔
101

2✔
102
export interface _TypescriptEnum {
2✔
103
    [key: string | number]: number | string;
2✔
104
}
2✔
105

2✔
106
export function adaptTypescriptEnum(map: _TypescriptEnum | string[]) {
1,534✔
107
    if (Array.isArray(map)) {
1,534✔
108
        let mm: _TypescriptEnum | null = null;
2✔
109
        // create map as flaggable enum
2✔
110
        mm = {};
2✔
111
        for (let i = 0; i < map.length; i++) {
2✔
112
            mm[map[i]] = 1 << i;
6✔
113
        }
6✔
114
        return mm;
2✔
115
    }
2✔
116
    return map as _TypescriptEnum;
1,532✔
117
}
1,532✔
118

2✔
119

2✔
120
/**
2✔
121
 * @class Enum
2✔
122
 * @constructor
2✔
123
 * Represents an Enum with enum items.
2✔
124
 * @param {Array || Object}  map     This are the enum items.
2✔
125
 */
2✔
126
export class Enum {
30,332✔
127
    private readonly enumItems: EnumItem[];
30,332✔
128
    private readonly _isFlaggable: boolean;
30,332✔
129

30,332✔
130
    constructor(map: _TypescriptEnum | string[]) {
30,332✔
131
        this.enumItems = [];
30,332✔
132
        let mm: _TypescriptEnum | null = null;
30,332✔
133
        let isFlaggable = null;
30,332✔
134
        if (Array.isArray(map)) {
30,332✔
135
            mm = adaptTypescriptEnum(map);
2✔
136
            isFlaggable = true;
2✔
137
        } else {
30,332✔
138
            mm = map;
30,330✔
139
        }
30,330✔
140

30,332✔
141
        for (const  [key, val] of Object.entries(mm)) {
30,332✔
142
            if (typeof val !== "number") {
147,466✔
143
                continue;
11,014✔
144
            }
11,014✔
145
            const kv = new EnumItem(key, val);
136,452✔
146
            const pThis = this as any;
136,452✔
147
            pThis[key] = kv;
136,452✔
148
            pThis[val] = kv;
136,452✔
149

136,452✔
150
            this.enumItems.push(kv);
136,452✔
151
        }
136,452✔
152

30,332✔
153
        if (!isFlaggable) {
30,332✔
154
            isFlaggable = checkIsFlaggable(this.enumItems);
30,330✔
155
        }
30,330✔
156
        this._isFlaggable = isFlaggable;
30,332✔
157
    }
30,332✔
158

30,332✔
159
    public get isFlaggable(): boolean {
30,332✔
160
        return this._isFlaggable;
300✔
161
    }
300✔
162
    /**
30,332✔
163
     * Returns the appropriate EnumItem.
30,332✔
164

30,332✔
165
     * @param  key The object to get with.
30,332✔
166
     * @return the get result.
30,332✔
167
     */
30,332✔
168
    public get(key: EnumItem | string | number): EnumItem | null {
30,332✔
169
        const pThis = this as any;
4,632,237✔
170
        if (key instanceof EnumItem) {
4,632,237✔
171
            if (!pThis[key.key]) {
28,786!
172
                throw new Error("Invalid key");
×
UNCOV
173
            }
×
174
            return key;
28,786✔
175
        }
28,786✔
176

4,603,451✔
177
        if (key === null || key === undefined) {
4,632,237!
178
            return null;
×
UNCOV
179
        }
×
180
        const prop = pThis[key];
4,603,451✔
181
        if (prop) {
4,632,237✔
182
            return prop;
4,534,339✔
183
        } else if (this._isFlaggable) {
4,632,237✔
184
            if (typeof key === "string") {
50,386✔
185
                return this._getByString(key);
50,356✔
186
            } else if (typeof key === "number") {
50,386✔
187
                return this._getByNum(key);
30✔
188
            }
30✔
189
        }
50,386✔
190
        return null;
18,726✔
191
    }
18,726✔
192

30,332✔
193
    public getDefaultValue(): EnumItem {
30,332✔
194
        return this.enumItems[0];
1,534✔
195
    }
1,534✔
196

30,332✔
197
    public toString(): string {
30,332✔
198
        return this.enumItems.join(" , ");
×
UNCOV
199
    }
×
200

30,332✔
201
    private _getByString(key: string): EnumItem | null {
30,332✔
202
        const pThis = this as any;
50,356✔
203
        const parts = key.split(" | ");
50,356✔
204

50,356✔
205
        let val = 0;
50,356✔
206
        for (const part of parts) {
50,356✔
207
            const item = pThis[part];
120,852✔
208
            if (undefined === item) {
120,852✔
209
                return null;
10,070✔
210
            }
10,070✔
211
            val |= item.value;
110,782✔
212
        }
110,782✔
213
        const kv = new EnumItem(key, val);
40,286✔
214

40,286✔
215
        // add in cache for later
40,286✔
216
        let prop = pThis[val];
40,286✔
217
        if (prop === undefined) {
50,356✔
218
            pThis[val] = kv;
20,146✔
219
        }
20,146✔
220
        prop = pThis[key];
40,286✔
221
        if (prop === undefined) {
40,286✔
222
            pThis[key] = kv;
40,286✔
223
        }
40,286✔
224
        return kv;
40,286✔
225
    }
40,286✔
226

30,332✔
227
    private _getByNum(key: number): EnumItem | null {
30,332✔
228
        if (key === 0) {
30✔
229
            return null;
4✔
230
        }
4✔
231
        const pThis = this as any;
26✔
232

26✔
233
        let name;
26✔
234
        let c = 1;
26✔
235
        for (let i = 0; c < key; i++) {
30✔
236
            if ((c & key) === c) {
212✔
237
                const item = pThis[c];
146✔
238
                if (undefined === item) {
146✔
239
                    return null;
4✔
240
                }
4✔
241
                if (name) {
146✔
242
                    name = name + " | " + item.key;
116✔
243
                } else {
146✔
244
                    name = item.key;
26✔
245
                }
26✔
246
            }
146✔
247
            c *= 2;
208✔
248
        }
208✔
249
        const kv = new EnumItem(name, key);
22✔
250
        // add in cache for later
22✔
251
        pThis[name] = kv;
22✔
252
        pThis[key] = kv;
22✔
253
        return kv;
22✔
254
    }
22✔
255
}
30,332✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc