Skip to content

Commit 41c1061

Browse files
authored
fix: Refactor of in checks (#237)
1 parent 8eba7a5 commit 41c1061

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
import { PropertyFilterProps } from '@cloudscape-design/components/property-filter';
4+
5+
export function isTokenGroup(
6+
tokenOrGroup: PropertyFilterProps.Token | PropertyFilterProps.TokenGroup,
7+
): tokenOrGroup is PropertyFilterProps.TokenGroup {
8+
const key: keyof PropertyFilterProps.TokenGroup = 'operation';
9+
return key in tokenOrGroup;
10+
}
11+
12+
export function isToken(
13+
tokenOrGroup: PropertyFilterProps.Token | PropertyFilterProps.TokenGroup,
14+
): tokenOrGroup is PropertyFilterProps.Token {
15+
const key: keyof PropertyFilterProps.Token = 'operator';
16+
return key in tokenOrGroup;
17+
}

src/fake-server/distributions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: MIT-0
33
import { PropertyFilterProps } from '@cloudscape-design/components/property-filter';
44

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

@@ -99,14 +100,14 @@ function filterItemsByProperty(options: FetchDistributionOptions) {
99100
opFn: (a: boolean, b: boolean) => void,
100101
tokenOrGroup: Token | TokenGroup,
101102
): boolean {
102-
if ('operation' in tokenOrGroup) {
103+
if (isTokenGroup(tokenOrGroup)) {
103104
const nextOpFn = operationFn<boolean>(tokenOrGroup.operation!);
104105
return tokenOrGroup.tokens.reduce(
105106
(include: boolean, token: Token | TokenGroup) => filterWithToken(include, nextOpFn, token),
106107
operation === 'and',
107108
);
108109
}
109-
if ('operator' in tokenOrGroup) {
110+
if (isToken(tokenOrGroup)) {
110111
const comparator = getComparatorForOperator(tokenOrGroup.operator);
111112
const searchableProps = tokenOrGroup.propertyKey ? [tokenOrGroup.propertyKey] : Object.keys(item);
112113
return searchableProps.some(propertyKey => {

src/pages/table-saved-filters/filter-set-modals.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Modal from '@cloudscape-design/components/modal';
1313
import { PropertyFilterProps } from '@cloudscape-design/components/property-filter';
1414
import SpaceBetween from '@cloudscape-design/components/space-between';
1515

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

1819
function queryToString(
@@ -44,7 +45,7 @@ function queryToString(
4445
}
4546

4647
function tokenOrGroupToString(tokenOrGroup: PropertyFilterProps.TokenGroup | PropertyFilterProps.Token): string {
47-
if ('operation' in tokenOrGroup) {
48+
if (isTokenGroup(tokenOrGroup)) {
4849
return '(' + tokenOrGroup.tokens.map(tokenOrGroupToString).join(`, ${tokenOrGroup.operation} `) + ')';
4950
} else {
5051
return tokenToString(tokenOrGroup);

test/e2e/a11y.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ describe('Checking examples accessibility', function () {
7777

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

95-
if ('error' in response) {
99+
if (isAxeError(response)) {
96100
throw response.error;
97101
}
98102

0 commit comments

Comments
 (0)