Skip to content

Commit

Permalink
Cast integers to string in Matrix vis with auto and fixed-point notation
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Feb 3, 2025
1 parent 1ee4149 commit 0748093
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions packages/app/src/vis-packs/core/matrix/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
isBoolType,
isComplexType,
isEnumType,
isFloatType,
isIntegerType,
isNumericType,
} from '@h5web/shared/guards';
import {
type ComplexType,
Expand All @@ -22,7 +22,18 @@ import {
} from '@h5web/shared/vis-utils';
import { format } from 'd3-format';

export function createNumericFormatter(
export function createIntegerFormatter(
notation: Notation,
): (val: ScalarValue<NumericType>) => string {
if (notation === Notation.Scientific) {
const formatter = format('.3e');
return (val) => formatter(Number(val));
}

return (val) => val.toString();
}

export function createFloatFormatter(
notation: Notation,
): (val: number) => string {
switch (notation) {
Expand All @@ -35,19 +46,6 @@ export function createNumericFormatter(
}
}

export function createBigIntFormatter(
notation: Notation,
): (val: ScalarValue<NumericType>) => string {
switch (notation) {
case Notation.Scientific: {
const formatter = createNumericFormatter(notation);
return (val) => formatter(Number(val));
}
default:
return (val) => val.toString();
}
}

export function createMatrixComplexFormatter(
notation: Notation,
): (val: ScalarValue<ComplexType>) => string {
Expand All @@ -68,12 +66,12 @@ export function getFormatter(
type: PrintableType,
notation: Notation,
): ValueFormatter<PrintableType> {
if (isIntegerType(type) && type.size === 64) {
return createBigIntFormatter(notation);
if (isIntegerType(type)) {
return createIntegerFormatter(notation);
}

if (isNumericType(type)) {
return createNumericFormatter(notation);
if (isFloatType(type)) {
return createFloatFormatter(notation);
}

if (isBoolType(type)) {
Expand Down

0 comments on commit 0748093

Please sign in to comment.