|
| 1 | + |
| 2 | +type keyOfRandom = ( |
| 3 | + 'a' | 'b' | 'c' | 'd' | 'e' | |
| 4 | + 'f' | 'g' | 'h' | 'i' | 'j' | |
| 5 | + 'k' | 'l' | 'm' | 'n' |
| 6 | +) |
| 7 | + |
| 8 | +export interface id { |
| 9 | + |
| 10 | + /** |
| 11 | + * Generates a random UUID |
| 12 | + */ |
| 13 | + generate(): string |
| 14 | + |
| 15 | + criteria <U extends string>(uuid: U): { |
| 16 | + $id: U, |
| 17 | + random: Record<keyOfRandom, number> |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +type KeyValsAsType<T> = { |
| 22 | + [ |
| 23 | + K in keyof T as T[K] extends string ? ( |
| 24 | + string extends T[K] ? never : T[K] |
| 25 | + ) : never |
| 26 | + ]?: AllowedValues<T> |
| 27 | +}; |
| 28 | + |
| 29 | +type AllowedValues<Criteria> = ( |
| 30 | + number | string | boolean | |
| 31 | + Array<any> | typeof Function | |
| 32 | + Schema<Criteria> |
| 33 | +); |
| 34 | + |
| 35 | +type Schema<Criteria> = { |
| 36 | + $param: keyof Criteria, |
| 37 | + $value?: AllowedValues<Criteria> |
| 38 | + $replace?: 'true' | true, |
| 39 | + $env?: keyof Criteria |
| 40 | + $coerce?: 'number' | 'array' | 'boolean' | 'object' |
| 41 | + $splitToken?: string | RegExp |
| 42 | + $filter?: keyof Criteria | { $env: keyof NodeJS.ProcessEnv }, |
| 43 | + $base?: AllowedValues<Criteria>, |
| 44 | + $default?: AllowedValues<Criteria> |
| 45 | + $id?: string, |
| 46 | + $range?: { |
| 47 | + limit: number, |
| 48 | + value: AllowedValues<Criteria>, |
| 49 | + id: string |
| 50 | + }[] |
| 51 | + $meta?: object | string, |
| 52 | +} | KeyValsAsType<Criteria> |
| 53 | + |
| 54 | +type ValueOf<T> = T[keyof T]; |
| 55 | + |
| 56 | +type FilterType<C, T extends string> = { |
| 57 | + $filter?: C, |
| 58 | +} | Record<T, string> |
| 59 | + |
| 60 | +type InferFromObject<T, K extends keyof T = keyof T> = T[K] extends infer C ? C : T[K]; |
| 61 | + |
| 62 | +/** |
| 63 | + * |
| 64 | + */ |
| 65 | +type ConfidenceStore< |
| 66 | + Criteria, |
| 67 | + ReturnType |
| 68 | +> = ( |
| 69 | + { |
| 70 | + [K in keyof ReturnType]: ConfidenceStore< |
| 71 | + Criteria, |
| 72 | + ReturnType[K] |
| 73 | + > | Schema<Criteria> |
| 74 | + } | |
| 75 | + { |
| 76 | + [K in keyof Schema<Criteria>]: ( |
| 77 | + ConfidenceStore<Criteria, ReturnType> | |
| 78 | + KeyValsAsType<Criteria> |
| 79 | + ) |
| 80 | + } |
| 81 | +); |
| 82 | + |
| 83 | +/** |
| 84 | + * Finds the '/' slash path in a type starting from the |
| 85 | + * beginning. This type appends the `/` to the root level |
| 86 | + * of the types and then calls the `PathImplMid` type for |
| 87 | + * the middle paths. The separation allows us to guess |
| 88 | + * that `{ a: { b: { c: true }}}` is the path `/a/b/c` |
| 89 | + * and not `/a//b//c`. |
| 90 | + */ |
| 91 | +type PathImplementation<T, K extends keyof T> = ( |
| 92 | + K extends string ? ( |
| 93 | + T[K] extends Record<string, any> ? ( |
| 94 | + T[K] extends ArrayLike<any> ? ( |
| 95 | + `/${K}` | `/${K}/${PathImplMid<T[K], Exclude<keyof T[K], keyof any[]>>}` |
| 96 | + ) : ( |
| 97 | + `/${K}` | `/${K}/${PathImplMid<T[K], keyof T[K]>}` |
| 98 | + ) |
| 99 | + ) : `/${K}` |
| 100 | + ) : never |
| 101 | +); |
| 102 | + |
| 103 | +/** |
| 104 | + * This generates the mid-path slash commands for `PathImplementation` |
| 105 | + */ |
| 106 | +type PathImplMid<T, K extends keyof T> = ( |
| 107 | + K extends string ? ( |
| 108 | + T[K] extends Record<string, any> ? ( |
| 109 | + T[K] extends ArrayLike<any> ? ( |
| 110 | + K | `${K}/${PathImplMid<T[K], Exclude<keyof T[K], keyof any[]>>}` |
| 111 | + ) : ( |
| 112 | + K | `${K}/${PathImplMid<T[K], keyof T[K]>}` |
| 113 | + ) |
| 114 | + ) : K |
| 115 | + ) : never |
| 116 | +); |
| 117 | + |
| 118 | +// The actual path implementation |
| 119 | +type Path<T> = '/' | PathImplementation<T, keyof T>; |
| 120 | + |
| 121 | +type PathValueImp<T, P extends Path<T>> = P extends `/${infer UKey}` ? ( |
| 122 | + P extends `/${infer LKey}/${infer Rest}` ? ( |
| 123 | + LKey extends keyof T ? ( |
| 124 | + Rest extends keyof T[LKey] ? ( |
| 125 | + T[LKey][Rest] |
| 126 | + ) : ( |
| 127 | + `/${Rest}` extends Path<T[LKey]> ? ( |
| 128 | + PathValueImp<T[LKey], `/${Rest}`> |
| 129 | + ) : never |
| 130 | + ) |
| 131 | + ) : never |
| 132 | + ) : ( |
| 133 | + UKey extends keyof T ? T[UKey] : never |
| 134 | + ) |
| 135 | +) : never |
| 136 | + |
| 137 | +type PathValue<T, P extends Path<T>> = P extends '/' ? T : PathValueImp<T, P> |
| 138 | + |
| 139 | +export class Store<Criteria, ReturnType> { |
| 140 | + |
| 141 | + constructor (document: ConfidenceStore<Criteria, ReturnType>); |
| 142 | + |
| 143 | + load(document: ConfidenceStore<Criteria, ReturnType>): void; |
| 144 | + |
| 145 | + get <K extends Path<ReturnType>> (key: K, criteria: Criteria): PathValue<ReturnType, K> |
| 146 | + |
| 147 | + meta <K extends Path<ReturnType> = '/'>(key: K, criteria: Criteria): any; |
| 148 | + |
| 149 | + static validate <T extends ConfidenceStore<any, any>>(node: T): Error | null; |
| 150 | + |
| 151 | +} |
0 commit comments