@tstdl/base 0.92.28 → 0.92.30
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/enumeration/enumeration.d.ts +8 -0
- package/enumeration/enumeration.js +16 -0
- package/enumeration/index.d.ts +1 -0
- package/enumeration/index.js +1 -0
- package/package.json +2 -1
- package/types.d.ts +1 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Tagged } from 'type-fest';
|
|
2
|
+
import type { EnumerationObject, SimplifyObject } from '../types.js';
|
|
3
|
+
export type EnumType<T extends EnumerationObject> = T[keyof T];
|
|
4
|
+
export declare function defineEnum<const Name extends string, const T extends EnumerationObject>(name: Name, enumObject: T): SimplifyObject<{
|
|
5
|
+
[P in keyof T]: Tagged<T[P], Name>;
|
|
6
|
+
}>;
|
|
7
|
+
export declare function tryGetEnumName(enumeration: EnumerationObject): string | undefined;
|
|
8
|
+
export declare function getEnumName(enumeration: EnumerationObject): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { isUndefined } from '../utils/type-guards.js';
|
|
2
|
+
const registry = new WeakMap();
|
|
3
|
+
export function defineEnum(name, enumObject) {
|
|
4
|
+
registry.set(enumObject, name);
|
|
5
|
+
return enumObject;
|
|
6
|
+
}
|
|
7
|
+
export function tryGetEnumName(enumeration) {
|
|
8
|
+
return registry.get(enumeration);
|
|
9
|
+
}
|
|
10
|
+
export function getEnumName(enumeration) {
|
|
11
|
+
const name = tryGetEnumName(enumeration);
|
|
12
|
+
if (isUndefined(name)) {
|
|
13
|
+
throw new Error('Unknown enumeration');
|
|
14
|
+
}
|
|
15
|
+
return name;
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './enumeration.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './enumeration.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tstdl/base",
|
|
3
|
-
"version": "0.92.
|
|
3
|
+
"version": "0.92.30",
|
|
4
4
|
"author": "Patrick Hein",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
"./distributed-loop": "./distributed-loop/index.js",
|
|
48
48
|
"./dom": "./dom/index.js",
|
|
49
49
|
"./document-management": "./document-management/index.js",
|
|
50
|
+
"./enumeration": "./enumeration/index.js",
|
|
50
51
|
"./enumerable": "./enumerable/index.js",
|
|
51
52
|
"./errors": "./errors/index.js",
|
|
52
53
|
"./file": "./file/index.js",
|
package/types.d.ts
CHANGED
|
@@ -49,7 +49,7 @@ export type EnumerationArray = readonly [string | number, ...(string | number)[]
|
|
|
49
49
|
export type EnumerationObject<V extends string | number = string | number> = Record<string, V>;
|
|
50
50
|
export type EnumerationKey<T extends EnumerationObject = EnumerationObject> = Extract<keyof T, string>;
|
|
51
51
|
export type EnumerationMap<T extends EnumerationObject = EnumerationObject> = SimplifyObject<{
|
|
52
|
-
[P in EnumerationKey<T>]: (T[P] extends number ? (`${T[P]}` extends `${infer U extends number}` ? U : never) :
|
|
52
|
+
[P in EnumerationKey<T>]: (T[P] extends number ? (`${T[P]}` extends `${infer U extends number}` ? U : never) : T[P]) | T[P];
|
|
53
53
|
}>;
|
|
54
54
|
export type EnumerationValue<T extends Enumeration = Enumeration> = T extends EnumerationObject ? SimplifyDeep<EnumerationMap<T>[keyof EnumerationMap<T>]> : T extends EnumerationArray ? T[number] : never;
|
|
55
55
|
export type EnumerationEntry<T extends EnumerationObject = EnumerationObject> = {
|