Skip to content

Commit

Permalink
fix(components): add missing utils
Browse files Browse the repository at this point in the history
Signed-off-by: Antonette Caldwell <[email protected]>
  • Loading branch information
nebula-aac committed Feb 3, 2024
1 parent db9832c commit 8d45977
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/custom/TypingFilter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
97 changes: 97 additions & 0 deletions packages/components/src/utils/typing.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { FilterSchema, Delimiter, FilterStateType } from "./typing.state";

export type Filters = Record<string, string[] | undefined>;

/**
* 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,
};
};

0 comments on commit 8d45977

Please sign in to comment.