Skip to content

Commit

Permalink
Tests. Rename field Tag -> Tags. Remove unused is_one_of and is_not_o…
Browse files Browse the repository at this point in the history
…ne_of operator.
  • Loading branch information
AaronPlave committed Dec 20, 2024
1 parent adb43c8 commit 5374a7b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
import { tags } from '../../../../stores/tags';
import type { ValueSchemaVariant } from '../../../../types/schema';
import type { ActivityLayerFilter, ActivityLayerFilterSubfieldSchema } from '../../../../types/timeline';
import { compare, getTarget } from '../../../../utilities/generic';
import { compare, getTarget, lowercase } from '../../../../utilities/generic';
import {
applyActivityLayerFilter,
getMatchingTypesForActivityLayerFilter,
getNextThingID,
lowercase,
} from '../../../../utilities/timeline';
import { tooltip } from '../../../../utilities/tooltip';
import Input from '../../../form/Input.svelte';
Expand Down Expand Up @@ -99,7 +98,7 @@
}
function onAddDynamicFilter(list: 'dynamic_type_filters' | 'global_filters') {
const field = list === 'dynamic_type_filters' ? 'Type' : 'Tag';
const field = list === 'dynamic_type_filters' ? 'Type' : 'Tags';
const listObj = dirtyFilter[list] || [];
const currentFilters = Array.isArray(listObj) ? listObj : [];
const id = getNextThingID(listObj);
Expand Down Expand Up @@ -487,7 +486,7 @@
<div class="filter-section-header st-typography-medium">
<div class="filter-section-title">
Global Filters
<div class="hint st-typography-body">Tag, parameter, scheduling goal, etc...</div>
<div class="hint st-typography-body">Tags, parameter, scheduling goal, etc...</div>
</div>
<button
class="st-button icon"
Expand Down Expand Up @@ -521,7 +520,7 @@
equals: { type: 'int' },
},

Tag: {
Tags: {
does_not_include: { type: 'tag', values: $tags },
includes: { type: 'tag', values: $tags },
},
Expand Down
2 changes: 0 additions & 2 deletions src/schemas/ui-view-schema-v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
{ "const": "does_not_equal" },
{ "const": "includes" },
{ "const": "does_not_include" },
{ "const": "is_one_of" },
{ "const": "is_not_one_of" },
{ "const": "is_greater_than" },
{ "const": "is_less_than" },
{ "const": "is_within" },
Expand Down
8 changes: 3 additions & 5 deletions src/types/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export enum ActivityLayerFilterField {
'Type' = 'Type',
'Name' = 'Name',
'Subsystem' = 'Subsystem',
'Tag' = 'Tag',
'Tags' = 'Tags',
'Parameter' = 'Parameter',
'SchedulingGoalId' = 'Scheduling Goal Id',
}
Expand All @@ -45,13 +45,13 @@ export type DynamicFilterDataType = ValueSchema['type'] | 'tag';
export type ActivityLayerFilter = {
dynamic_type_filters?: ActivityLayerDynamicFilter<Pick<typeof ActivityLayerFilterField, 'Type' | 'Subsystem'>>[];
global_filters?: ActivityLayerDynamicFilter<
Pick<typeof ActivityLayerFilterField, 'Tag' | 'Parameter' | 'SchedulingGoalId' | 'Name'>
Pick<typeof ActivityLayerFilterField, 'Tags' | 'Parameter' | 'SchedulingGoalId' | 'Name'>
>[];
static_types?: string[];
type_subfilters?: Record<
string,
ActivityLayerDynamicFilter<
Pick<typeof ActivityLayerFilterField, 'Tag' | 'Parameter' | 'SchedulingGoalId' | 'Name'>
Pick<typeof ActivityLayerFilterField, 'Tags' | 'Parameter' | 'SchedulingGoalId' | 'Name'>
>[]
>;
};
Expand Down Expand Up @@ -80,8 +80,6 @@ export enum FilterOperator {
'does_not_equal' = 'does not equal',
'includes' = 'includes',
'does_not_include' = 'does not include',
'is_one_of' = 'is one of',
'is_not_one_of' = 'is not one of',
'is_greater_than' = 'is greater than',
'is_less_than' = 'is less than',
'is_within' = 'is within',
Expand Down
6 changes: 6 additions & 0 deletions src/utilities/generic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
filterEmpty,
getSearchParameterNumber,
isMacOs,
lowercase,
parseJSONStream,
unique,
} from './generic';
Expand Down Expand Up @@ -190,4 +191,9 @@ describe('Generic utility function tests', () => {
expect(await parseJSONStream(readable as unknown as ReadableStream)).toBeTypeOf('object');
});
});

describe('lowercase', () => {
expect(lowercase([]).to.deep.eq([]));
expect(lowercase('ABC').to.eq('abc'));
});
});
7 changes: 7 additions & 0 deletions src/utilities/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,10 @@ export async function parseJSONStream<R>(jsonStream: ReadableStream): Promise<R>
}
});
}

