|
| 1 | +import { Category } from "../models/types"; |
| 2 | +import { VIEW_KIND } from "../views/types"; |
| 3 | +import { CategoryConfig } from "./types"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Returns the category config for the given category config key and view kind. |
| 7 | + * @param viewKind - View kind. |
| 8 | + * @param key - Category config key. |
| 9 | + * @param configs - Category configs. |
| 10 | + * @returns category config. |
| 11 | + */ |
| 12 | +export function findCategoryConfig<V extends VIEW_KIND>( |
| 13 | + viewKind: V, |
| 14 | + key: Category["key"], |
| 15 | + configs: CategoryConfig[] |
| 16 | +): Extract<CategoryConfig, { viewKind: V }> | undefined { |
| 17 | + return configs.find( |
| 18 | + (c): c is Extract<CategoryConfig, { viewKind: V }> => |
| 19 | + c.viewKind === viewKind && c.key === key |
| 20 | + ); |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Returns the range category config for the given category config key. |
| 25 | + * @param key - Category config key. |
| 26 | + * @param configs - Category configs. |
| 27 | + * @returns category config. |
| 28 | + */ |
| 29 | +export function findRangeCategoryConfig( |
| 30 | + key: Category["key"], |
| 31 | + configs: CategoryConfig[] |
| 32 | +): Extract<CategoryConfig, { viewKind: VIEW_KIND.RANGE }> | undefined { |
| 33 | + return findCategoryConfig(VIEW_KIND.RANGE, key, configs); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Returns the select category config for the given category config key. |
| 38 | + * @param key - Category config key. |
| 39 | + * @param configs - Category configs. |
| 40 | + * @returns category config. |
| 41 | + */ |
| 42 | +export function findSelectCategoryConfig( |
| 43 | + key: Category["key"], |
| 44 | + configs: CategoryConfig[] |
| 45 | +): Extract<CategoryConfig, { viewKind?: VIEW_KIND.SELECT }> | undefined { |
| 46 | + return findCategoryConfig(VIEW_KIND.SELECT, key, configs); |
| 47 | +} |
0 commit comments