Skip to content

Commit 6852665

Browse files
committed
Cast integers to string in Matrix vis with auto and fixed-point notation
1 parent 808b099 commit 6852665

File tree

1 file changed

+17
-19
lines changed
  • packages/app/src/vis-packs/core/matrix

1 file changed

+17
-19
lines changed

packages/app/src/vis-packs/core/matrix/utils.ts

+17-19
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {
33
isBoolType,
44
isComplexType,
55
isEnumType,
6+
isFloatType,
67
isIntegerType,
7-
isNumericType,
88
} from '@h5web/shared/guards';
99
import {
1010
type ComplexType,
@@ -22,7 +22,18 @@ import {
2222
} from '@h5web/shared/vis-utils';
2323
import { format } from 'd3-format';
2424

25-
export function createNumericFormatter(
25+
export function createIntegerFormatter(
26+
notation: Notation,
27+
): (val: ScalarValue<NumericType>) => string {
28+
if (notation === Notation.Scientific) {
29+
const formatter = format('.3e');
30+
return (val) => formatter(Number(val));
31+
}
32+
33+
return (val) => val.toString();
34+
}
35+
36+
export function createFloatFormatter(
2637
notation: Notation,
2738
): (val: number) => string {
2839
switch (notation) {
@@ -35,19 +46,6 @@ export function createNumericFormatter(
3546
}
3647
}
3748

38-
export function createBigIntFormatter(
39-
notation: Notation,
40-
): (val: ScalarValue<NumericType>) => string {
41-
switch (notation) {
42-
case Notation.Scientific: {
43-
const formatter = createNumericFormatter(notation);
44-
return (val) => formatter(Number(val));
45-
}
46-
default:
47-
return (val) => val.toString();
48-
}
49-
}
50-
5149
export function createMatrixComplexFormatter(
5250
notation: Notation,
5351
): (val: ScalarValue<ComplexType>) => string {
@@ -68,12 +66,12 @@ export function getFormatter(
6866
type: PrintableType,
6967
notation: Notation,
7068
): ValueFormatter<PrintableType> {
71-
if (isIntegerType(type) && type.size === 64) {
72-
return createBigIntFormatter(notation);
69+
if (isIntegerType(type)) {
70+
return createIntegerFormatter(notation);
7371
}
7472

75-
if (isNumericType(type)) {
76-
return createNumericFormatter(notation);
73+
if (isFloatType(type)) {
74+
return createFloatFormatter(notation);
7775
}
7876

7977
if (isBoolType(type)) {

0 commit comments

Comments
 (0)