diff --git a/packages/components/src/custom/TypingFilter/TypingFIlters.tsx b/packages/components/src/custom/TypingFilter/TypingFIlters.tsx index 0dd44e57..b19834b3 100644 --- a/packages/components/src/custom/TypingFilter/TypingFIlters.tsx +++ b/packages/components/src/custom/TypingFilter/TypingFIlters.tsx @@ -4,6 +4,7 @@ import { List } from '../../base/List'; import { ListItem } from '../../base/ListItem'; import { Typography } from '../../base/Typography'; import { FilterSchema, FilterStateType, FilteringEvents } from '../../utils/typing.state'; +import { getCurrentFilterAndValue } from '../../utils/typing.utils'; interface TypingFiltersType { filterStateMachine: FilterStateType; diff --git a/packages/components/src/custom/TypingFilter/TypingFilterSuggestions.tsx b/packages/components/src/custom/TypingFilter/TypingFilterSuggestions.tsx index fdd93488..2cfe0386 100644 --- a/packages/components/src/custom/TypingFilter/TypingFilterSuggestions.tsx +++ b/packages/components/src/custom/TypingFilter/TypingFilterSuggestions.tsx @@ -4,6 +4,7 @@ import { List } from '../../base/List'; import { ListItem } from '../../base/ListItem'; import { Typography } from '../../base/Typography'; import { FilterSchema, FilterStateType, FilteringEvents } from '../../utils/typing.state'; +import { getCurrentFilterAndValue } from '../../utils/typing.utils'; interface TypingFilterValueSuggestionsType { filterStateMachine: FilterStateType; diff --git a/packages/components/src/custom/TypingFilter/index.tsx b/packages/components/src/custom/TypingFilter/index.tsx index e061704f..262877a9 100644 --- a/packages/components/src/custom/TypingFilter/index.tsx +++ b/packages/components/src/custom/TypingFilter/index.tsx @@ -13,6 +13,7 @@ import { import TypingFilterInput from './TypingFIlterInput'; import { TypingFilters } from './TypingFIlters'; import { TypingFilterValueSuggestions } from './TypingFilterSuggestions'; +import { getFilters } from '../../utils/typing.utils'; interface TypingFilterType { filterSchema: FilterSchema; diff --git a/packages/components/src/utils/typing.utils.ts b/packages/components/src/utils/typing.utils.ts new file mode 100644 index 00000000..faee7947 --- /dev/null +++ b/packages/components/src/utils/typing.utils.ts @@ -0,0 +1,97 @@ +import { FilterSchema, Delimiter, FilterStateType } from "./typing.state"; + +export type Filters = Record; + + /** + * Returns the filter object from the filterSchema + * + * @param value + * @param filterSchema + * @returns + */ + export const getFilterByValue = ( + value: string, + filterSchema: FilterSchema, + ): { value: string; multiple: boolean } | undefined => { + const matchingFilterKey = Object.keys(filterSchema).find( + (key) => filterSchema[key].value === value, + ); + + if (matchingFilterKey) { + const matchingFilter = filterSchema[matchingFilterKey]; + return { + value: matchingFilter.value ?? "", + multiple: !!matchingFilter.multiple, + }; + } + + return undefined; + }; + + /** + * Parses a filter string and returns a filter object + * + * @param filterString - The input filter string of the form "type:value type2:value2 type:value2" + * @param filterSchema - The filter object with types as keys and arrays of values as values + * @returns + */ + export const getFilters = ( + filterString: string, + filterSchema: FilterSchema, + ): Filters => { + const filters: Filters = {}; + + const filterValuePairs = filterString.split(Delimiter.FILTER); + + filterValuePairs.forEach((filterValuePair) => { + const [filter, value] = filterValuePair.split(Delimiter.FILTER_VALUE); + + const schemaEntry = filterSchema[filter]; + + if (schemaEntry && schemaEntry.multiple) { + filters[filter] = filters[filter] ?? []; + filters[filter]!.push(value); // Using non-null assertion + } else { + filters[filter] = [value]; // Treat as an array + } + }); + + return filters; + }; + + /** + * Returns a filter string of form "type:value type2:value2 type:value2" from + * a filter object of { type: { values }, type2: { values } } + * + * @param filters + * @returns + */ + export const getFilterString = (filters: FilterSchema) => { + return Object.entries(filters).reduce((filterString, [filter, values]) => { + const valuesArray = values?.values ?? []; + const filterValuesString = valuesArray + .map((value) => `${filter}${Delimiter.FILTER_VALUE}${value}`) + .join(" "); + + return filterString + filterValuesString; + }, ""); + }; + + /** + * + * @param filteringState + * @returns + */ + export const getCurrentFilterAndValue = (filteringState: FilterStateType) => { + const { context } = filteringState; + const currentFilterValue = context?.value?.split(Delimiter.FILTER).at(-1); + const currentFilter = + currentFilterValue?.split(Delimiter.FILTER_VALUE)?.[0] ?? ""; + const currentValue = + currentFilterValue?.split(Delimiter.FILTER_VALUE)?.[1] ?? ""; + return { + filter: currentFilter, + value: currentValue, + }; + }; + \ No newline at end of file