Skip to content

Example solution for Advanced Filtering Challenge #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/advanced-filtering/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
LogSourceMap,
} from './constants';
import {
advancedFilter,
advancedSort,
NO_FILTER,
simpleLogFilter,
} from './services/logProcessing';
import {
FilterCriteria,
LogEntry,
LogLevel,
SimpleLogFilterCriteria,
SortCriteria,
UpdateLogFilterFunction,
} from './types';
Expand All @@ -29,9 +29,9 @@ function LogDashboard({ logs }: LogDashboardProps) {
const [sortCriteria, setSortCriteria] = useState<SortCriteria<LogEntry>>({
timestamp: { direction: 'desc' },
});
const [filterCriteria, setFilterCriteria] = useState<SimpleLogFilterCriteria>(
{},
);
const [filterCriteria, setFilterCriteria] = useState<
FilterCriteria<LogEntry>
>({});

const compareLogLevels = (a: LogLevel, b: LogLevel) =>
LogLevelOrder[a] - LogLevelOrder[b];
Expand All @@ -58,7 +58,7 @@ function LogDashboard({ logs }: LogDashboardProps) {
};

const filteredAndSortedLogs = useMemo(() => {
const filteredLogs = simpleLogFilter(logs, filterCriteria);
const filteredLogs = advancedFilter(logs, filterCriteria);
return advancedSort(filteredLogs, sortCriteria);
}, [logs, filterCriteria, sortCriteria]);

Expand Down
10 changes: 5 additions & 5 deletions src/advanced-filtering/src/components/LogFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
SimpleFilterableLogValue,
SimpleLogFilterKey,
FilterableLogValue,
LogFilterKey,
UpdateLogFilterFunction,
} from '../types';

Expand All @@ -18,8 +18,8 @@ const SelectArrow = () => (

interface LogFilterProps {
updateFilter: UpdateLogFilterFunction;
filterKey: SimpleLogFilterKey;
options: readonly SimpleFilterableLogValue[];
filterKey: LogFilterKey;
options: readonly FilterableLogValue[];
allLabel: string;
}

Expand All @@ -32,7 +32,7 @@ export const LogFilter = ({
<div className="relative inline-block w-full">
<select
onChange={(e) =>
updateFilter(filterKey, e.target.value as SimpleFilterableLogValue | '')
updateFilter(filterKey, e.target.value as FilterableLogValue | '')
}
className="w-full px-4 py-2 pr-10 text-gray-700 bg-white border rounded-lg appearance-none focus:outline-none focus:ring-2 focus:ring-blue-500"
>
Expand Down
23 changes: 13 additions & 10 deletions src/advanced-filtering/src/services/logProcessing.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { LogEntry, SimpleLogFilterCriteria, SortCriteria } from '../types';
import { FilterCriteria, SortCriteria } from '../types';

export function advancedSort<ItemType extends object>(
arr: ItemType[],
criteria: SortCriteria<ItemType>,
criteria: SortCriteria<ItemType>
): ItemType[] {
return [...arr].sort((a, b) => {
for (const key in criteria) {
Expand All @@ -23,20 +23,23 @@ export function advancedSort<ItemType extends object>(

export const NO_FILTER = null;

export function simpleLogFilter(
logs: any[],
criteria: SimpleLogFilterCriteria,
): any[] {
return logs.filter((log) => {
export function advancedFilter<ItemType extends object>(
arr: ItemType[],
criteria: FilterCriteria<ItemType>
): ItemType[] {
return arr.filter((el) => {
return (
Object.entries(criteria) as [keyof LogEntry, SimpleLogFilterCriteria][]
Object.entries(criteria) as [
keyof ItemType,
FilterCriteria<ItemType>[keyof ItemType]
][]
).every(([key, condition]) => {
if (condition === NO_FILTER) {
return true;
}
const value = log[key];
const value = el[key];
if (typeof condition === 'function') {
return (condition as (value: any) => boolean)(value);
return condition(value);
}
return value === condition;
});
Expand Down
15 changes: 7 additions & 8 deletions src/advanced-filtering/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface LogEntry {

export type CompareFunction<ItemTypeValue> = (
a: ItemTypeValue,
b: ItemTypeValue,
b: ItemTypeValue
) => number;

export type SortCriteria<ItemType extends object> = {
Expand All @@ -26,16 +26,15 @@ export type SortCriteria<ItemType extends object> = {
};
};

export type SimpleLogFilterCriteria = {
level?: LogLevel | ((value: LogLevel) => boolean);
source?: LogSource | ((value: LogSource) => boolean);
export type FilterCriteria<ItemType> = {
[Key in keyof ItemType]?: ItemType[Key] | ((value: ItemType[Key]) => boolean);
};

export type SimpleLogFilterKey = 'level' | 'source';
export type LogFilterKey = Extract<keyof LogEntry, 'level' | 'source'>;

export type SimpleFilterableLogValue = LogLevel | LogSource;
export type FilterableLogValue = LogEntry[LogFilterKey];

export type UpdateLogFilterFunction = (
key: SimpleLogFilterKey,
value: SimpleFilterableLogValue | '',
key: LogFilterKey,
value: FilterableLogValue | ''
) => void;