-
Notifications
You must be signed in to change notification settings - Fork 0
/
defineDictionary.ts
276 lines (261 loc) · 6.43 KB
/
defineDictionary.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
interface BaseOption {
key: PropertyKey
value: string | number
}
type ToPropertyPrefix<N extends string = ''> = N extends '' ? '' : `get_${N}_`
type ToProperty<
Property extends string,
N extends string = '',
> = `${ToPropertyPrefix<N>}${Property}`
type ToKeys<T> = T extends readonly [infer A, ...infer B]
? A extends {
readonly key: infer K
}
? B['length'] extends 0
? [K]
: [K, ...ToKeys<B>]
: never
: []
type ToValues<T> = T extends readonly [infer A, ...infer B]
? A extends {
readonly value: infer K
}
? B['length'] extends 0
? [K]
: [K, ...ToValues<B>]
: never
: []
type ToSingleKeyMap<T> = T extends {
readonly key: infer K
}
? K extends PropertyKey
? {
readonly [Key in K]: T;
}
: never
: never
export type MergeIntersection<A> = A extends infer T
? { [Key in keyof T]: T[Key] }
: never
type ToKeyMap<T> = T extends readonly [infer A, ...infer B]
? B['length'] extends 0
? ToSingleKeyMap<A>
: MergeIntersection<ToSingleKeyMap<A> & ToKeyMap<B>>
: []
type ToSingleValueMap<T> = T extends {
readonly value: infer K
}
? K extends PropertyKey
? {
readonly [Key in K]: T;
}
: never
: never
type ToValueMap<T> = T extends readonly [infer A, ...infer B]
? B['length'] extends 0
? ToSingleValueMap<A>
: MergeIntersection<ToSingleValueMap<A> & ToValueMap<B>>
: []
type ToSingleKeyValue<T> = T extends {
readonly key: infer K
readonly value: infer V
}
? K extends PropertyKey
? {
readonly [Key in K]: V;
}
: never
: never
type ToKeyValue<T> = T extends readonly [infer A, ...infer B]
? B['length'] extends 0
? ToSingleKeyValue<A>
: MergeIntersection<ToSingleKeyValue<A> & ToKeyValue<B>>
: []
type ToSingleValueKey<T> = T extends {
readonly key: infer K
readonly value: infer V
}
? V extends PropertyKey
? {
readonly [Key in V]: K;
}
: never
: never
type ToValueKey<T> = T extends readonly [infer A, ...infer B]
? B['length'] extends 0
? ToSingleValueKey<A>
: MergeIntersection<ToSingleValueKey<A> & ToValueKey<B>>
: []
/**
* 定义业务字典
* @param {T} options - The options of the dictionary. (ts as const is required !!!)
* @param {N} namespace - The namespace of the dictionary.
* @returns
* @example
* ```ts
// at src/constant.ts
const {
get_MUSIC_TYPE_KEYS,
get_MUSIC_TYPE_KV,
get_MUSIC_TYPE_MAP,
get_MUSIC_TYPE_MAP_BY_KEY,
get_MUSIC_TYPE_MAP_BY_VALUE,
get_MUSIC_TYPE_OPTIONS,
get_MUSIC_TYPE_VALUES,
get_MUSIC_TYPE_VK
} = defineDictionary([
{
key: 'POP',
value: 1,
label: '流行音乐',
color: 'red',
},
{
key: 'ROCK',
value: 2,
label: '摇滚音乐',
color: 'blue',
},
] as const, 'MUSIC_TYPE') // !!! as const is required for type safe
export const MUSIC_TYPE_KEYS = get_MUSIC_TYPE_KEYS()
// ['POP', 'ROCK']
export const MUSIC_TYPE_VALUES = get_MUSIC_TYPE_VALUES()
// [1, 2]
export const MUSIC_TYPE_KV = get_MUSIC_TYPE_KV()
export type MusicType = keyof typeof MUSIC_TYPE_KV
// { POP: 1, ROCK: 2 }
export const MUSIC_TYPE_VK = get_MUSIC_TYPE_VK()
// { 1: 'POP', 2: 'ROCK' }
export const MUSIC_TYPE_MAP_BY_KEY = get_MUSIC_TYPE_MAP_BY_KEY()
// POP: {
// key: 'POP',
// value: 1,
// label: '流行音乐',
// color: 'red',
// },
// ROCK: {
// key: 'ROCK',
// value: 2,
// label: '摇滚音乐',
// color: 'blue',
// },
export const MUSIC_TYPE_MAP_BY_VALUE = get_MUSIC_TYPE_MAP_BY_VALUE()
// 1: {
// key: 'POP',
// value: 1,
// label: '流行音乐',
// color: 'red',
// },
// 2: {
// key: 'ROCK',
// value: 2,
// label: '摇滚音乐',
// color: 'blue',
// },
export const MUSIC_TYPE_MAP = get_MUSIC_TYPE_MAP()
// { POP: 1, ROCK: 2 }
export const MUSIC_TYPE_OPTIONS = get_MUSIC_TYPE_OPTIONS()
// [
// {
// key: 'POP',
// value: 1,
// label: '流行音乐',
// color: 'red',
// },
// {
// key: 'ROCK',
// value: 2,
// label: '摇滚音乐',
// color: 'blue',
// }
// ]
* ```
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/defineDictionary.ts
*/
export function defineDictionary<
T extends readonly BaseOption[],
N extends string,
>(options: T, namespace: N) {
if (!namespace)
throw new Error('namespace is required')
const prefix = `get_${namespace}_`
return {
[`${prefix}KEYS`]: () => options.map(item => item.key),
[`${prefix}VALUES`]: () => options.map(item => item.value),
[`${prefix}KV`]: () => {
return options.reduce(
(prev, cur) => ({
...prev,
[cur.key]: cur.value,
}),
{},
)
},
[`${prefix}VK`]: () => {
return options.reduce(
(prev, cur) => ({
...prev,
[cur.value]: cur.key,
}),
{},
)
},
[`${prefix}MAP_BY_KEY`]: () => {
return options.reduce(
(prev, cur) => ({
...prev,
[cur.key]: cur,
}),
{},
)
},
[`${prefix}MAP_BY_VALUE`]: () => {
return options.reduce(
(prev, cur) => ({
...prev,
[cur.value]: cur,
}),
{},
)
},
// [`${prefix}KEY_MAP`]: () => {
// return {
// [`${namespace}_KEY_MAP`]: options.reduce(
// (prev, cur) => ({
// ...prev,
// [cur.key]: cur,
// }),
// {},
// ),
// }
// },
[`${prefix}MAP`]: () => {
return options.reduce(
(prev, cur) => ({
...prev,
[cur.key]: cur.value,
}),
{},
)
},
[`${prefix}OPTIONS`]: () => options,
} as MergeIntersection<
{
[Key in ToProperty<'KV', N>]: () => ToKeyValue<T>
} & {
[Key in ToProperty<'VK', N>]: () => ToValueKey<T>
} & {
[Key in ToProperty<'KEYS', N>]: () => ToKeys<T>
} & {
[Key in ToProperty<'VALUES', N>]: () => ToValues<T>
} & {
[Key in ToProperty<'MAP_BY_KEY', N>]: () => ToKeyMap<T>
} & {
[Key in ToProperty<'MAP_BY_VALUE', N>]: () => ToValueMap<T>
} & {
[Key in ToProperty<'MAP', N>]: () => ToKeyValue<T>
} & {
[Key in ToProperty<'OPTIONS', N>]: () => T
}
>
}