@khgtrn/lib 1.0.1 → 1.0.2
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/README.md +11 -1
- package/dist/cjs/base-enum.d.ts +5 -5
- package/dist/cjs/base-enum.js +4 -4
- package/dist/esm/base-enum.d.ts +5 -5
- package/dist/esm/base-enum.js +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,7 +43,17 @@ Role.Admin.name(); // 'Admin'
|
|
|
43
43
|
new Role(2, 'x'); // compile error — constructor is protected
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
The constructor also accepts an optional
|
|
46
|
+
The constructor also accepts an optional third parameter, `opt?: O`, for storing arbitrary extra data per constant. Pass the second type argument on `BaseEnum<T, O>` to get accurate type hints on `opt`:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
class Role extends BaseEnum<number, { icon: string }> {
|
|
50
|
+
static readonly Admin = new Role(1, 'Administrator', { icon: 'shield' });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
Role.Admin.opt?.icon; // typed as `string | undefined`
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Without an explicit `O`, it defaults to `any`.
|
|
47
57
|
|
|
48
58
|
## Utility functions (`func.ts`)
|
|
49
59
|
|
package/dist/cjs/base-enum.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Base class for simulating Java-style enums in TypeScript.
|
|
3
3
|
*
|
|
4
4
|
* Subclasses only need to `extends BaseEnum<...>` and declare constants as
|
|
5
|
-
* `static readonly Xxx = new SubClass(value, label,
|
|
5
|
+
* `static readonly Xxx = new SubClass(value, label, opt?)`, without
|
|
6
6
|
* redeclaring a constructor. Because `BaseEnum`'s constructor is `protected`,
|
|
7
7
|
* subclasses inherit it while keeping the same protection — instances can't
|
|
8
8
|
* be `new`-ed from outside the class, which preserves enum singleton/identity
|
|
@@ -10,10 +10,10 @@
|
|
|
10
10
|
*
|
|
11
11
|
* @typeParam T - Type of the `value` field (defaults to `number`).
|
|
12
12
|
*/
|
|
13
|
-
export declare abstract class BaseEnum<T = number> {
|
|
13
|
+
export declare abstract class BaseEnum<T = number, O = any> {
|
|
14
14
|
readonly value: T;
|
|
15
15
|
readonly label: string;
|
|
16
|
-
readonly
|
|
16
|
+
readonly opt?: O | undefined;
|
|
17
17
|
/**
|
|
18
18
|
* Registry of every instance created, keyed per subclass and per `value`.
|
|
19
19
|
* The outer key is the subclass constructor (so each subclass has its own
|
|
@@ -28,9 +28,9 @@ export declare abstract class BaseEnum<T = number> {
|
|
|
28
28
|
*
|
|
29
29
|
* @param value - Identifying value of the constant (used by `fromValue()`, `equals()`).
|
|
30
30
|
* @param label - Display label/description of the constant.
|
|
31
|
-
* @param
|
|
31
|
+
* @param opt - Optional extra data, freely defined by the subclass as needed.
|
|
32
32
|
*/
|
|
33
|
-
protected constructor(value: T, label: string,
|
|
33
|
+
protected constructor(value: T, label: string, opt?: O | undefined);
|
|
34
34
|
/**
|
|
35
35
|
* Returns the names of the `static readonly` fields declared on the
|
|
36
36
|
* subclass, in declaration order. Mirrors the idea of an enum constant's
|
package/dist/cjs/base-enum.js
CHANGED
|
@@ -5,7 +5,7 @@ exports.BaseEnum = void 0;
|
|
|
5
5
|
* Base class for simulating Java-style enums in TypeScript.
|
|
6
6
|
*
|
|
7
7
|
* Subclasses only need to `extends BaseEnum<...>` and declare constants as
|
|
8
|
-
* `static readonly Xxx = new SubClass(value, label,
|
|
8
|
+
* `static readonly Xxx = new SubClass(value, label, opt?)`, without
|
|
9
9
|
* redeclaring a constructor. Because `BaseEnum`'s constructor is `protected`,
|
|
10
10
|
* subclasses inherit it while keeping the same protection — instances can't
|
|
11
11
|
* be `new`-ed from outside the class, which preserves enum singleton/identity
|
|
@@ -21,12 +21,12 @@ class BaseEnum {
|
|
|
21
21
|
*
|
|
22
22
|
* @param value - Identifying value of the constant (used by `fromValue()`, `equals()`).
|
|
23
23
|
* @param label - Display label/description of the constant.
|
|
24
|
-
* @param
|
|
24
|
+
* @param opt - Optional extra data, freely defined by the subclass as needed.
|
|
25
25
|
*/
|
|
26
|
-
constructor(value, label,
|
|
26
|
+
constructor(value, label, opt) {
|
|
27
27
|
this.value = value;
|
|
28
28
|
this.label = label;
|
|
29
|
-
this.
|
|
29
|
+
this.opt = opt;
|
|
30
30
|
let map = BaseEnum.registry.get(this.constructor);
|
|
31
31
|
if (!map) {
|
|
32
32
|
map = new Map();
|
package/dist/esm/base-enum.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Base class for simulating Java-style enums in TypeScript.
|
|
3
3
|
*
|
|
4
4
|
* Subclasses only need to `extends BaseEnum<...>` and declare constants as
|
|
5
|
-
* `static readonly Xxx = new SubClass(value, label,
|
|
5
|
+
* `static readonly Xxx = new SubClass(value, label, opt?)`, without
|
|
6
6
|
* redeclaring a constructor. Because `BaseEnum`'s constructor is `protected`,
|
|
7
7
|
* subclasses inherit it while keeping the same protection — instances can't
|
|
8
8
|
* be `new`-ed from outside the class, which preserves enum singleton/identity
|
|
@@ -10,10 +10,10 @@
|
|
|
10
10
|
*
|
|
11
11
|
* @typeParam T - Type of the `value` field (defaults to `number`).
|
|
12
12
|
*/
|
|
13
|
-
export declare abstract class BaseEnum<T = number> {
|
|
13
|
+
export declare abstract class BaseEnum<T = number, O = any> {
|
|
14
14
|
readonly value: T;
|
|
15
15
|
readonly label: string;
|
|
16
|
-
readonly
|
|
16
|
+
readonly opt?: O | undefined;
|
|
17
17
|
/**
|
|
18
18
|
* Registry of every instance created, keyed per subclass and per `value`.
|
|
19
19
|
* The outer key is the subclass constructor (so each subclass has its own
|
|
@@ -28,9 +28,9 @@ export declare abstract class BaseEnum<T = number> {
|
|
|
28
28
|
*
|
|
29
29
|
* @param value - Identifying value of the constant (used by `fromValue()`, `equals()`).
|
|
30
30
|
* @param label - Display label/description of the constant.
|
|
31
|
-
* @param
|
|
31
|
+
* @param opt - Optional extra data, freely defined by the subclass as needed.
|
|
32
32
|
*/
|
|
33
|
-
protected constructor(value: T, label: string,
|
|
33
|
+
protected constructor(value: T, label: string, opt?: O | undefined);
|
|
34
34
|
/**
|
|
35
35
|
* Returns the names of the `static readonly` fields declared on the
|
|
36
36
|
* subclass, in declaration order. Mirrors the idea of an enum constant's
|
package/dist/esm/base-enum.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Base class for simulating Java-style enums in TypeScript.
|
|
3
3
|
*
|
|
4
4
|
* Subclasses only need to `extends BaseEnum<...>` and declare constants as
|
|
5
|
-
* `static readonly Xxx = new SubClass(value, label,
|
|
5
|
+
* `static readonly Xxx = new SubClass(value, label, opt?)`, without
|
|
6
6
|
* redeclaring a constructor. Because `BaseEnum`'s constructor is `protected`,
|
|
7
7
|
* subclasses inherit it while keeping the same protection — instances can't
|
|
8
8
|
* be `new`-ed from outside the class, which preserves enum singleton/identity
|
|
@@ -18,12 +18,12 @@ export class BaseEnum {
|
|
|
18
18
|
*
|
|
19
19
|
* @param value - Identifying value of the constant (used by `fromValue()`, `equals()`).
|
|
20
20
|
* @param label - Display label/description of the constant.
|
|
21
|
-
* @param
|
|
21
|
+
* @param opt - Optional extra data, freely defined by the subclass as needed.
|
|
22
22
|
*/
|
|
23
|
-
constructor(value, label,
|
|
23
|
+
constructor(value, label, opt) {
|
|
24
24
|
this.value = value;
|
|
25
25
|
this.label = label;
|
|
26
|
-
this.
|
|
26
|
+
this.opt = opt;
|
|
27
27
|
let map = BaseEnum.registry.get(this.constructor);
|
|
28
28
|
if (!map) {
|
|
29
29
|
map = new Map();
|