Skip to content
Merged
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
17 changes: 17 additions & 0 deletions src/common/property-filter-type-guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
import { PropertyFilterProps } from '@cloudscape-design/components/property-filter';

export function isTokenGroup(
tokenOrGroup: PropertyFilterProps.Token | PropertyFilterProps.TokenGroup,
): tokenOrGroup is PropertyFilterProps.TokenGroup {
const key: keyof PropertyFilterProps.TokenGroup = 'operation';
return key in tokenOrGroup;
}

export function isToken(
tokenOrGroup: PropertyFilterProps.Token | PropertyFilterProps.TokenGroup,
): tokenOrGroup is PropertyFilterProps.Token {
const key: keyof PropertyFilterProps.Token = 'operator';
return key in tokenOrGroup;
}
5 changes: 3 additions & 2 deletions src/fake-server/distributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: MIT-0
import { PropertyFilterProps } from '@cloudscape-design/components/property-filter';

import { isToken, isTokenGroup } from '../common/property-filter-type-guards';
import { Distribution } from './types';
import fetchJson from './utils/fetch-json';

Expand Down Expand Up @@ -99,14 +100,14 @@ function filterItemsByProperty(options: FetchDistributionOptions) {
opFn: (a: boolean, b: boolean) => void,
tokenOrGroup: Token | TokenGroup,
): boolean {
if ('operation' in tokenOrGroup) {
if (isTokenGroup(tokenOrGroup)) {
const nextOpFn = operationFn<boolean>(tokenOrGroup.operation!);
return tokenOrGroup.tokens.reduce(
(include: boolean, token: Token | TokenGroup) => filterWithToken(include, nextOpFn, token),
operation === 'and',
);
}
if ('operator' in tokenOrGroup) {
if (isToken(tokenOrGroup)) {
const comparator = getComparatorForOperator(tokenOrGroup.operator);
const searchableProps = tokenOrGroup.propertyKey ? [tokenOrGroup.propertyKey] : Object.keys(item);
return searchableProps.some(propertyKey => {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/table-saved-filters/filter-set-modals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Modal from '@cloudscape-design/components/modal';
import { PropertyFilterProps } from '@cloudscape-design/components/property-filter';
import SpaceBetween from '@cloudscape-design/components/space-between';

import { isTokenGroup } from '../../common/property-filter-type-guards';
import { FilterSet } from './use-filter-sets';

function queryToString(
Expand Down Expand Up @@ -44,7 +45,7 @@ function queryToString(
}

function tokenOrGroupToString(tokenOrGroup: PropertyFilterProps.TokenGroup | PropertyFilterProps.Token): string {
if ('operation' in tokenOrGroup) {
if (isTokenGroup(tokenOrGroup)) {
return '(' + tokenOrGroup.tokens.map(tokenOrGroupToString).join(`, ${tokenOrGroup.operation} `) + ')';
} else {
return tokenToString(tokenOrGroup);
Expand Down
6 changes: 5 additions & 1 deletion test/e2e/a11y.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ describe('Checking examples accessibility', function () {

await browser.execute(fs.readFileSync(require.resolve('axe-core/axe.min.js'), 'utf8'));
type AxeResult = { result: Axe.AxeResults } | { error: Error };
function isAxeError(response: AxeResult): response is { error: Error } {
const key: keyof { error: Error } = 'error';
return key in response;
}
const runAxe = (done: (result: AxeResult) => void) =>
axe
.run({
Expand All @@ -92,7 +96,7 @@ describe('Checking examples accessibility', function () {
// executeAsync has incorrect typings: https://github.com/webdriverio/webdriverio/issues/6206
const response = (await browser.executeAsync(runAxe as any)) as AxeResult;

if ('error' in response) {
if (isAxeError(response)) {
throw response.error;
}

Expand Down
Loading