Skip to content

Commit 1b9e0ae

Browse files
authored
Merge pull request #379 from silx-kit/clean-up
Remove obsolete type guards and fix `axes` attribute creation
2 parents 685b9e8 + 9fe12db commit 1b9e0ae

File tree

10 files changed

+25
-46
lines changed

10 files changed

+25
-46
lines changed

Diff for: src/h5web/metadata-viewer/EntityTable.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { ReactElement } from 'react';
22
import type { MyHDF5Entity } from '../providers/models';
33
import styles from './MetadataViewer.module.css';
44
import {
5-
hasMySimpleShape,
5+
hasSimpleShape,
66
isDataset,
77
isDatatype,
88
isLink,
@@ -64,7 +64,7 @@ function EntityTable(props: Props): ReactElement {
6464
<tr>
6565
<th scope="row">Shape</th>
6666
<td>
67-
{hasMySimpleShape(entity)
67+
{hasSimpleShape(entity)
6868
? renderShapeDims(entity.shape.dims)
6969
: entity.shape.class}
7070
</td>

Diff for: src/h5web/providers/mock/utils.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { mockValues, mockMetadata } from './data';
2-
import {
3-
assertDataset,
4-
assertMySimpleShape,
5-
assertNumericType,
6-
} from '../utils';
2+
import { assertDataset, assertSimpleShape, assertNumericType } from '../utils';
73
import { assertArray, assertDefined } from '../../visualizations/shared/utils';
84
import ndarray from 'ndarray';
95
import { getEntityAtPath } from '../../explorer/utils';
@@ -13,7 +9,7 @@ export function getMockDataArray(path: string): ndarray {
139
assertDefined(dataset, `Expected entity at path "${path}"`);
1410
assertDataset(dataset, `Expected group at path "${path}"`);
1511
assertNumericType(dataset);
16-
assertMySimpleShape(dataset);
12+
assertSimpleShape(dataset);
1713

1814
const value = mockValues[dataset.id as keyof typeof mockValues];
1915
assertArray<number>(value);

Diff for: src/h5web/providers/my-utils.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ import {
2020
MyHDF5ResolvedEntity,
2121
} from '../providers/models';
2222
import { NxInterpretation, SilxStyle } from '../visualizations/nexus/models';
23-
import { makeSimpleShape, makeStrAttr } from './raw-utils';
23+
import { makeNxAxesAttr, makeSimpleShape, makeStrAttr } from './raw-utils';
2424

2525
type EntityOpts = Partial<
2626
Pick<MyHDF5ResolvedEntity, 'id' | 'attributes' | 'rawLink'>
2727
>;
2828
type GroupOpts = EntityOpts & { children?: MyHDF5Entity[] };
29-
type AllOrNone<T> = T | { [K in keyof T]?: never };
3029

3130
export function makeMyGroup(
3231
name: string,
@@ -149,7 +148,10 @@ export function makeMyNxDataGroup<
149148
errors?: MyHDF5Dataset<HDF5SimpleShape, HDF5NumericType>;
150149
title?: MyHDF5Dataset<HDF5ScalarShape, HDF5StringType>;
151150
silxStyle?: SilxStyle;
152-
} & AllOrNone<{ axes: T; axesAttr: (keyof T | '.')[] }> &
151+
} & (
152+
| { axes: T; axesAttr: (Extract<keyof T, string> | '.')[] }
153+
| { axes?: never; axesAttr?: never }
154+
) &
153155
GroupOpts
154156
): MyHDF5Group {
155157
const {
@@ -176,7 +178,7 @@ export function makeMyNxDataGroup<
176178
...(groupOpts.attributes || []),
177179
makeStrAttr('NX_class', 'NXdata'),
178180
makeStrAttr('signal', signal.name),
179-
makeStrAttr('axes', axesAttr),
181+
...(axesAttr ? [makeNxAxesAttr(axesAttr)] : []),
180182
...(silxStyle
181183
? [makeStrAttr('SILX_style', JSON.stringify(silxStyle))]
182184
: []),

Diff for: src/h5web/providers/raw-utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function makeAttr(
7070
return { name, type, shape, value };
7171
}
7272

73-
export function makeStrAttr(name: string, value: HDF5Value): HDF5Attribute {
73+
export function makeStrAttr(name: string, value: string): HDF5Attribute {
7474
return makeAttr(name, scalarShape, stringType, value);
7575
}
7676

Diff for: src/h5web/providers/utils.ts

+2-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
HDF5Type,
1010
HDF5TypeClass,
1111
HDF5RootLink,
12-
HDF5Dataset,
1312
HDF5ScalarShape,
1413
HDF5NumericType,
1514
MyHDF5Entity,
@@ -58,13 +57,7 @@ export function isLink(entity: MyHDF5Entity): entity is MyHDF5Link {
5857
return entity.kind === MyHDF5EntityKind.Link;
5958
}
6059

61-
function hasSimpleShape<T extends HDF5Type>(
62-
dataset: HDF5Dataset<HDF5Shape, T>
63-
): dataset is HDF5Dataset<HDF5SimpleShape, T> {
64-
return dataset.shape.class === HDF5ShapeClass.Simple;
65-
}
66-
67-
export function hasMySimpleShape<T extends HDF5Type>(
60+
export function hasSimpleShape<T extends HDF5Type>(
6861
dataset: MyHDF5Dataset<HDF5Shape, T>
6962
): dataset is MyHDF5Dataset<HDF5SimpleShape, T> {
7063
return dataset.shape.class === HDF5ShapeClass.Simple;
@@ -123,21 +116,9 @@ export function assertNumericType<S extends HDF5Shape>(
123116
}
124117

125118
export function assertSimpleShape<T extends HDF5Type>(
126-
dataset: HDF5Dataset<HDF5Shape, T>
127-
): asserts dataset is HDF5Dataset<HDF5SimpleShape, T> {
128-
if (!hasSimpleShape(dataset)) {
129-
throw new Error('Expected dataset to have simple shape');
130-
}
131-
132-
if (dataset.shape.dims.length === 0) {
133-
throw new Error('Expected dataset with simple shape to have dimensions');
134-
}
135-
}
136-
137-
export function assertMySimpleShape<T extends HDF5Type>(
138119
dataset: MyHDF5Dataset<HDF5Shape, T>
139120
): asserts dataset is MyHDF5Dataset<HDF5SimpleShape, T> {
140-
if (!hasMySimpleShape(dataset)) {
121+
if (!hasSimpleShape(dataset)) {
141122
throw new Error('Expected dataset to have simple shape');
142123
}
143124

Diff for: src/h5web/visualizations/containers/HeatmapVisContainer.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { useDatasetValue } from './hooks';
33
import {
44
assertDataset,
55
assertNumericType,
6-
assertMySimpleShape,
6+
assertSimpleShape,
77
} from '../../providers/utils';
88
import MappedHeatmapVis from '../heatmap/MappedHeatmapVis';
99
import type { VisContainerProps } from './models';
1010

1111
function HeatmapVisContainer(props: VisContainerProps): ReactElement {
1212
const { entity } = props;
1313
assertDataset(entity);
14-
assertMySimpleShape(entity);
14+
assertSimpleShape(entity);
1515
assertNumericType(entity);
1616

1717
const { dims } = entity.shape;

Diff for: src/h5web/visualizations/containers/LineVisContainer.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { useDatasetValue } from './hooks';
33
import {
44
assertDataset,
55
assertNumericType,
6-
assertMySimpleShape,
6+
assertSimpleShape,
77
} from '../../providers/utils';
88
import MappedLineVis from '../line/MappedLineVis';
99
import type { VisContainerProps } from './models';
1010

1111
function LineVisContainer(props: VisContainerProps): ReactElement {
1212
const { entity } = props;
1313
assertDataset(entity);
14-
assertMySimpleShape(entity);
14+
assertSimpleShape(entity);
1515
assertNumericType(entity);
1616

1717
const value = useDatasetValue(entity.id);

Diff for: src/h5web/visualizations/containers/MatrixVisContainer.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React, { ReactElement } from 'react';
22
import { useDatasetValue } from './hooks';
3-
import { assertDataset, assertMySimpleShape } from '../../providers/utils';
3+
import { assertDataset, assertSimpleShape } from '../../providers/utils';
44
import MappedMatrixVis from '../matrix/MappedMatrixVis';
55
import type { VisContainerProps } from './models';
66

77
function MatrixVisContainer(props: VisContainerProps): ReactElement {
88
const { entity } = props;
99
assertDataset(entity);
10-
assertMySimpleShape(entity);
10+
assertSimpleShape(entity);
1111

1212
const value = useDatasetValue(entity.id);
1313
if (!value) {

Diff for: src/h5web/visualizations/index.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { MyHDF5Entity } from '../providers/models';
77
import {
88
hasScalarShape,
99
hasBaseType,
10-
hasMySimpleShape,
10+
hasSimpleShape,
1111
hasNumericType,
1212
isDataset,
1313
isGroup,
@@ -72,7 +72,7 @@ export const VIS_DEFS: Record<Vis, VisDef> = {
7272
return (
7373
isDataset(entity) &&
7474
hasBaseType(entity) &&
75-
hasMySimpleShape(entity) &&
75+
hasSimpleShape(entity) &&
7676
entity.shape.dims.length >= 1
7777
);
7878
},
@@ -86,7 +86,7 @@ export const VIS_DEFS: Record<Vis, VisDef> = {
8686
return (
8787
isDataset(entity) &&
8888
hasNumericType(entity) &&
89-
hasMySimpleShape(entity) &&
89+
hasSimpleShape(entity) &&
9090
entity.shape.dims.length >= 1
9191
);
9292
},
@@ -100,7 +100,7 @@ export const VIS_DEFS: Record<Vis, VisDef> = {
100100
return (
101101
isDataset(entity) &&
102102
hasNumericType(entity) &&
103-
hasMySimpleShape(entity) &&
103+
hasSimpleShape(entity) &&
104104
entity.shape.dims.length >= 2
105105
);
106106
},

Diff for: src/h5web/visualizations/nexus/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
assertGroup,
1313
assertDataset,
1414
assertNumericType,
15-
assertMySimpleShape,
15+
assertSimpleShape,
1616
} from '../../providers/utils';
1717
import {
1818
NxAttribute,
@@ -85,7 +85,7 @@ export function findSignalDataset(
8585
assertDefined(dataset, `Expected "${signalName}" signal entity to exist`);
8686
assertDataset(dataset, `Expected "${signalName}" signal to be a dataset`);
8787
assertNumericType(dataset);
88-
assertMySimpleShape(dataset);
88+
assertSimpleShape(dataset);
8989

9090
return dataset;
9191
}

0 commit comments

Comments
 (0)