@lumjs/core 1.11.0 → 1.12.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/lib/enum.js +87 -28
- package/package.json +1 -1
package/lib/enum.js
CHANGED
|
@@ -9,9 +9,62 @@ const ENUM_ID = new InternalObjectId({name: '$Enum'});
|
|
|
9
9
|
*
|
|
10
10
|
* Like the built-in `Symbol`, this is called as a function, not a constructor.
|
|
11
11
|
*
|
|
12
|
-
* @param {
|
|
13
|
-
*
|
|
12
|
+
* @param {(string[]|object)} spec - May be in one of two formats:
|
|
13
|
+
*
|
|
14
|
+
* - An `Array` of strings. Each string represents the name of an Enum
|
|
15
|
+
* property name. The property value will be automatically determined
|
|
16
|
+
* based on the options. The default is a series of sequential integers
|
|
17
|
+
* starting at `0`.
|
|
18
|
+
*
|
|
19
|
+
* - A plain object used as a map of property names to underlying values.
|
|
20
|
+
* In most cases the value can be whatever you want and will be directly
|
|
21
|
+
* mapped as is. However if `opts.symbols` is `true`, and the value is a
|
|
22
|
+
* string, the string will be used as the name for the symbol rather than
|
|
23
|
+
* the property name.
|
|
24
|
+
*
|
|
25
|
+
* @param {object} [opts] Options for creating the Enum
|
|
26
|
+
*
|
|
27
|
+
* @param {bool} [opts.symbols=false] Use `Symbol` property values.
|
|
28
|
+
*
|
|
29
|
+
* @param {bool} [opts.globals=false] Use `Symbol.for()` property values.
|
|
30
|
+
*
|
|
31
|
+
* This option is only used if `opts.symbols` is `true`.
|
|
32
|
+
*
|
|
33
|
+
* @param {bool} [opts.strings=false] The property name is also the value.
|
|
34
|
+
*
|
|
35
|
+
* This is only used if `spec` is an `Array`, and `opts.symbols` is `false`.
|
|
36
|
+
*
|
|
37
|
+
* @param {bool} [opts.flags=false] Use binary flag property values.
|
|
38
|
+
*
|
|
39
|
+
* This is only used if `spec` is an `Array`, and both `opts.symbols` and
|
|
40
|
+
* `opts.strings` are `false`.
|
|
41
|
+
*
|
|
42
|
+
* This handles incrementing automatically, so: `[1,2,4,8,16,...]`
|
|
43
|
+
*
|
|
44
|
+
* @param {number} [opts.counter] The starting value when auto-incrementing.
|
|
45
|
+
*
|
|
46
|
+
* The default value is `1` if `opts.flags` is `true` or `0` otherwise.
|
|
47
|
+
*
|
|
48
|
+
* @param {bool} [opts.open=false] If `true` the Enum object won't be locked.
|
|
49
|
+
* If `false`, by default we use `Object.freeze()` to lock the object.
|
|
50
|
+
*
|
|
51
|
+
* @param {(bool|object|Array)} [opts.lock=null] If this is *not* `null`,
|
|
52
|
+
* use {@see module:@lumjs/core/obj.lock lock()} instead of `Object.freeze()`.
|
|
53
|
+
*
|
|
54
|
+
* Only used if `opts.open` is `false`. The supported values are:
|
|
55
|
+
*
|
|
56
|
+
* - `Array`: The `[cloneable, cloneOpts, useSeal]` parameters for `clone()`.
|
|
57
|
+
* - `object`: The `cloneOpts` parameter for `clone()`.
|
|
58
|
+
* - `bool`: The `cloneable` parameter for `clone()`.
|
|
59
|
+
*
|
|
60
|
+
* @param {bool} [opts.configurable=false] Enum properties are configurable?
|
|
61
|
+
*
|
|
62
|
+
* This option is ignored if `opts.open` is `false`.
|
|
63
|
+
*
|
|
64
|
+
* @param {bool} [opts.enumerable=true] Enum properties are enumerable?
|
|
65
|
+
*
|
|
14
66
|
* @returns {object} A magic Enum object.
|
|
67
|
+
* @throws {TypeError} If an invalid value was passed.
|
|
15
68
|
* @exports module:@lumjs/core/enum
|
|
16
69
|
*/
|
|
17
70
|
function Enum (spec, opts={})
|
|
@@ -52,11 +105,10 @@ function Enum (spec, opts={})
|
|
|
52
105
|
|
|
53
106
|
if (Array.isArray(spec))
|
|
54
107
|
{ // An array of strings is expected.
|
|
55
|
-
let counter = opts.counter ?? 1;
|
|
108
|
+
let counter = opts.counter ?? (opts.flags ? 1 : 0);
|
|
56
109
|
|
|
57
|
-
for (
|
|
110
|
+
for (const name of spec)
|
|
58
111
|
{
|
|
59
|
-
const name = spec[i];
|
|
60
112
|
if (typeof name !== S)
|
|
61
113
|
{
|
|
62
114
|
throw new TypeError("Non-string passed in Enum object");
|
|
@@ -65,7 +117,7 @@ function Enum (spec, opts={})
|
|
|
65
117
|
const val
|
|
66
118
|
= opts.strings
|
|
67
119
|
? name
|
|
68
|
-
:
|
|
120
|
+
: counter;
|
|
69
121
|
|
|
70
122
|
addVal(name, name, val);
|
|
71
123
|
|
|
@@ -73,6 +125,10 @@ function Enum (spec, opts={})
|
|
|
73
125
|
{ // Increment the binary flag counter.
|
|
74
126
|
counter *= 2;
|
|
75
127
|
}
|
|
128
|
+
else
|
|
129
|
+
{ // Increment a regular counter.
|
|
130
|
+
counter++;
|
|
131
|
+
}
|
|
76
132
|
}
|
|
77
133
|
}
|
|
78
134
|
else
|
|
@@ -85,30 +141,33 @@ function Enum (spec, opts={})
|
|
|
85
141
|
}
|
|
86
142
|
}
|
|
87
143
|
|
|
88
|
-
if (
|
|
89
|
-
{
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
144
|
+
if (!opts.open)
|
|
145
|
+
{
|
|
146
|
+
if (notNil(opts.lock))
|
|
147
|
+
{ // Use lock() function.
|
|
148
|
+
let lockOpts;
|
|
149
|
+
if (Array.isArray(opts.lock))
|
|
150
|
+
{ // Specified the lock parameters as an array.
|
|
151
|
+
lockOpts = opts.lock;
|
|
152
|
+
}
|
|
153
|
+
else if (isObj(opts.lock))
|
|
154
|
+
{ // An object is assumed to be the `cloneOpts` parameter.
|
|
155
|
+
lockOpts = [true, opts.lock, false];
|
|
156
|
+
}
|
|
157
|
+
else if (typeof opts.lock === B)
|
|
158
|
+
{ // Boolean is assumed to be the `cloneable` parameter.
|
|
159
|
+
lockOpts = [opts.lock, null, false];
|
|
160
|
+
}
|
|
161
|
+
else
|
|
162
|
+
{ // Anything else is invalid.
|
|
163
|
+
throw new TypeError('opts.lock was not a valid value');
|
|
164
|
+
}
|
|
165
|
+
return lock(anEnum, ...lockOpts);
|
|
102
166
|
}
|
|
103
|
-
else
|
|
104
|
-
{ //
|
|
105
|
-
|
|
167
|
+
else
|
|
168
|
+
{ // Use Object.freeze()
|
|
169
|
+
return Object.freeze(anEnum);
|
|
106
170
|
}
|
|
107
|
-
return lock(anEnum, ...lockOpts);
|
|
108
|
-
}
|
|
109
|
-
else if (!opts.open)
|
|
110
|
-
{ // Use Object.freeze()
|
|
111
|
-
return Object.freeze(anEnum);
|
|
112
171
|
}
|
|
113
172
|
|
|
114
173
|
return anEnum;
|