-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathutils.ts
35 lines (30 loc) · 948 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { hasBoolType, hasComplexType, hasEnumType } from '@h5web/shared/guards';
import {
type ArrayShape,
type Dataset,
type PrintableType,
type ScalarValue,
} from '@h5web/shared/hdf5-models';
import { type ValueFormatter } from '@h5web/shared/vis-models';
import {
createEnumFormatter,
formatBool,
formatScalarComplex,
} from '@h5web/shared/vis-utils';
export function getFormatter(
dataset: Dataset<ArrayShape, PrintableType>,
): (val: ScalarValue<PrintableType>) => string; // override distributivity of `ValueFormatter`
export function getFormatter<T extends PrintableType>(
dataset: Dataset<ArrayShape, T>,
): ValueFormatter<PrintableType> {
if (hasComplexType(dataset)) {
return formatScalarComplex;
}
if (hasBoolType(dataset)) {
return formatBool;
}
if (hasEnumType(dataset)) {
return createEnumFormatter(dataset.type.mapping);
}
return (val: number | bigint | string) => val.toString();
}