@fjell/core 4.2.12
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/LICENSE +202 -0
- package/dist/src/AItemService.d.ts +8 -0
- package/dist/src/AItemService.js +21 -0
- package/dist/src/AItemService.js.map +1 -0
- package/dist/src/dictionary.d.ts +16 -0
- package/dist/src/dictionary.js +44 -0
- package/dist/src/dictionary.js.map +1 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.js +12 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/item/IFactory.d.ts +16 -0
- package/dist/src/item/IFactory.js +64 -0
- package/dist/src/item/IFactory.js.map +1 -0
- package/dist/src/item/IQFactory.d.ts +23 -0
- package/dist/src/item/IQFactory.js +129 -0
- package/dist/src/item/IQFactory.js.map +1 -0
- package/dist/src/item/IQUtils.d.ts +35 -0
- package/dist/src/item/IQUtils.js +278 -0
- package/dist/src/item/IQUtils.js.map +1 -0
- package/dist/src/item/IUtils.d.ts +6 -0
- package/dist/src/item/IUtils.js +68 -0
- package/dist/src/item/IUtils.js.map +1 -0
- package/dist/src/item/ItemQuery.d.ts +62 -0
- package/dist/src/item/ItemQuery.js +10 -0
- package/dist/src/item/ItemQuery.js.map +1 -0
- package/dist/src/items.d.ts +51 -0
- package/dist/src/items.js +4 -0
- package/dist/src/items.js.map +1 -0
- package/dist/src/key/KUtils.d.ts +35 -0
- package/dist/src/key/KUtils.js +225 -0
- package/dist/src/key/KUtils.js.map +1 -0
- package/dist/src/keys.d.ts +66 -0
- package/dist/src/keys.js +5 -0
- package/dist/src/keys.js.map +1 -0
- package/dist/src/logger.d.ts +2 -0
- package/dist/src/logger.js +4 -0
- package/dist/src/logger.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/eslint.config.mjs +70 -0
- package/package.json +40 -0
- package/src/AItemService.ts +38 -0
- package/src/dictionary.ts +54 -0
- package/src/index.ts +13 -0
- package/src/item/IFactory.ts +122 -0
- package/src/item/IQFactory.ts +163 -0
- package/src/item/IQUtils.ts +360 -0
- package/src/item/IUtils.ts +99 -0
- package/src/item/ItemQuery.ts +87 -0
- package/src/items.ts +102 -0
- package/src/key/KUtils.ts +347 -0
- package/src/keys.ts +95 -0
- package/src/logger.ts +5 -0
package/src/items.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ComKey, PriKey } from './keys';
|
|
2
|
+
|
|
3
|
+
export type RecursivePartial<T> = {
|
|
4
|
+
[P in keyof T]?:
|
|
5
|
+
T[P] extends (infer U)[] ? RecursivePartial<U>[] :
|
|
6
|
+
T[P] extends object | undefined ? RecursivePartial<T[P]> :
|
|
7
|
+
T[P];
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type Identified<S extends string,
|
|
11
|
+
L1 extends string = never,
|
|
12
|
+
L2 extends string = never,
|
|
13
|
+
L3 extends string = never,
|
|
14
|
+
L4 extends string = never,
|
|
15
|
+
L5 extends string = never> = {
|
|
16
|
+
key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type Propertied = { [key: string]: any };
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* This is a generic item event, and it's designed to make sure we have the ability to define not just
|
|
23
|
+
* the required fields, but also the optional fields.
|
|
24
|
+
*/
|
|
25
|
+
export interface ItemEvent {
|
|
26
|
+
at: Date | null;
|
|
27
|
+
by?: ComKey<any, any | never, any | never, any | never, any | never, any | never> | PriKey<any>;
|
|
28
|
+
agg?: Item<any, any, any, any, any, any>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* This is a required item event, and it's here mainly to define that there are events that must be present.
|
|
33
|
+
*/
|
|
34
|
+
export interface RequiredItemEvent extends ItemEvent {
|
|
35
|
+
at: Date;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type Evented = Record<string, ItemEvent>;
|
|
39
|
+
|
|
40
|
+
export interface Timestamped extends Evented {
|
|
41
|
+
created: RequiredItemEvent;
|
|
42
|
+
updated: RequiredItemEvent;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export interface Deletable extends Partial<Evented> {
|
|
46
|
+
deleted: ItemEvent;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type ManagedEvents = Timestamped & Deletable;
|
|
50
|
+
|
|
51
|
+
export type ItemProperties<
|
|
52
|
+
S extends string,
|
|
53
|
+
L1 extends string = never,
|
|
54
|
+
L2 extends string = never,
|
|
55
|
+
L3 extends string = never,
|
|
56
|
+
L4 extends string = never,
|
|
57
|
+
L5 extends string = never> =
|
|
58
|
+
Omit<Item<S, L1, L2, L3, L4, L5>, 'events' | 'key'> & {
|
|
59
|
+
events?: Evented;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type TypesProperties<
|
|
63
|
+
V extends Item<S, L1, L2, L3, L4, L5>,
|
|
64
|
+
S extends string,
|
|
65
|
+
L1 extends string = never,
|
|
66
|
+
L2 extends string = never,
|
|
67
|
+
L3 extends string = never,
|
|
68
|
+
L4 extends string = never,
|
|
69
|
+
L5 extends string = never> =
|
|
70
|
+
Omit<V, 'events' | 'key'> & {
|
|
71
|
+
events?: Evented;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export type References = Record<
|
|
75
|
+
string,
|
|
76
|
+
ComKey<string, string | never, string | never, string | never, string | never, string | never> |
|
|
77
|
+
PriKey<string>
|
|
78
|
+
>;
|
|
79
|
+
|
|
80
|
+
export type ReferenceItem<
|
|
81
|
+
S extends string,
|
|
82
|
+
L1 extends string = never,
|
|
83
|
+
L2 extends string = never,
|
|
84
|
+
L3 extends string = never,
|
|
85
|
+
L4 extends string = never,
|
|
86
|
+
L5 extends string = never
|
|
87
|
+
> = { key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, item: Item<S, L1, L2, L3, L4, L5> };
|
|
88
|
+
|
|
89
|
+
export type ReferenceItems = Record<
|
|
90
|
+
string, ReferenceItem<string, string | never, string | never, string | never, string | never, string | never>>;
|
|
91
|
+
|
|
92
|
+
export interface Item<S extends string = never,
|
|
93
|
+
L1 extends string = never,
|
|
94
|
+
L2 extends string = never,
|
|
95
|
+
L3 extends string = never,
|
|
96
|
+
L4 extends string = never,
|
|
97
|
+
L5 extends string = never> extends Identified<S, L1, L2, L3, L4, L5>, Propertied {
|
|
98
|
+
events: ManagedEvents & Evented;
|
|
99
|
+
// TODO: This is a bit of a hack to get around the fact that we don't want to pass all these generics
|
|
100
|
+
aggs?: ReferenceItems;
|
|
101
|
+
refs?: References;
|
|
102
|
+
}
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/* eslint-disable no-undefined */
|
|
2
|
+
import {
|
|
3
|
+
ComKey,
|
|
4
|
+
LocKey,
|
|
5
|
+
LocKeyArray,
|
|
6
|
+
PriKey,
|
|
7
|
+
UUID
|
|
8
|
+
} from "@/keys";
|
|
9
|
+
|
|
10
|
+
import LibLogger from "@/logger";
|
|
11
|
+
|
|
12
|
+
const logger = LibLogger.get('KUtils');
|
|
13
|
+
|
|
14
|
+
export const isItemKeyEqual = <
|
|
15
|
+
S extends string,
|
|
16
|
+
L1 extends string = never,
|
|
17
|
+
L2 extends string = never,
|
|
18
|
+
L3 extends string = never,
|
|
19
|
+
L4 extends string = never,
|
|
20
|
+
L5 extends string = never
|
|
21
|
+
>(a: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>, b: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>): boolean => {
|
|
22
|
+
logger.trace('isKeyEqual', { a, b });
|
|
23
|
+
if (isComKey(a) && isComKey(b)) {
|
|
24
|
+
return isComKeyEqual(a as ComKey<S, L1, L2, L3, L4, L5>, b as ComKey<S, L1, L2, L3, L4, L5>);
|
|
25
|
+
} else if (isPriKey(a) && isPriKey(b)) {
|
|
26
|
+
if(isComKey(a) || isComKey(b)) {
|
|
27
|
+
return false;
|
|
28
|
+
} else {
|
|
29
|
+
return isPriKeyEqual(a as PriKey<S>, b as PriKey<S>);
|
|
30
|
+
}
|
|
31
|
+
} else {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const isPriKeyEqual = <
|
|
37
|
+
S extends string,
|
|
38
|
+
>(a: PriKey<S>, b: PriKey<S>): boolean => {
|
|
39
|
+
logger.trace('isPriKeyEqual', { a, b });
|
|
40
|
+
return a && b && a.pk === b.pk && a.kt === b.kt;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const isLocKeyEqual = <
|
|
44
|
+
L1 extends string,
|
|
45
|
+
L2 extends string = never,
|
|
46
|
+
L3 extends string = never,
|
|
47
|
+
L4 extends string = never,
|
|
48
|
+
L5 extends string = never
|
|
49
|
+
>(a: LocKey<L1 | L2 | L3 | L4 | L5>, b: LocKey<L1 | L2 | L3 | L4 | L5>): boolean => {
|
|
50
|
+
logger.trace('isLocKeyEqual', { a, b });
|
|
51
|
+
return a && b && a.lk === b.lk && a.kt === b.kt;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const isComKeyEqual = <
|
|
55
|
+
S extends string,
|
|
56
|
+
L1 extends string = never,
|
|
57
|
+
L2 extends string = never,
|
|
58
|
+
L3 extends string = never,
|
|
59
|
+
L4 extends string = never,
|
|
60
|
+
L5 extends string = never
|
|
61
|
+
>(a: ComKey<S, L1, L2, L3, L4, L5>, b: ComKey<S, L1, L2, L3, L4, L5>): boolean => {
|
|
62
|
+
logger.trace('isComKeyEqual', { a, b });
|
|
63
|
+
if (a && b && isPriKeyEqual({ kt: a.kt, pk: a.pk } as PriKey<S>, { kt: b.kt, pk: b.pk } as PriKey<S>)) {
|
|
64
|
+
if (a.loc.length === b.loc.length) {
|
|
65
|
+
for (let i = 0; i < a.loc.length; i++) {
|
|
66
|
+
if (!isLocKeyEqual<L1, L2, L3, L4, L5>(a.loc[i], b.loc[i])) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
} else {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const isItemKey = (key: any): boolean => {
|
|
80
|
+
logger.trace('isItemKey', { key });
|
|
81
|
+
return key !== undefined && (isComKey(key) || isPriKey(key));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const isComKey = (key: any): boolean => {
|
|
85
|
+
logger.trace('isComKey', { key });
|
|
86
|
+
return key !== undefined &&
|
|
87
|
+
(key.pk !== undefined && key.kt !== undefined) && (key.loc !== undefined && key.loc.length > 0);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const isPriKey = (key: any): boolean => {
|
|
91
|
+
logger.trace('isPriKey', { key });
|
|
92
|
+
return key !== undefined &&
|
|
93
|
+
(key.pk !== undefined && key.kt !== undefined) && (key.loc === undefined || key.loc.length === 0);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const isLocKey = (key: any): boolean => {
|
|
97
|
+
logger.trace('isLocKey', { key });
|
|
98
|
+
return key !== undefined && (key.lk !== undefined && key.kt !== undefined);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export const generateKeyArray = <
|
|
102
|
+
S extends string,
|
|
103
|
+
L1 extends string = never,
|
|
104
|
+
L2 extends string = never,
|
|
105
|
+
L3 extends string = never,
|
|
106
|
+
L4 extends string = never,
|
|
107
|
+
L5 extends string = never,
|
|
108
|
+
>(key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S> | LocKeyArray<L1, L2, L3, L4, L5> | []):
|
|
109
|
+
Array<PriKey<S> | LocKey<L1 | L2 | L3 | L4 | L5>> => {
|
|
110
|
+
logger.trace('generateKeyArray', { key });
|
|
111
|
+
const keys: Array<PriKey<S> | LocKey<L1 | L2 | L3 | L4 | L5>> = [];
|
|
112
|
+
|
|
113
|
+
if (isComKey(key) || isPriKey(key)) {
|
|
114
|
+
// console.log('it is an item key');
|
|
115
|
+
if (isComKey(key)) {
|
|
116
|
+
// console.log('it is a composite key');
|
|
117
|
+
const comKey = key as ComKey<S, L1, L2, L3, L4, L5>;
|
|
118
|
+
keys.push({ pk: comKey.pk, kt: comKey.kt });
|
|
119
|
+
for (let i = 0; i < comKey.loc.length; i++) {
|
|
120
|
+
keys.push(comKey.loc[i]);
|
|
121
|
+
}
|
|
122
|
+
} else {
|
|
123
|
+
keys.push(key as PriKey<S>);
|
|
124
|
+
}
|
|
125
|
+
} else {
|
|
126
|
+
// console.log('is is an array, length: ' + key.length);
|
|
127
|
+
const locKeys = key as LocKey<L1 | L2 | L3 | L4 | L5>[];
|
|
128
|
+
for (let i = 0; i < locKeys.length; i++) {
|
|
129
|
+
// console.log('Pushing a key');
|
|
130
|
+
keys.push(locKeys[i]);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return keys;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// TODO: Exactly the same as in ContainedItemLib
|
|
137
|
+
export const constructPriKey = <S extends string>(
|
|
138
|
+
pk: string | PriKey<S>,
|
|
139
|
+
kt: S,
|
|
140
|
+
) => {
|
|
141
|
+
logger.trace('constructPriKey', { pk, kt });
|
|
142
|
+
let pri: PriKey<S>;
|
|
143
|
+
if (typeof pk === 'string') {
|
|
144
|
+
pri = { kt: kt as S, pk: pk as UUID };
|
|
145
|
+
} else {
|
|
146
|
+
pri = pk;
|
|
147
|
+
}
|
|
148
|
+
return pri;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// TODO: Exactly the same as in ContainedItemLib
|
|
152
|
+
export const cPK = constructPriKey;
|
|
153
|
+
|
|
154
|
+
export const toKeyTypeArray = <
|
|
155
|
+
S extends string,
|
|
156
|
+
L1 extends string = never,
|
|
157
|
+
L2 extends string = never,
|
|
158
|
+
L3 extends string = never,
|
|
159
|
+
L4 extends string = never,
|
|
160
|
+
L5 extends string = never
|
|
161
|
+
>(ik: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>):
|
|
162
|
+
string[] => {
|
|
163
|
+
logger.trace('toKeyTypeArray', { ik });
|
|
164
|
+
if (isComKey(ik)) {
|
|
165
|
+
const ck = ik as ComKey<S, L1, L2, L3, L4, L5>;
|
|
166
|
+
return [ck.kt, ...ck.loc.map(l => l.kt)];
|
|
167
|
+
} else {
|
|
168
|
+
return [(ik as PriKey<S>).kt];
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export const abbrevIK = <
|
|
173
|
+
S extends string,
|
|
174
|
+
L1 extends string = never,
|
|
175
|
+
L2 extends string = never,
|
|
176
|
+
L3 extends string = never,
|
|
177
|
+
L4 extends string = never,
|
|
178
|
+
L5 extends string = never
|
|
179
|
+
>(ik: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>): string => {
|
|
180
|
+
logger.trace('abbrevIK', { ik });
|
|
181
|
+
if(ik) {
|
|
182
|
+
if (isComKey(ik)) {
|
|
183
|
+
const ck = ik as ComKey<S, L1, L2, L3, L4, L5>;
|
|
184
|
+
return `${ck.kt}:${ck.pk}:${ck.loc.map(l => `${l.kt}:${l.lk}`).join(',')}`;
|
|
185
|
+
} else {
|
|
186
|
+
return `${(ik as PriKey<S>).kt}:${(ik as PriKey<S>).pk}`;
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
return 'null IK';
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export const abbrevLKA = <
|
|
194
|
+
L1 extends string,
|
|
195
|
+
L2 extends string = never,
|
|
196
|
+
L3 extends string = never,
|
|
197
|
+
L4 extends string = never,
|
|
198
|
+
L5 extends string = never
|
|
199
|
+
>(keyArray: Array<LocKey<L1 | L2 | L3 | L4 | L5>> | null): string => {
|
|
200
|
+
logger.trace('abbrevLKA', { keyArray });
|
|
201
|
+
if (keyArray === undefined || keyArray === null) {
|
|
202
|
+
return 'null LKA';
|
|
203
|
+
} else {
|
|
204
|
+
return keyArray.map(key => {
|
|
205
|
+
if (key) {
|
|
206
|
+
return `${key.kt}:${key.lk}`;
|
|
207
|
+
} else {
|
|
208
|
+
return key;
|
|
209
|
+
}
|
|
210
|
+
}).join(',');
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export const primaryType = <
|
|
215
|
+
S extends string,
|
|
216
|
+
L1 extends string = never,
|
|
217
|
+
L2 extends string = never,
|
|
218
|
+
L3 extends string = never,
|
|
219
|
+
L4 extends string = never,
|
|
220
|
+
L5 extends string = never
|
|
221
|
+
>(ik: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>): string => {
|
|
222
|
+
logger.trace('primaryType', { ik });
|
|
223
|
+
if (isComKey(ik)) {
|
|
224
|
+
return (ik as ComKey<S, L1, L2, L3, L4, L5>).kt;
|
|
225
|
+
} else {
|
|
226
|
+
return (ik as PriKey<S>).kt;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
*
|
|
232
|
+
* @param ik ItemKey to be used as a basis for a location
|
|
233
|
+
* @returns
|
|
234
|
+
*/
|
|
235
|
+
export const itemKeyToLocKeyArray =
|
|
236
|
+
<
|
|
237
|
+
S extends string,
|
|
238
|
+
L1 extends string = never,
|
|
239
|
+
L2 extends string = never,
|
|
240
|
+
L3 extends string = never,
|
|
241
|
+
L4 extends string = never,
|
|
242
|
+
L5 extends string = never
|
|
243
|
+
>(ik: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>): LocKeyArray<S, L1, L2, L3, L4> => {
|
|
244
|
+
logger.trace('itemKeyToLocKeyArray', { ik: abbrevIK(ik) });
|
|
245
|
+
let lka: Array<LocKey<L1 | L2 | L3 | L4 | L5>> = [];
|
|
246
|
+
if (isComKey(ik)) {
|
|
247
|
+
const ck = ik as ComKey<S, L1, L2, L3, L4, L5>;
|
|
248
|
+
// @ts-expect-error
|
|
249
|
+
lka = [{ kt: ck.kt, lk: ck.pk } as LocKey<S>, ...ck.loc];
|
|
250
|
+
} else {
|
|
251
|
+
const pk = ik as PriKey<S>;
|
|
252
|
+
// @ts-expect-error
|
|
253
|
+
lka = [{ kt: pk.kt, lk: pk.pk } as LocKey<S>];
|
|
254
|
+
}
|
|
255
|
+
logger.trace('itemKeyToLocKeyArray Results', { ik: abbrevIK(ik), lka: abbrevLKA(lka) });
|
|
256
|
+
return lka as LocKeyArray<S, L1, L2, L3, L4>;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export const ikToLKA = itemKeyToLocKeyArray;
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Sometimes you need to take a location key array and convert it to the item key that points to the containing item.
|
|
263
|
+
* @param lka A location key array
|
|
264
|
+
* @returns An item key corresponding to the containing item this location refers to.
|
|
265
|
+
*/
|
|
266
|
+
export const locKeyArrayToItemKey =
|
|
267
|
+
<
|
|
268
|
+
L1 extends string,
|
|
269
|
+
L2 extends string = never,
|
|
270
|
+
L3 extends string = never,
|
|
271
|
+
L4 extends string = never,
|
|
272
|
+
L5 extends string = never
|
|
273
|
+
>(lka: LocKeyArray<L1, L2, L3, L4, L5>):
|
|
274
|
+
PriKey<L1> | ComKey<L1, L2, L3, L4, L5> => {
|
|
275
|
+
// @ts-expect-error
|
|
276
|
+
logger.trace('locKeyArrayToItemKey', { lka: abbrevLKA(lka) });
|
|
277
|
+
|
|
278
|
+
if(lka.length === 1) {
|
|
279
|
+
const priKey = cPK(lka[0].lk, lka[0].kt);
|
|
280
|
+
return priKey as PriKey<L1>;
|
|
281
|
+
} else {
|
|
282
|
+
const locs = lka.slice(1);
|
|
283
|
+
// @ts-expect-error
|
|
284
|
+
const priKey = cPK(lka[0].lk, lka[0].kt);
|
|
285
|
+
const comKey = { kt: priKey.kt, pk: priKey.pk, loc: locs as LocKeyArray<L2, L3, L4, L5> };
|
|
286
|
+
return comKey as ComKey<L1, L2, L3, L4, L5>;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// TODO: This is annoying that we have to check for '' and 'null'
|
|
291
|
+
export const isValidPriKey = <S extends string>(key: PriKey<S>): boolean => {
|
|
292
|
+
const valid = (key !== undefined && key !== null)
|
|
293
|
+
&& (key.pk !== undefined && key.pk !== null && key.pk !== '' && key.pk !== 'null')
|
|
294
|
+
&& (key.kt !== undefined && key.kt !== null && key.kt !== '' && key.kt !== 'null');
|
|
295
|
+
return valid;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// TODO: This is annoying that we have to check for '' and 'null'
|
|
299
|
+
export const isValidLocKey = <
|
|
300
|
+
L1 extends string,
|
|
301
|
+
L2 extends string = never,
|
|
302
|
+
L3 extends string = never,
|
|
303
|
+
L4 extends string = never,
|
|
304
|
+
L5 extends string = never
|
|
305
|
+
>(key: LocKey<L1 | L2 | L3 | L4 | L5>): boolean => {
|
|
306
|
+
const valid = (key !== undefined && key !== null)
|
|
307
|
+
&& (key.lk !== undefined && key.lk !== null && key.lk !== '' && key.lk !== 'null')
|
|
308
|
+
&& (key.kt !== undefined && key.kt !== null && key.kt !== '' && key.kt !== 'null');
|
|
309
|
+
return valid;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export const isValidLocKeyArray = <
|
|
313
|
+
L1 extends string,
|
|
314
|
+
L2 extends string = never,
|
|
315
|
+
L3 extends string = never,
|
|
316
|
+
L4 extends string = never,
|
|
317
|
+
L5 extends string = never
|
|
318
|
+
>(keyArray: Array<LocKey<L1 | L2 | L3 | L4 | L5>>): boolean => {
|
|
319
|
+
return (keyArray !== undefined && keyArray !== null) && keyArray.every(isValidLocKey);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export const isValidComKey = <
|
|
323
|
+
S extends string,
|
|
324
|
+
L1 extends string = never,
|
|
325
|
+
L2 extends string = never,
|
|
326
|
+
L3 extends string = never,
|
|
327
|
+
L4 extends string = never,
|
|
328
|
+
L5 extends string = never
|
|
329
|
+
>(key: ComKey<S, L1, L2, L3, L4, L5>): boolean => {
|
|
330
|
+
return (key !== undefined
|
|
331
|
+
// @ts-expect-error
|
|
332
|
+
&& key !== null) && isValidPriKey(key) && isValidLocKeyArray(key.loc);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export const isValidItemKey = <
|
|
336
|
+
S extends string,
|
|
337
|
+
L1 extends string = never,
|
|
338
|
+
L2 extends string = never,
|
|
339
|
+
L3 extends string = never,
|
|
340
|
+
L4 extends string = never,
|
|
341
|
+
L5 extends string = never
|
|
342
|
+
>(key: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>): boolean => {
|
|
343
|
+
return (isComKey(key) &&
|
|
344
|
+
isValidComKey(key as ComKey<S, L1, L2, L3, L4, L5>)) || (isPriKey(key) && isValidPriKey(key as PriKey<S>));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export const lkaToIK = locKeyArrayToItemKey;
|
package/src/keys.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
|
|
2
|
+
export type UUID = `${string}-${string}-${string}-${string}-${string}`;
|
|
3
|
+
|
|
4
|
+
export interface ComKey<
|
|
5
|
+
S extends string,
|
|
6
|
+
L1 extends string = never,
|
|
7
|
+
L2 extends string = never,
|
|
8
|
+
L3 extends string = never,
|
|
9
|
+
L4 extends string = never,
|
|
10
|
+
L5 extends string = never
|
|
11
|
+
> {
|
|
12
|
+
readonly kt: S,
|
|
13
|
+
readonly pk: UUID | string,
|
|
14
|
+
readonly loc: LocKeyArray<L1, L2, L3, L4, L5>
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export interface PriKey<
|
|
18
|
+
S extends string,
|
|
19
|
+
> {
|
|
20
|
+
readonly kt: S,
|
|
21
|
+
readonly pk: UUID | string,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export interface LocKey<
|
|
25
|
+
L extends string
|
|
26
|
+
> {
|
|
27
|
+
readonly kt: L,
|
|
28
|
+
readonly lk: UUID | string
|
|
29
|
+
};
|
|
30
|
+
export type LocKeyArray<
|
|
31
|
+
L1 extends string = never,
|
|
32
|
+
L2 extends string = never,
|
|
33
|
+
L3 extends string = never,
|
|
34
|
+
L4 extends string = never,
|
|
35
|
+
L5 extends string = never
|
|
36
|
+
> =
|
|
37
|
+
([L5] extends [never] ?
|
|
38
|
+
([L4] extends [never] ?
|
|
39
|
+
([L3] extends [never] ?
|
|
40
|
+
([L2] extends [never] ?
|
|
41
|
+
([L1] extends [never] ?
|
|
42
|
+
[] | never :
|
|
43
|
+
[LocKey<L1>]) :
|
|
44
|
+
[LocKey<L1>, LocKey<L2>]) :
|
|
45
|
+
[LocKey<L1>, LocKey<L2>, LocKey<L3>]) :
|
|
46
|
+
[LocKey<L1>, LocKey<L2>, LocKey<L3>, LocKey<L4>]) :
|
|
47
|
+
[LocKey<L1>, LocKey<L2>, LocKey<L3>, LocKey<L4>, LocKey<L5>]);
|
|
48
|
+
|
|
49
|
+
export type ItemTypeArray<
|
|
50
|
+
S extends string,
|
|
51
|
+
L1 extends string = never,
|
|
52
|
+
L2 extends string = never,
|
|
53
|
+
L3 extends string = never,
|
|
54
|
+
L4 extends string = never,
|
|
55
|
+
L5 extends string = never,
|
|
56
|
+
> =
|
|
57
|
+
([L5] extends [never] ?
|
|
58
|
+
([L4] extends [never] ?
|
|
59
|
+
([L3] extends [never] ?
|
|
60
|
+
([L2] extends [never] ?
|
|
61
|
+
([L1] extends [never] ?
|
|
62
|
+
[S] :
|
|
63
|
+
[S, L1]) :
|
|
64
|
+
[S, L1, L2]) :
|
|
65
|
+
[S, L1, L2, L3]) :
|
|
66
|
+
[S, L1, L2, L3, L4]) :
|
|
67
|
+
[S, L1, L2, L3, L4, L5]);
|
|
68
|
+
|
|
69
|
+
export type AllItemTypeArrays<
|
|
70
|
+
S extends string,
|
|
71
|
+
L1 extends string = never,
|
|
72
|
+
L2 extends string = never,
|
|
73
|
+
L3 extends string = never,
|
|
74
|
+
L4 extends string = never,
|
|
75
|
+
L5 extends string = never,
|
|
76
|
+
> = readonly [S] |
|
|
77
|
+
readonly [S, L1] |
|
|
78
|
+
readonly [S, L1, L2] |
|
|
79
|
+
readonly [S, L1, L2, L3] |
|
|
80
|
+
readonly [S, L1, L2, L3, L4] |
|
|
81
|
+
readonly [S, L1, L2, L3, L4, L5];
|
|
82
|
+
|
|
83
|
+
export type AllLocTypeArrays<
|
|
84
|
+
L1 extends string = never,
|
|
85
|
+
L2 extends string = never,
|
|
86
|
+
L3 extends string = never,
|
|
87
|
+
L4 extends string = never,
|
|
88
|
+
L5 extends string = never,
|
|
89
|
+
> = readonly [] |
|
|
90
|
+
readonly [L1] |
|
|
91
|
+
readonly [L1, L2] |
|
|
92
|
+
readonly [L1, L2, L3] |
|
|
93
|
+
readonly [L1, L2, L3, L4] |
|
|
94
|
+
readonly [L1, L2, L3, L4, L5];
|
|
95
|
+
|