/**
* Lowercase the unknown value if it is a string
*/
export function lowercase(value: any) {
return typeof value === 'string' ? value.toLowerCase() : value;
}
24 changes: 23 additions & 1 deletion src/utilities/timeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
isExternalEventLayer,
isLineLayer,
isXRangeLayer,
matchesDynamicFilter,
paginateNodes,
spanInView,
} from './timeline';
Expand Down Expand Up @@ -1568,7 +1569,7 @@ describe('applyActivityLayerFilter', () => {
{
global_filters: [
{
field: 'Tag',
field: 'Tags',
id: 1,
operator: 'includes',
value: [1],
Expand All @@ -1586,3 +1587,24 @@ describe('applyActivityLayerFilter', () => {
});
});
});

test.only('matchesDynamicFilter', () => {
expect(matchesDynamicFilter('Foo', 'equals', 'Foo')).toBeTruthy();
expect(matchesDynamicFilter('Foo', 'does_not_equal', 'Bar')).toBeTruthy();
expect(matchesDynamicFilter('Foo', 'includes', '')).toBeFalsy();
expect(matchesDynamicFilter('Foo', 'includes', 'oo')).toBeTruthy();
expect(matchesDynamicFilter([], 'includes', [1])).toBeFalsy();
expect(matchesDynamicFilter([1, 2, 3], 'includes', [1])).toBeTruthy();
expect(matchesDynamicFilter('Foo', 'does_not_include', '')).toBeTruthy();
expect(matchesDynamicFilter('Foo', 'does_not_include', 'oo')).toBeFalsy();
expect(matchesDynamicFilter([], 'does_not_include', [1])).toBeTruthy();
expect(matchesDynamicFilter([1, 2, 3], 'does_not_include', [1])).toBeFalsy();
expect(matchesDynamicFilter(2, 'is_greater_than', 1)).toBeTruthy();
expect(matchesDynamicFilter('2', 'is_greater_than', '1')).toBeTruthy();
expect(matchesDynamicFilter(2, 'is_less_than', 1)).toBeFalsy();
expect(matchesDynamicFilter('2', 'is_less_than', '1')).toBeFalsy();
expect(matchesDynamicFilter(2, 'is_within', [1, 3])).toBeTruthy();
expect(matchesDynamicFilter(2, 'is_not_within', [1, 3])).toBeFalsy();
// @ts-expect-error forcing the case where an invalid operator is specified
expect(matchesDynamicFilter(2, 'is_definitely_somewhere_near', [1, 3])).toBeFalsy();
});
21 changes: 5 additions & 16 deletions src/utilities/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import type {
} from '../types/timeline';
import { generateRandomPastelColor } from './color';
import { getExternalEventRowId } from './externalEvents';
import { filterEmpty } from './generic';
import { filterEmpty, lowercase } from './generic';
import { getDoyTime } from './time';

export enum TimelineLockStatus {
Expand Down Expand Up @@ -1559,7 +1559,7 @@ export function directiveOrSpanMatchesDynamicFilters(
subsystemTagId = typeDef.subsystem_tag.id;
}
matches = matchesDynamicFilter(subsystemTagId, curr.operator, curr.value);
} else if (curr.field === 'Tag' && isArray((directiveOrSpan as ActivityDirective).tags)) {
} else if (curr.field === 'Tags' && isArray((directiveOrSpan as ActivityDirective).tags)) {
const ids = (directiveOrSpan as ActivityDirective).tags.map(tag => tag.tag.id);
matches = matchesDynamicFilter(ids, curr.operator, curr.value);
} else if (curr.field === 'Parameter' && curr.subfield) {
Expand Down Expand Up @@ -1604,10 +1604,6 @@ export function typeMatchesDynamicFilters(
}, true);
}

export function lowercase(value: any) {
return typeof value === 'string' ? value.toLowerCase() : value;
}

export function matchesDynamicFilter(
rawItemValue: ActivityLayerDynamicFilter<ActivityLayerFilterField>['value'], // the actual value
operator: ActivityLayerDynamicFilter<ActivityLayerFilterField>['operator'],
Expand All @@ -1634,23 +1630,16 @@ export function matchesDynamicFilter(
return false;
case 'does_not_include':
if (typeof filterValue === 'string' && typeof itemValue === 'string') {
if (filterValue === '') {
return true;
}
return itemValue.indexOf(filterValue) < 0;
} else if (isArray(filterValue)) {
return !(isArray(itemValue) ? itemValue : [itemValue]).find(
item => (filterValue as (typeof itemValue)[]).indexOf(item) > -1,
);
}
return false;
case 'is_one_of':
if (!isArray(filterValue)) {
return itemValue === filterValue;
}
return (filterValue as (typeof itemValue)[]).indexOf(itemValue) > -1;
case 'is_not_one_of':
if (!isArray(filterValue)) {
return itemValue !== filterValue;
}
return (filterValue as (typeof itemValue)[]).indexOf(itemValue) < 0;
case 'is_greater_than':
return itemValue > filterValue;
case 'is_less_than':
Expand Down

0 comments on commit 5374a7b

Please sign in to comment.