|
1 | | -import { get } from './utils' |
| 1 | +import { get, PropertyPath } from './utils' |
2 | 2 | import { CatalogComponents } from './types' |
3 | 3 |
|
4 | | -export interface ICatalog<T extends CatalogComponents = CatalogComponents> { |
| 4 | +export interface ICatalog<T extends CatalogComponents> { |
5 | 5 | // contains the raw catalog |
6 | | - _catalog: T | Record<string, any> |
7 | | - // get a component by id, if not available it will return null |
8 | | - getComponent: (component: string) => any |
9 | | - // validates if the given component exists in the catalog |
10 | | - hasComponent: (component: string) => boolean |
| 6 | + _catalog: T |
| 7 | + /** |
| 8 | + * Get a Component by it's path in the given catalog. If it is not available, |
| 9 | + * getComponent will return undefined |
| 10 | + * |
| 11 | + * @example |
| 12 | + * ``` |
| 13 | + * const Button = catalog.getComponent('button'); |
| 14 | + * const Button = catalog.getComponent(['button']); |
| 15 | + * |
| 16 | + * // or, if the components in the catalog are nested |
| 17 | + * const Button = catalog.getComponent("common.button") |
| 18 | + * const Button = catalog.getComponent(["common", "button"]) |
| 19 | + * ``` |
| 20 | + * |
| 21 | + * types are inspired by |
| 22 | + * @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/7caeca4bfbd5ca9f306c14def3dd6b416869c615/types/lodash/common/object.d.ts#L1669 |
| 23 | + * @see https://codewithstyle.info/Deep-property-access-in-TypeScript/ |
| 24 | + * |
| 25 | + * Playground |
| 26 | + * @see https://nttr.st/2xu77eG |
| 27 | + * |
| 28 | + * Additional references: |
| 29 | + * @see https://github.com/pirix-gh/ts-toolbelt |
| 30 | + * @see https://stackoverflow.com/q/47256723/1238150 |
| 31 | + * @see https://stackoverflow.com/a/58436959/1238150 |
| 32 | + */ |
| 33 | + getComponent<TKey extends keyof NonNullable<T>>( |
| 34 | + component: TKey | [TKey], |
| 35 | + ): NonNullable<T>[TKey] | undefined |
| 36 | + getComponent< |
| 37 | + TKey1 extends keyof NonNullable<T>, |
| 38 | + TKey2 extends keyof NonNullable<T>[TKey1] |
| 39 | + >( |
| 40 | + component: [TKey1, TKey2], |
| 41 | + ): NonNullable<T>[TKey1][TKey2] | undefined |
| 42 | + getComponent< |
| 43 | + TKey1 extends keyof NonNullable<T>, |
| 44 | + TKey2 extends keyof NonNullable<T>[TKey1], |
| 45 | + TKey3 extends keyof NonNullable<T>[TKey1][TKey2] |
| 46 | + >( |
| 47 | + component: [TKey1, TKey2, TKey3], |
| 48 | + ): NonNullable<T>[TKey1][TKey2][TKey3] | undefined |
| 49 | + getComponent< |
| 50 | + TKey1 extends keyof NonNullable<T>, |
| 51 | + TKey2 extends keyof NonNullable<T>[TKey1], |
| 52 | + TKey3 extends keyof NonNullable<T>[TKey1][TKey2], |
| 53 | + TKey4 extends keyof NonNullable<T>[TKey1][TKey2][TKey3] |
| 54 | + >( |
| 55 | + component: [TKey1, TKey2, TKey3, TKey4], |
| 56 | + ): NonNullable<T>[TKey1][TKey2][TKey3][TKey4] | undefined |
| 57 | + getComponent(component: PropertyPath): any |
| 58 | + /** |
| 59 | + * validates if the given component exists in the catalog |
| 60 | + * |
| 61 | + * @example |
| 62 | + * ``` |
| 63 | + * const hasButton = catalog.hasComponent('button'); |
| 64 | + * const hasButton = catalog.hasComponent(['button']); |
| 65 | + * |
| 66 | + * // or, if the components in the catalog are nested |
| 67 | + * const hasButton = catalog.hasComponent("common.button") |
| 68 | + * const hasButton = catalog.hasComponent(["common", "button"]) |
| 69 | + * ``` |
| 70 | + */ |
| 71 | + hasComponent<TKey extends keyof NonNullable<T>>( |
| 72 | + component: TKey | [TKey], |
| 73 | + ): boolean |
| 74 | + hasComponent< |
| 75 | + TKey1 extends keyof NonNullable<T>, |
| 76 | + TKey2 extends keyof NonNullable<T>[TKey1] |
| 77 | + >( |
| 78 | + component: [TKey1, TKey2], |
| 79 | + ): boolean |
| 80 | + hasComponent< |
| 81 | + TKey1 extends keyof NonNullable<T>, |
| 82 | + TKey2 extends keyof NonNullable<T>[TKey1], |
| 83 | + TKey3 extends keyof NonNullable<T>[TKey1][TKey2] |
| 84 | + >( |
| 85 | + component: [TKey1, TKey2, TKey3], |
| 86 | + ): boolean |
| 87 | + hasComponent< |
| 88 | + TKey1 extends keyof NonNullable<T>, |
| 89 | + TKey2 extends keyof NonNullable<T>[TKey1], |
| 90 | + TKey3 extends keyof NonNullable<T>[TKey1][TKey2], |
| 91 | + TKey4 extends keyof NonNullable<T>[TKey1][TKey2][TKey3] |
| 92 | + >( |
| 93 | + component: [TKey1, TKey2, TKey3, TKey4], |
| 94 | + ): boolean |
| 95 | + hasComponent(component: PropertyPath): boolean |
11 | 96 | } |
12 | 97 |
|
13 | | -export class Catalog<T extends CatalogComponents = CatalogComponents> |
14 | | - implements ICatalog<T> { |
| 98 | +export class Catalog<T extends CatalogComponents> implements ICatalog<T> { |
15 | 99 | public _catalog: T |
16 | 100 |
|
17 | 101 | constructor(catalog: T) { |
18 | 102 | this._catalog = catalog |
19 | 103 | } |
20 | 104 |
|
21 | | - public getComponent: ICatalog<T>['getComponent'] = component => |
| 105 | + public getComponent: ICatalog<T>['getComponent'] = (component: any) => |
22 | 106 | get(this._catalog, component) |
23 | 107 |
|
24 | | - public hasComponent: ICatalog<T>['hasComponent'] = component => |
| 108 | + public hasComponent: ICatalog<T>['hasComponent'] = (component: any) => |
25 | 109 | !!get(this._catalog, component) |
26 | 110 | } |
27 | 111 |
|
|
0 commit comments