node-opcua-enum 2.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.mocharc.yml ADDED
@@ -0,0 +1,8 @@
1
+ require:
2
+ - ts-node/register
3
+ extension:
4
+ - ts
5
+ - js
6
+ recursive: false
7
+ timeout: 30000
8
+
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014-2021 Etienne Rossignon
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/dist/enum.d.ts ADDED
@@ -0,0 +1,74 @@
1
+ /**
2
+ * @module node-opcua-enum
3
+ *
4
+ */
5
+ /**
6
+ * Represents an Item of an Enum.
7
+ *
8
+ * @class Enum
9
+ */
10
+ export declare class EnumItem {
11
+ key: string;
12
+ value: number;
13
+ /**
14
+ *
15
+ * @param key the enum key
16
+ * @param value the enum value
17
+ */
18
+ constructor(key: string, value: number);
19
+ /**
20
+ * Checks if the EnumItem is the same as the passing object.
21
+ * @method is
22
+ * @param {EnumItem | String | Number} item The object to check with.
23
+ * @return {Boolean} The check result.
24
+ */
25
+ is(item: any): boolean;
26
+ /**
27
+ * Checks if the flagged EnumItem has the passing object.
28
+ * @method has
29
+ * @param {EnumItem | String |Number} value The object to check with.
30
+ * @return {Boolean} The check result.
31
+ */
32
+ has(value: string | number | EnumItem): boolean;
33
+ /**
34
+ * Returns String representation of this EnumItem.
35
+ * @method toString
36
+ * @return {String} String representation of this EnumItem.
37
+ */
38
+ toString(): string;
39
+ /**
40
+ * Returns JSON object representation of this EnumItem.
41
+ * @method toJSON
42
+ * @return {String} JSON object representation of this EnumItem.
43
+ */
44
+ toJSON(): any;
45
+ /**
46
+ * Returns the value to compare with.
47
+ * @method valueOf
48
+ * @return {String} The value to compare with.
49
+ */
50
+ valueOf(): number;
51
+ }
52
+ /**
53
+ * @class Enum
54
+ * @constructor
55
+ * Represents an Enum with enum items.
56
+ * @param {Array || Object} map This are the enum items.
57
+ */
58
+ export declare class Enum {
59
+ private readonly enumItems;
60
+ private readonly _isFlaggable;
61
+ constructor(map: any);
62
+ get isFlaggable(): boolean;
63
+ /**
64
+ * Returns the appropriate EnumItem.
65
+ * @method get
66
+ * @param key The object to get with.
67
+ * @return the get result.
68
+ */
69
+ get(key: EnumItem | string | number): (EnumItem | null);
70
+ getDefaultValue(): EnumItem;
71
+ toString(): string;
72
+ private _getByString;
73
+ private _getByNum;
74
+ }
package/dist/enum.js ADDED
@@ -0,0 +1,232 @@
1
+ "use strict";
2
+ /**
3
+ * @module node-opcua-enum
4
+ *
5
+ */
6
+ // tslint:disable:no-bitwise
7
+ // tslint:disable:max-classes-per-file
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.Enum = exports.EnumItem = void 0;
10
+ /**
11
+ * Represents an Item of an Enum.
12
+ *
13
+ * @class Enum
14
+ */
15
+ class EnumItem {
16
+ /**
17
+ *
18
+ * @param key the enum key
19
+ * @param value the enum value
20
+ */
21
+ constructor(key, value) {
22
+ this.key = key;
23
+ this.value = value;
24
+ }
25
+ /**
26
+ * Checks if the EnumItem is the same as the passing object.
27
+ * @method is
28
+ * @param {EnumItem | String | Number} item The object to check with.
29
+ * @return {Boolean} The check result.
30
+ */
31
+ is(item) {
32
+ if (item instanceof EnumItem) {
33
+ return this.value === item.value;
34
+ }
35
+ else if (typeof item === "string") {
36
+ return this.key === item;
37
+ }
38
+ else {
39
+ return this.value === item;
40
+ }
41
+ }
42
+ /**
43
+ * Checks if the flagged EnumItem has the passing object.
44
+ * @method has
45
+ * @param {EnumItem | String |Number} value The object to check with.
46
+ * @return {Boolean} The check result.
47
+ */
48
+ has(value) {
49
+ if (value instanceof EnumItem) {
50
+ return (value.value & this.value) !== 0;
51
+ }
52
+ else if (typeof value === "string") {
53
+ return this.key.indexOf(value) >= 0;
54
+ }
55
+ else {
56
+ return (value & this.value) !== 0;
57
+ }
58
+ }
59
+ /**
60
+ * Returns String representation of this EnumItem.
61
+ * @method toString
62
+ * @return {String} String representation of this EnumItem.
63
+ */
64
+ toString() {
65
+ return this.key;
66
+ }
67
+ /**
68
+ * Returns JSON object representation of this EnumItem.
69
+ * @method toJSON
70
+ * @return {String} JSON object representation of this EnumItem.
71
+ */
72
+ toJSON() {
73
+ return this.key;
74
+ }
75
+ /**
76
+ * Returns the value to compare with.
77
+ * @method valueOf
78
+ * @return {String} The value to compare with.
79
+ */
80
+ valueOf() {
81
+ return this.value;
82
+ }
83
+ }
84
+ exports.EnumItem = EnumItem;
85
+ function powerOfTwo(n) {
86
+ return n && (!(n & (n - 1))) ? true : false;
87
+ }
88
+ // check if enum is flaggable
89
+ function checkIsFlaggable(enums) {
90
+ for (const e of enums) {
91
+ const value = +e.value;
92
+ if (isNaN(value)) {
93
+ continue; // skipping none number value
94
+ }
95
+ if ((value !== 0 && value !== 1) && !powerOfTwo(value)) {
96
+ return false;
97
+ }
98
+ }
99
+ return true;
100
+ }
101
+ /**
102
+ * @class Enum
103
+ * @constructor
104
+ * Represents an Enum with enum items.
105
+ * @param {Array || Object} map This are the enum items.
106
+ */
107
+ class Enum {
108
+ constructor(map) {
109
+ this.enumItems = [];
110
+ let mm = null;
111
+ let isFlaggable = null;
112
+ if (Array.isArray(map)) {
113
+ // create map as flaggable enum
114
+ mm = {};
115
+ for (let i = 0; i < map.length; i++) {
116
+ mm[map[i]] = 1 << i;
117
+ }
118
+ isFlaggable = true;
119
+ }
120
+ else {
121
+ mm = map;
122
+ }
123
+ for (const key of Object.keys(mm)) {
124
+ const val = mm[key];
125
+ if (undefined === val) {
126
+ continue;
127
+ }
128
+ const kv = new EnumItem(key, val);
129
+ const pThis = this;
130
+ pThis[key] = kv;
131
+ pThis[val] = kv;
132
+ this.enumItems.push(kv);
133
+ }
134
+ if (!isFlaggable) {
135
+ isFlaggable = checkIsFlaggable(this.enumItems);
136
+ }
137
+ this._isFlaggable = isFlaggable;
138
+ }
139
+ get isFlaggable() {
140
+ return this._isFlaggable;
141
+ }
142
+ /**
143
+ * Returns the appropriate EnumItem.
144
+ * @method get
145
+ * @param key The object to get with.
146
+ * @return the get result.
147
+ */
148
+ get(key) {
149
+ const pThis = this;
150
+ if (key instanceof EnumItem) {
151
+ if (!pThis[key.key]) {
152
+ throw new Error("Invalid key");
153
+ }
154
+ return key;
155
+ }
156
+ if (key === null || key === undefined) {
157
+ return null;
158
+ }
159
+ const prop = pThis[key];
160
+ if (prop) {
161
+ return prop;
162
+ }
163
+ else if (this._isFlaggable) {
164
+ if (typeof key === "string") {
165
+ return this._getByString(key);
166
+ }
167
+ else if (typeof key === "number") {
168
+ return this._getByNum(key);
169
+ }
170
+ }
171
+ return null;
172
+ }
173
+ getDefaultValue() {
174
+ return this.enumItems[0];
175
+ }
176
+ toString() {
177
+ return this.enumItems.join(" , ");
178
+ }
179
+ _getByString(key) {
180
+ const pThis = this;
181
+ const parts = key.split(" | ");
182
+ let val = 0;
183
+ for (const part of parts) {
184
+ const item = pThis[part];
185
+ if (undefined === item) {
186
+ return null;
187
+ }
188
+ val |= item.value;
189
+ }
190
+ const kv = new EnumItem(key, val);
191
+ // add in cache for later
192
+ let prop = pThis[val];
193
+ if (prop === undefined) {
194
+ pThis[val] = kv;
195
+ }
196
+ prop = pThis[key];
197
+ if (prop === undefined) {
198
+ pThis[key] = kv;
199
+ }
200
+ return kv;
201
+ }
202
+ _getByNum(key) {
203
+ if (key === 0) {
204
+ return null;
205
+ }
206
+ const pThis = this;
207
+ let name;
208
+ let c = 1;
209
+ for (let i = 0; c < key; i++) {
210
+ if ((c & key) === c) {
211
+ const item = pThis[c];
212
+ if (undefined === item) {
213
+ return null;
214
+ }
215
+ if (name) {
216
+ name = name + " | " + item.key;
217
+ }
218
+ else {
219
+ name = item.key;
220
+ }
221
+ }
222
+ c *= 2;
223
+ }
224
+ const kv = new EnumItem(name, key);
225
+ // add in cache for later
226
+ pThis[name] = kv;
227
+ pThis[key] = kv;
228
+ return kv;
229
+ }
230
+ }
231
+ exports.Enum = Enum;
232
+ //# sourceMappingURL=enum.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enum.js","sourceRoot":"","sources":["../source/enum.ts"],"names":[],"mappings":";AAAA;;;GAGG;AACH,4BAA4B;AAC5B,sCAAsC;;;AAEtC;;;;GAIG;AACH,MAAa,QAAQ;IAIjB;;;;OAIG;IACH,YAAmB,GAAW,EAAE,KAAa;QACzC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACvB,CAAC;IAED;;;;;OAKG;IACI,EAAE,CAAC,IAAS;QACf,IAAI,IAAI,YAAY,QAAQ,EAAE;YAC1B,OAAO,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,CAAC;SACpC;aAAM,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;YACjC,OAAO,IAAI,CAAC,GAAG,KAAK,IAAI,CAAC;SAC5B;aAAM;YACH,OAAO,IAAI,CAAC,KAAK,KA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KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;QAChB,OAAO,EAAE,CAAC;IACd,CAAC;CAGJ;AA9ID,oBA8IC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @module node-opcua-enum
3
+ *
4
+ */
5
+ export * from "./enum";
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ /**
14
+ * @module node-opcua-enum
15
+ *
16
+ */
17
+ __exportStar(require("./enum"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA;;;GAGG;AACH,yCAAuB"}
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "node-opcua-enum",
3
+ "version": "2.51.0",
4
+ "description": "pure nodejs OPCUA SDK - module -enum",
5
+ "scripts": {
6
+ "build": "tsc -b",
7
+ "lint": "tslint source/**/*.ts",
8
+ "clean": "node -e \"require('rimraf').sync('dist');\"",
9
+ "test": "mocha"
10
+ },
11
+ "main": "./dist/index.js",
12
+ "@types": "./dist/index.d.ts",
13
+ "dependencies": {
14
+ "node-opcua-assert": "2.51.0"
15
+ },
16
+ "devDependencies": {
17
+ "enum": "3.0.4",
18
+ "node-opcua-benchmarker": "2.51.0",
19
+ "should": "^13.2.3"
20
+ },
21
+ "author": "Etienne Rossignon",
22
+ "license": "MIT",
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git://github.com/node-opcua/node-opcua.git"
26
+ },
27
+ "keywords": [
28
+ "OPCUA",
29
+ "opcua",
30
+ "m2m",
31
+ "iot",
32
+ "opc ua",
33
+ "internet of things"
34
+ ],
35
+ "homepage": "http://node-opcua.github.io/",
36
+ "gitHead": "75feb111daf7ec65fa0111e4fa5beb8987fd4945"
37
+ }
package/source/enum.ts ADDED
@@ -0,0 +1,255 @@
1
+ /**
2
+ * @module node-opcua-enum
3
+ *
4
+ */
5
+ // tslint:disable:no-bitwise
6
+ // tslint:disable:max-classes-per-file
7
+
8
+ /**
9
+ * Represents an Item of an Enum.
10
+ *
11
+ * @class Enum
12
+ */
13
+ export class EnumItem {
14
+ public key: string;
15
+ public value: number;
16
+
17
+ /**
18
+ *
19
+ * @param key the enum key
20
+ * @param value the enum value
21
+ */
22
+ public constructor(key: string, value: number) {
23
+ this.key = key;
24
+ this.value = value;
25
+ }
26
+
27
+ /**
28
+ * Checks if the EnumItem is the same as the passing object.
29
+ * @method is
30
+ * @param {EnumItem | String | Number} item The object to check with.
31
+ * @return {Boolean} The check result.
32
+ */
33
+ public is(item: any): boolean {
34
+ if (item instanceof EnumItem) {
35
+ return this.value === item.value;
36
+ } else if (typeof item === "string") {
37
+ return this.key === item;
38
+ } else {
39
+ return this.value === item;
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Checks if the flagged EnumItem has the passing object.
45
+ * @method has
46
+ * @param {EnumItem | String |Number} value The object to check with.
47
+ * @return {Boolean} The check result.
48
+ */
49
+ public has(value: string | number | EnumItem): boolean {
50
+ if (value instanceof EnumItem) {
51
+ return (value.value & this.value) !== 0;
52
+ } else if (typeof value === "string") {
53
+ return this.key.indexOf(value) >= 0;
54
+ } else {
55
+ return (value & this.value) !== 0;
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Returns String representation of this EnumItem.
61
+ * @method toString
62
+ * @return {String} String representation of this EnumItem.
63
+ */
64
+ public toString(): string {
65
+ return this.key;
66
+ }
67
+
68
+ /**
69
+ * Returns JSON object representation of this EnumItem.
70
+ * @method toJSON
71
+ * @return {String} JSON object representation of this EnumItem.
72
+ */
73
+
74
+ public toJSON(): any {
75
+ return this.key;
76
+ }
77
+
78
+ /**
79
+ * Returns the value to compare with.
80
+ * @method valueOf
81
+ * @return {String} The value to compare with.
82
+ */
83
+
84
+ public valueOf(): number {
85
+ return this.value;
86
+ }
87
+ }
88
+
89
+ function powerOfTwo(n: number): boolean
90
+ {
91
+ return n && (!(n & (n-1))) ? true: false;
92
+ }
93
+ // check if enum is flaggable
94
+ function checkIsFlaggable(enums: EnumItem[]): boolean {
95
+ for (const e of enums) {
96
+ const value = +e.value;
97
+ if (isNaN(value)) {
98
+ continue; // skipping none number value
99
+ }
100
+ if ((value !== 0 && value !==1) && !powerOfTwo(value)) {
101
+ return false;
102
+ }
103
+ }
104
+ return true;
105
+ }
106
+
107
+ /**
108
+ * @class Enum
109
+ * @constructor
110
+ * Represents an Enum with enum items.
111
+ * @param {Array || Object} map This are the enum items.
112
+ */
113
+ export class Enum {
114
+
115
+ private readonly enumItems: EnumItem[];
116
+ private readonly _isFlaggable: boolean;
117
+
118
+ constructor(map: any) {
119
+
120
+ this.enumItems = [];
121
+ let mm: any = null;
122
+ let isFlaggable = null;
123
+ if (Array.isArray(map)) {
124
+ // create map as flaggable enum
125
+ mm = {};
126
+ for (let i = 0; i < map.length; i++) {
127
+ mm[map[i]] = 1 << i;
128
+ }
129
+ isFlaggable = true;
130
+ } else {
131
+ mm = map;
132
+ }
133
+
134
+ for (const key of Object.keys(mm)) {
135
+ const val = mm[key];
136
+ if (undefined === val) {
137
+ continue;
138
+ }
139
+ const kv = new EnumItem(key, val);
140
+
141
+ const pThis = this as any;
142
+ pThis[key] = kv;
143
+ pThis[val] = kv;
144
+
145
+ this.enumItems.push(kv);
146
+ }
147
+
148
+ if (!isFlaggable) {
149
+ isFlaggable = checkIsFlaggable(this.enumItems);
150
+ }
151
+ this._isFlaggable = isFlaggable;
152
+ }
153
+
154
+ public get isFlaggable() {
155
+ return this._isFlaggable;
156
+ }
157
+ /**
158
+ * Returns the appropriate EnumItem.
159
+ * @method get
160
+ * @param key The object to get with.
161
+ * @return the get result.
162
+ */
163
+ public get(key: EnumItem | string | number): (EnumItem | null) {
164
+
165
+ const pThis = this as any;
166
+ if (key instanceof EnumItem) {
167
+ if (!pThis[key.key]) {
168
+ throw new Error("Invalid key");
169
+ }
170
+ return key;
171
+ }
172
+
173
+ if (key === null || key === undefined) {
174
+ return null;
175
+ }
176
+ const prop = pThis[key];
177
+ if (prop) {
178
+ return prop;
179
+ } else if (this._isFlaggable) {
180
+ if (typeof key === "string") {
181
+ return this._getByString(key);
182
+ } else if (typeof key === "number") {
183
+ return this._getByNum(key);
184
+ }
185
+ }
186
+ return null;
187
+ }
188
+
189
+ public getDefaultValue(): EnumItem {
190
+ return this.enumItems[0];
191
+ }
192
+
193
+ public toString(): string {
194
+ return this.enumItems.join(" , ");
195
+ }
196
+
197
+ private _getByString(key: string): (EnumItem | null) {
198
+
199
+ const pThis = this as any;
200
+ const parts = key.split(" | ");
201
+
202
+ let val = 0;
203
+ for (const part of parts) {
204
+ const item = pThis[part];
205
+ if (undefined === item) {
206
+ return null;
207
+ }
208
+ val |= item.value;
209
+ }
210
+ const kv = new EnumItem(key, val);
211
+
212
+ // add in cache for later
213
+ let prop = pThis[val];
214
+ if (prop === undefined) {
215
+ pThis[val] = kv;
216
+ }
217
+ prop = pThis[key];
218
+ if (prop === undefined) {
219
+ pThis[key] = kv;
220
+ }
221
+ return kv;
222
+ }
223
+
224
+ private _getByNum(key: number): (EnumItem | null) {
225
+
226
+ if (key === 0) {
227
+ return null;
228
+ }
229
+ const pThis = this as any;
230
+
231
+ let name;
232
+ let c = 1;
233
+ for (let i = 0; c < key; i++) {
234
+ if ((c & key) === c) {
235
+ const item = pThis[c];
236
+ if (undefined === item) {
237
+ return null;
238
+ }
239
+ if (name) {
240
+ name = name + " | " + item.key;
241
+ } else {
242
+ name = item.key;
243
+ }
244
+ }
245
+ c *= 2;
246
+ }
247
+ const kv = new EnumItem(name, key);
248
+ // add in cache for later
249
+ pThis[name] = kv;
250
+ pThis[key] = kv;
251
+ return kv;
252
+ }
253
+
254
+
255
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @module node-opcua-enum
3
+ *
4
+ */
5
+ export * from "./enum";