-
Notifications
You must be signed in to change notification settings - Fork 0
/
constants.ts
111 lines (106 loc) · 3.62 KB
/
constants.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {
Constants,
ConstantsWrapper,
InferTaggedConstants,
InferUntaggedConstants,
} from "../types/consts.js";
import { Narrow } from "../types/narrowable.js";
import { TagSupertype } from "../types/tag.js";
/**
* Create a Constants object from an array of PropertyKeys, using each element both as key and value
*
* @param tag - The tag used to enable nominal typing
* @param arr - The input tuple containing the constants
* @returns A Constants object
*/
export function constants<
Tag extends TagSupertype,
T extends readonly PropertyKey[] | [],
>(
tag: Tag,
arr: Narrow<T>,
): ConstantsWrapper<{ [I in keyof T & `${number}` as T[I]]: T[I] }, Tag>;
/**
* Create a Constants object from a record containing arbitrary values
*
* @param tag - The tag used to enable nominal typing
* @param obj - The input record containing the keys and their values
* @returns A Constants object
*/
export function constants<
Tag extends TagSupertype,
T extends Record<PropertyKey, unknown>,
>(tag: Tag, obj: Narrow<T>): ConstantsWrapper<T, Tag>;
export function constants(tag: PropertyKey, x: unknown[] | object): Constants {
let constants = null;
if (Array.isArray(x)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
constants = Object.freeze(Object.fromEntries(x.map(el => [el, el])));
} else {
constants = Object.freeze({ ...x });
}
return {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
tagged: constants,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
untagged: constants,
};
}
/**
* Create a record of tagged constants from an array of PropertyKeys, using each element both as key and value
*
* @param tag - The tag used to enable nominal typing
* @param arr - The input tuple containing the constants
* @returns A record of tagged constants
*/
export function constantsTagged<
Tag extends TagSupertype,
T extends readonly PropertyKey[] | [],
>(
tag: Tag,
arr: Narrow<T>,
): InferTaggedConstants<
ConstantsWrapper<{ [I in keyof T & `${number}` as T[I]]: T[I] }, Tag>
>;
/**
* Create a record of tagged constants from a record containing arbitrary values
*
* @param tag - The tag used to enable nominal typing
* @param obj - The input record containing the keys and their values
* @returns A record of tagged constants
*/
export function constantsTagged<
Tag extends TagSupertype,
T extends Record<PropertyKey, unknown>,
>(tag: Tag, obj: Narrow<T>): InferTaggedConstants<ConstantsWrapper<T, Tag>>;
export function constantsTagged(
tag: PropertyKey,
x: unknown[] | object,
): Constants {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return constants(tag, x as any).tagged as any;
}
/**
* Create a record of untagged constants from an array of PropertyKeys, using each element both as key and value
*
* @param arr - The input tuple containing the constants
* @returns A record of untagged constants
*/
export function constantsUntagged<T extends readonly PropertyKey[] | []>(
arr: Narrow<T>,
): InferUntaggedConstants<
ConstantsWrapper<{ [I in keyof T & `${number}` as T[I]]: T[I] }, PropertyKey>
>;
/**
* Create a record of untagged constants from a record containing arbitrary values
*
* @param obj - The input record containing the keys and their values
* @returns A record of untagged constants
*/
export function constantsUntagged<T extends Record<PropertyKey, unknown>>(
obj: Narrow<T>,
): InferUntaggedConstants<ConstantsWrapper<T, PropertyKey>>;
export function constantsUntagged(x: unknown[] | object): Constants {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return constants(null as any, x as any).untagged as any;
}