Created
June 10, 2025 18:09
-
-
Save toky-nomena/e3b3f63d958e84b81e525cf090bd968d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { LookupValue } from '@diqiq/policy-auto-ts-models'; | |
| import { LookupOption } from '@eisgroup/react-components'; | |
| interface VehicleFilters { | |
| modelYear?: string[]; | |
| make?: string[]; | |
| } | |
| interface ExtendedLookupValue extends LookupValue { | |
| [key: string]: unknown; | |
| } | |
| interface VehicleLookupOption extends LookupOption { | |
| readonly lookupValue: ExtendedLookupValue; | |
| readonly filters?: VehicleFilters; | |
| } | |
| // Utility function to check if a filter array includes the target value | |
| const matchesFilter = ( | |
| filterArray: string[] | undefined, | |
| targetValue: string | |
| ): boolean => { | |
| return filterArray?.includes(targetValue) ?? false; | |
| }; | |
| // Utility function to get filter value from either filters or lookupValue | |
| const getFilterValue = ( | |
| option: VehicleLookupOption, | |
| filterKey: keyof VehicleFilters | |
| ): string[] | undefined => { | |
| // First check the dedicated filters attribute | |
| const filtersValue = option.filters?.[filterKey]; | |
| if (filtersValue) return filtersValue; | |
| // Fallback to lookupValue with type-safe access | |
| const lookupValue = option.lookupValue[filterKey]; | |
| return Array.isArray(lookupValue) ? lookupValue as string[] : undefined; | |
| }; | |
| export const filterVehicleByModelYear = ( | |
| lookup: VehicleLookupOption, | |
| modelYear: string | |
| ): boolean => { | |
| const modelYearFilter = getFilterValue(lookup, 'modelYear'); | |
| return matchesFilter(modelYearFilter, modelYear); | |
| }; | |
| export const filterVehicleByMake = ( | |
| lookup: VehicleLookupOption, | |
| make: string | |
| ): boolean => { | |
| const makeFilter = getFilterValue(lookup, 'make'); | |
| return matchesFilter(makeFilter, make); | |
| }; | |
| export const filterVehicleModelLookups = ( | |
| lookup: VehicleLookupOption, | |
| modelYear: string, | |
| make: string | |
| ): boolean => { | |
| return filterVehicleByModelYear(lookup, modelYear) && | |
| filterVehicleByMake(lookup, make); | |
| }; | |
| // Alternative: More flexible filter function that accepts multiple criteria | |
| export const filterVehicleBy = ( | |
| lookup: VehicleLookupOption, | |
| criteria: Partial<{ modelYear: string; make: string }> | |
| ): boolean => { | |
| const checks = []; | |
| if (criteria.modelYear) { | |
| checks.push(filterVehicleByModelYear(lookup, criteria.modelYear)); | |
| } | |
| if (criteria.make) { | |
| checks.push(filterVehicleByMake(lookup, criteria.make)); | |
| } | |
| return checks.length === 0 || checks.every(Boolean); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import {
filterVehicleByMake,
filterVehicleByModelYear,
filterVehicleModelLookups
} from './vehicleFilters'; // replace with the actual path
type LookupValue = Record<string, any>;
interface LookupOption {
label: string;
value: string;
}
interface VehicleFilters {
modelYear?: string[];
make?: string[];
}
interface ExtendedLookupValue extends LookupValue {
[key: string]: unknown;
}
interface VehicleLookupOption extends LookupOption {
readonly lookupValue: ExtendedLookupValue;
readonly filters?: VehicleFilters;
}
describe('Vehicle Filter Utilities', () => {
const baseLookup: VehicleLookupOption = {
label: 'Honda Civic 2023',
value: 'civic-2023',
lookupValue: {
make: ['Honda'],
modelYear: ['2023'],
}
};
it('should match modelYear from lookupValue', () => {
const result = filterVehicleByModelYear(baseLookup, '2023');
expect(result).toBe(true);
});
it('should not match modelYear when different', () => {
const result = filterVehicleByModelYear(baseLookup, '2022');
expect(result).toBe(false);
});
it('should match make from lookupValue', () => {
const result = filterVehicleByMake(baseLookup, 'Honda');
expect(result).toBe(true);
});
it('should not match make when different', () => {
const result = filterVehicleByMake(baseLookup, 'Toyota');
expect(result).toBe(false);
});
it('should match both make and modelYear from lookupValue', () => {
const result = filterVehicleModelLookups(baseLookup, '2023', 'Honda');
expect(result).toBe(true);
});
it('should not match both make and modelYear if one mismatches', () => {
const result = filterVehicleModelLookups(baseLookup, '2022', 'Honda');
expect(result).toBe(false);
});
it('should match make and modelYear from
filtersproperty', () => {const lookupWithFilters: VehicleLookupOption = {
label: 'Toyota Corolla 2024',
value: 'corolla-2024',
lookupValue: {},
filters: {
make: ['Toyota'],
modelYear: ['2024'],
}
};
});
it('should fallback to lookupValue if filters are undefined', () => {
const result = filterVehicleModelLookups(baseLookup, '2023', 'Honda');
expect(result).toBe(true);
});
it('should return false when neither filters nor lookupValue contains valid data', () => {
const emptyLookup: VehicleLookupOption = {
label: 'Empty',
value: 'empty',
lookupValue: {}
};
});
});