From 8a0d4c944400ee6aee3ca6b1a1a61c269370e46e Mon Sep 17 00:00:00 2001 From: hatim dinia <diniahatim@gmail.com> Date: Tue, 15 Oct 2024 11:28:53 +0200 Subject: [PATCH] feat: minor improvements --- .../explore/Results/ResultDetails/index.tsx | 4 +- .../common/MatrixGrid/index.test.tsx | 20 ++--- .../src/components/common/MatrixGrid/types.ts | 28 +++---- .../MatrixGrid/useColumnMapping.test.ts | 20 ++--- .../common/MatrixGrid/useColumnMapping.ts | 4 +- .../MatrixGrid/useGridCellContent.test.ts | 54 ++++++------- .../common/MatrixGrid/useGridCellContent.ts | 12 +-- .../components/common/MatrixGrid/useMatrix.ts | 16 ++-- .../common/MatrixGrid/utils.test.ts | 14 ++-- .../src/components/common/MatrixGrid/utils.ts | 81 ++++++++++--------- 10 files changed, 123 insertions(+), 130 deletions(-) diff --git a/webapp/src/components/App/Singlestudy/explore/Results/ResultDetails/index.tsx b/webapp/src/components/App/Singlestudy/explore/Results/ResultDetails/index.tsx index 3011f257d6..d05fa51d13 100644 --- a/webapp/src/components/App/Singlestudy/explore/Results/ResultDetails/index.tsx +++ b/webapp/src/components/App/Singlestudy/explore/Results/ResultDetails/index.tsx @@ -58,7 +58,7 @@ import { generateCustomColumns, generateDateTime, } from "../../../../../common/MatrixGrid/utils.ts"; -import { ColumnTypes } from "../../../../../common/MatrixGrid/types.ts"; +import { Column } from "../../../../../common/MatrixGrid/types.ts"; import SplitView from "../../../../../common/SplitView/index.tsx"; import ResultFilters from "./ResultFilters.tsx"; import { toError } from "../../../../../../utils/fnUtils.ts"; @@ -284,7 +284,7 @@ function ResultDetails() { { id: "date", title: "Date", - type: ColumnTypes.DateTime, + type: Column.DateTime, editable: false, }, ...generateCustomColumns({ diff --git a/webapp/src/components/common/MatrixGrid/index.test.tsx b/webapp/src/components/common/MatrixGrid/index.test.tsx index 363ae7b8c2..fbbeaa4a2c 100644 --- a/webapp/src/components/common/MatrixGrid/index.test.tsx +++ b/webapp/src/components/common/MatrixGrid/index.test.tsx @@ -16,7 +16,7 @@ import { render } from "@testing-library/react"; import MatrixGrid, { MatrixGridProps } from "."; import Box from "@mui/material/Box"; import { mockGetBoundingClientRect } from "../../../tests/mocks/mockGetBoundingClientRect"; -import { type EnhancedGridColumn, ColumnTypes } from "./types"; +import { type EnhancedGridColumn, Column } from "./types"; import { mockHTMLCanvasElement } from "../../../tests/mocks/mockHTMLCanvasElement"; beforeEach(() => { @@ -67,7 +67,7 @@ describe("MatrixGrid rendering", () => { id: "col1", title: "Column 1", width: 100, - type: ColumnTypes.Number, + type: Column.Number, editable: true, order: 0, }, @@ -75,7 +75,7 @@ describe("MatrixGrid rendering", () => { id: "col2", title: "Column 2", width: 100, - type: ColumnTypes.Number, + type: Column.Number, editable: true, order: 1, }, @@ -83,7 +83,7 @@ describe("MatrixGrid rendering", () => { id: "col3", title: "Column 3", width: 100, - type: ColumnTypes.Number, + type: Column.Number, editable: true, order: 2, }, @@ -118,7 +118,7 @@ describe("MatrixGrid rendering", () => { id: "col1", title: "Column 1", width: 100, - type: ColumnTypes.Number, + type: Column.Number, editable: true, order: 0, }, @@ -126,7 +126,7 @@ describe("MatrixGrid rendering", () => { id: "col2", title: "Column 2", width: 100, - type: ColumnTypes.Number, + type: Column.Number, editable: true, order: 1, }, @@ -134,7 +134,7 @@ describe("MatrixGrid rendering", () => { id: "col3", title: "Column 3", width: 100, - type: ColumnTypes.Number, + type: Column.Number, editable: true, order: 2, }, @@ -171,7 +171,7 @@ describe("MatrixGrid rendering", () => { id: "col1", title: "Column 1", width: 100, - type: ColumnTypes.Number, + type: Column.Number, editable: true, order: 0, }, @@ -179,7 +179,7 @@ describe("MatrixGrid rendering", () => { id: "col2", title: "Column 2", width: 100, - type: ColumnTypes.Number, + type: Column.Number, editable: true, order: 1, }, @@ -187,7 +187,7 @@ describe("MatrixGrid rendering", () => { id: "col3", title: "Column 3", width: 100, - type: ColumnTypes.Number, + type: Column.Number, editable: true, order: 2, }, diff --git a/webapp/src/components/common/MatrixGrid/types.ts b/webapp/src/components/common/MatrixGrid/types.ts index a996de9a63..aa6044fca1 100644 --- a/webapp/src/components/common/MatrixGrid/types.ts +++ b/webapp/src/components/common/MatrixGrid/types.ts @@ -22,16 +22,14 @@ import { // Enums //////////////////////////////////////////////////////////////// -// TODO update enums to be singular - -export const ColumnTypes = { +export const Column = { DateTime: "datetime", Number: "number", Text: "text", Aggregate: "aggregate", } as const; -export const Operations = { +export const Operation = { Add: "+", Sub: "-", Mul: "*", @@ -41,7 +39,7 @@ export const Operations = { } as const; // !NOTE: Keep lowercase to match Glide Data Grid column ids -export const Aggregates = { +export const Aggregate = { Min: "min", Max: "max", Avg: "avg", @@ -49,12 +47,11 @@ export const Aggregates = { } as const; export const TimeFrequency = { - // TODO update old enum occurrences - ANNUAL: "annual", - MONTHLY: "monthly", - WEEKLY: "weekly", - DAILY: "daily", - HOURLY: "hourly", + Annual: "annual", + Monthly: "monthly", + Weekly: "weekly", + Daily: "daily", + Hourly: "hourly", } as const; //////////////////////////////////////////////////////////////// @@ -62,10 +59,9 @@ export const TimeFrequency = { //////////////////////////////////////////////////////////////// // Derived types -export type ColumnType = (typeof ColumnTypes)[keyof typeof ColumnTypes]; -// TODO add sufix Type -export type Operation = (typeof Operations)[keyof typeof Operations]; -export type AggregateType = (typeof Aggregates)[keyof typeof Aggregates]; +export type ColumnType = (typeof Column)[keyof typeof Column]; +export type OperationType = (typeof Operation)[keyof typeof Operation]; +export type AggregateType = (typeof Aggregate)[keyof typeof Aggregate]; export type TimeFrequencyType = (typeof TimeFrequency)[keyof typeof TimeFrequency]; @@ -127,7 +123,7 @@ export interface GridUpdate { // Shape of updates to be sent to the API export interface MatrixUpdate { - operation: Operation; + operation: OperationType; value: number; } diff --git a/webapp/src/components/common/MatrixGrid/useColumnMapping.test.ts b/webapp/src/components/common/MatrixGrid/useColumnMapping.test.ts index e88473b9a6..2dc3ebd7f2 100644 --- a/webapp/src/components/common/MatrixGrid/useColumnMapping.test.ts +++ b/webapp/src/components/common/MatrixGrid/useColumnMapping.test.ts @@ -15,42 +15,42 @@ import { renderHook } from "@testing-library/react"; import { describe, test, expect } from "vitest"; import { useColumnMapping } from "./useColumnMapping"; -import { EnhancedGridColumn, ColumnTypes } from "./types"; +import { EnhancedGridColumn, Column } from "./types"; describe("useColumnMapping", () => { const testColumns: EnhancedGridColumn[] = [ { id: "text", title: "Text", - type: ColumnTypes.Text, + type: Column.Text, width: 100, editable: false, }, { id: "date", title: "Date", - type: ColumnTypes.DateTime, + type: Column.DateTime, width: 100, editable: false, }, { id: "num1", title: "Number 1", - type: ColumnTypes.Number, + type: Column.Number, width: 100, editable: true, }, { id: "num2", title: "Number 2", - type: ColumnTypes.Number, + type: Column.Number, width: 100, editable: true, }, { id: "agg", title: "Aggregate", - type: ColumnTypes.Aggregate, + type: Column.Aggregate, width: 100, editable: false, }, @@ -90,14 +90,14 @@ describe("useColumnMapping", () => { { id: "text", title: "Text", - type: ColumnTypes.Text, + type: Column.Text, width: 100, editable: false, }, { id: "date", title: "Date", - type: ColumnTypes.DateTime, + type: Column.DateTime, width: 100, editable: false, }, @@ -113,14 +113,14 @@ describe("useColumnMapping", () => { { id: "num1", title: "Number 1", - type: ColumnTypes.Number, + type: Column.Number, width: 100, editable: true, }, { id: "num2", title: "Number 2", - type: ColumnTypes.Number, + type: Column.Number, width: 100, editable: true, }, diff --git a/webapp/src/components/common/MatrixGrid/useColumnMapping.ts b/webapp/src/components/common/MatrixGrid/useColumnMapping.ts index 522f93d025..ffbf683965 100644 --- a/webapp/src/components/common/MatrixGrid/useColumnMapping.ts +++ b/webapp/src/components/common/MatrixGrid/useColumnMapping.ts @@ -14,7 +14,7 @@ import { useMemo } from "react"; import { Item } from "@glideapps/glide-data-grid"; -import { EnhancedGridColumn, ColumnTypes } from "./types"; +import { EnhancedGridColumn, Column } from "./types"; /** * A custom hook that provides coordinate mapping functions for a grid with mixed column types. @@ -48,7 +48,7 @@ import { EnhancedGridColumn, ColumnTypes } from "./types"; export function useColumnMapping(columns: EnhancedGridColumn[]) { return useMemo(() => { const dataColumnIndices = columns.reduce((acc, col, index) => { - if (col.type === ColumnTypes.Number) { + if (col.type === Column.Number) { acc.push(index); } return acc; diff --git a/webapp/src/components/common/MatrixGrid/useGridCellContent.test.ts b/webapp/src/components/common/MatrixGrid/useGridCellContent.test.ts index 8adf3041d5..cdb2c2003c 100644 --- a/webapp/src/components/common/MatrixGrid/useGridCellContent.test.ts +++ b/webapp/src/components/common/MatrixGrid/useGridCellContent.test.ts @@ -14,11 +14,7 @@ import { renderHook } from "@testing-library/react"; import { useGridCellContent } from "./useGridCellContent"; -import { - ColumnTypes, - MatrixAggregates, - type EnhancedGridColumn, -} from "./types"; +import { Column, MatrixAggregates, type EnhancedGridColumn } from "./types"; import { useColumnMapping } from "./useColumnMapping"; // Mocking i18next @@ -85,7 +81,7 @@ describe("useGridCellContent", () => { { id: "total", title: "Total", - type: ColumnTypes.Aggregate, + type: Column.Aggregate, width: 100, editable: false, }, @@ -136,28 +132,28 @@ describe("useGridCellContent", () => { { id: "date", title: "Date", - type: ColumnTypes.DateTime, + type: Column.DateTime, width: 150, editable: false, }, { id: "ts1", title: "TS 1", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, { id: "ts2", title: "TS 2", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, { id: "total", title: "Total", - type: ColumnTypes.Aggregate, + type: Column.Aggregate, width: 100, editable: false, }, @@ -208,35 +204,35 @@ describe("useGridCellContent with mixed column types", () => { { id: "rowHeader", title: "Row", - type: ColumnTypes.Text, + type: Column.Text, width: 100, editable: false, }, { id: "date", title: "Date", - type: ColumnTypes.DateTime, + type: Column.DateTime, width: 150, editable: false, }, { id: "data1", title: "TS 1", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, { id: "data2", title: "TS 2", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, { id: "total", title: "Total", - type: ColumnTypes.Aggregate, + type: Column.Aggregate, width: 100, editable: false, }, @@ -307,21 +303,21 @@ describe("useGridCellContent with mixed column types", () => { { id: "data1", title: "TS 1", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, { id: "data2", title: "TS 2", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, { id: "data3", title: "TS 3", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, @@ -352,7 +348,7 @@ describe("useGridCellContent additional tests", () => { { id: "data1", title: "TS 1", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, @@ -374,7 +370,7 @@ describe("useGridCellContent additional tests", () => { { id: "data1", title: "TS 1", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, @@ -397,7 +393,7 @@ describe("useGridCellContent additional tests", () => { { id: "data1", title: "TS 1", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, @@ -419,7 +415,7 @@ describe("useGridCellContent additional tests", () => { { id: "total", title: "Total", - type: ColumnTypes.Aggregate, + type: Column.Aggregate, width: 100, editable: false, }, @@ -444,14 +440,14 @@ describe("useGridCellContent additional tests", () => { { id: "data1", title: "TS 1", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, { id: "data2", title: "TS 2", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: false, }, @@ -476,7 +472,7 @@ describe("useGridCellContent additional tests", () => { { id: "data1", title: "TS 1", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, @@ -500,14 +496,14 @@ describe("useGridCellContent additional tests", () => { { id: "large", title: "Large", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, { id: "small", title: "Small", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, @@ -539,7 +535,7 @@ describe("useGridCellContent additional tests", () => { { id: "negative", title: "Negative", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, @@ -563,7 +559,7 @@ describe("useGridCellContent additional tests", () => { { id: "zero", title: "Zero", - type: ColumnTypes.Number, + type: Column.Number, width: 50, editable: true, }, diff --git a/webapp/src/components/common/MatrixGrid/useGridCellContent.ts b/webapp/src/components/common/MatrixGrid/useGridCellContent.ts index 57c94c5fe4..d718cd3dd7 100644 --- a/webapp/src/components/common/MatrixGrid/useGridCellContent.ts +++ b/webapp/src/components/common/MatrixGrid/useGridCellContent.ts @@ -17,8 +17,8 @@ import { GridCell, GridCellKind, Item } from "@glideapps/glide-data-grid"; import { type EnhancedGridColumn, type ColumnType, - ColumnTypes, MatrixAggregates, + Column, } from "./types"; import { formatNumber } from "./utils"; @@ -37,7 +37,7 @@ type CellContentGenerator = ( * Each generator function creates the appropriate GridCell based on the column type and data. */ const cellContentGenerators: Record<ColumnType, CellContentGenerator> = { - [ColumnTypes.Text]: ( + [Column.Text]: ( row, col, column, @@ -52,14 +52,14 @@ const cellContentGenerators: Record<ColumnType, CellContentGenerator> = { readonly: !column.editable, allowOverlay: false, }), - [ColumnTypes.DateTime]: (row, col, column, data, dateTime) => ({ + [Column.DateTime]: (row, col, column, data, dateTime) => ({ kind: GridCellKind.Text, data: "", // Date/time columns are not editable displayData: dateTime?.[row] ?? "", readonly: !column.editable, allowOverlay: false, }), - [ColumnTypes.Number]: (row, col, column, data) => { + [Column.Number]: (row, col, column, data) => { const value = data?.[row]?.[col]; return { @@ -72,7 +72,7 @@ const cellContentGenerators: Record<ColumnType, CellContentGenerator> = { thousandSeparator: " ", }; }, - [ColumnTypes.Aggregate]: (row, col, column, data, dateTime, aggregates) => { + [Column.Aggregate]: (row, col, column, data, dateTime, aggregates) => { const value = aggregates?.[column.id as keyof MatrixAggregates]?.[row]; return { @@ -160,7 +160,7 @@ export function useGridCellContent( // accounting for any non-data columns in the grid let adjustedCol = col; - if (column.type === ColumnTypes.Number && gridToData) { + if (column.type === Column.Number && gridToData) { // Map grid cell to data array index const dataCell = gridToData(cell); diff --git a/webapp/src/components/common/MatrixGrid/useMatrix.ts b/webapp/src/components/common/MatrixGrid/useMatrix.ts index b747aadce2..225b1146e0 100644 --- a/webapp/src/components/common/MatrixGrid/useMatrix.ts +++ b/webapp/src/components/common/MatrixGrid/useMatrix.ts @@ -26,13 +26,13 @@ import { getStudyData } from "../../../services/api/study"; import { EnhancedGridColumn, MatrixDataDTO, - ColumnTypes, GridUpdate, MatrixUpdateDTO, MatrixAggregates, AggregateConfig, - Aggregates, - Operations, + Column, + Operation, + Aggregate, } from "./types"; import { aggregatesTheme, @@ -158,7 +158,7 @@ export function useMatrix( baseColumns.push({ id: "date", title: "Date", - type: ColumnTypes.DateTime, + type: Column.DateTime, editable: false, themeOverride: { bgCell: "#2D2E40" }, }); @@ -168,7 +168,7 @@ export function useMatrix( baseColumns.unshift({ id: "rowHeaders", title: "", - type: ColumnTypes.Text, + type: Column.Text, editable: false, }); } @@ -184,10 +184,10 @@ export function useMatrix( (aggregateType) => ({ id: aggregateType, title: aggregateType.charAt(0).toUpperCase() + aggregateType.slice(1), // Capitalize first letter - type: ColumnTypes.Aggregate, + type: Column.Aggregate, editable: false, themeOverride: - aggregateType === Aggregates.Avg + aggregateType === Aggregate.Avg ? aggregatesTheme : { ...aggregatesTheme, bgCell: "#464770" }, }), @@ -218,7 +218,7 @@ export function useMatrix( return { coordinates: [[col, row]], operation: { - operation: Operations.Eq, + operation: Operation.Eq, value: value.data, }, }; diff --git a/webapp/src/components/common/MatrixGrid/utils.test.ts b/webapp/src/components/common/MatrixGrid/utils.test.ts index 403e46c0ed..93a419f742 100644 --- a/webapp/src/components/common/MatrixGrid/utils.test.ts +++ b/webapp/src/components/common/MatrixGrid/utils.test.ts @@ -16,7 +16,7 @@ import { MatrixIndex, StudyOutputDownloadLevelDTO, } from "../../../common/types"; -import { ColumnTypes } from "./types"; +import { Column } from "./types"; import { calculateMatrixAggregates, formatNumber, @@ -49,21 +49,21 @@ describe("generateTimeSeriesColumns", () => { { id: "data1", title: "TS 1", - type: ColumnTypes.Number, + type: Column.Number, style: "normal", editable: true, }, { id: "data2", title: "TS 2", - type: ColumnTypes.Number, + type: Column.Number, style: "normal", editable: true, }, { id: "data3", title: "TS 3", - type: ColumnTypes.Number, + type: Column.Number, style: "normal", editable: true, }, @@ -81,14 +81,14 @@ describe("generateTimeSeriesColumns", () => { { id: "data10", title: "Data 10", - type: ColumnTypes.Number, + type: Column.Number, style: "normal", editable: false, }, { id: "data11", title: "Data 11", - type: ColumnTypes.Number, + type: Column.Number, style: "normal", editable: false, }, @@ -110,7 +110,7 @@ describe("generateTimeSeriesColumns", () => { test("maintains consistent type and style", () => { const result = generateTimeSeriesColumns({ count: 1000 }); result.forEach((column) => { - expect(column.type).toBe(ColumnTypes.Number); + expect(column.type).toBe(Column.Number); expect(column.style).toBe("normal"); }); }); diff --git a/webapp/src/components/common/MatrixGrid/utils.ts b/webapp/src/components/common/MatrixGrid/utils.ts index 408cd140e8..26701d7fff 100644 --- a/webapp/src/components/common/MatrixGrid/utils.ts +++ b/webapp/src/components/common/MatrixGrid/utils.ts @@ -14,18 +14,18 @@ import { EnhancedGridColumn, - ColumnTypes, TimeSeriesColumnOptions, CustomColumnOptions, MatrixAggregates, AggregateType, AggregateConfig, - Aggregates, DateIncrementFunction, FormatFunction, TimeFrequency, TimeFrequencyType, DateTimeMetadataDTO, + Aggregate, + Column, } from "./types"; import { type FirstWeekContainsDate, @@ -82,7 +82,7 @@ export const darkTheme: Theme = { export const readOnlyDarkTheme: Partial<Theme> = { bgCell: "#1A1C2A", bgCellMedium: "#22243A", - textDark: "#A0A0A0", + textDark: "#FAF9F6", textMedium: "#808080", textLight: "#606060", accentColor: "#4A4C66", @@ -118,7 +118,6 @@ export function formatNumber(num: number | undefined): string { const [integerPart, decimalPart] = num.toString().split("."); - // Format integer part with thousand separators using a non-regex approach const formattedInteger = integerPart .split("") .reverse() @@ -152,15 +151,15 @@ const TIME_FREQUENCY_CONFIG: Record< format: FormatFunction; } > = { - [TimeFrequency.ANNUAL]: { + [TimeFrequency.Annual]: { increment: addYears, format: () => t("global.time.annual"), }, - [TimeFrequency.MONTHLY]: { + [TimeFrequency.Monthly]: { increment: addMonths, format: (date: Date) => format(date, "MMM", { locale: getLocale() }), }, - [TimeFrequency.WEEKLY]: { + [TimeFrequency.Weekly]: { increment: addWeeks, format: (date: Date, firstWeekSize: number) => { const weekStart = startOfWeek(date, { locale: getLocale() }); @@ -172,11 +171,11 @@ const TIME_FREQUENCY_CONFIG: Record< }); }, }, - [TimeFrequency.DAILY]: { + [TimeFrequency.Daily]: { increment: addDays, format: (date: Date) => format(date, "EEE d MMM", { locale: getLocale() }), }, - [TimeFrequency.HOURLY]: { + [TimeFrequency.Hourly]: { increment: addHours, format: (date: Date) => format(date, "EEE d MMM HH:mm", { locale: getLocale() }), @@ -222,12 +221,12 @@ export const generateDateTime = (config: DateTimeMetadataDTO): string[] => { * * @example <caption>Usage within a column definition array</caption> * const columns = [ - * { id: "rowHeaders", title: "", type: ColumnTypes.Text, ... }, - * { id: "date", title: "Date", type: ColumnTypes.DateTime, ... }, + * { id: "rowHeaders", title: "", type: Column.Text, ... }, + * { id: "date", title: "Date", type: Column.DateTime, ... }, * ...generateTimeSeriesColumns({ count: 60 }), - * { id: "min", title: "Min", type: ColumnTypes.Aggregate, ... }, - * { id: "max", title: "Max", type: ColumnTypes.Aggregate, ... }, - * { id: "avg", title: "Avg", type: ColumnTypes.Aggregate, ... } + * { id: "min", title: "Min", type: Column.Aggregate, ... }, + * { id: "max", title: "Max", type: Column.Aggregate, ... }, + * { id: "avg", title: "Avg", type: Column.Aggregate, ... } * ]; */ export function generateTimeSeriesColumns({ @@ -241,7 +240,7 @@ export function generateTimeSeriesColumns({ return Array.from({ length: count }, (_, index) => ({ id: `data${startIndex + index}`, title: `${prefix} ${startIndex + index}`, - type: ColumnTypes.Number, + type: Column.Number, style, width, editable, @@ -263,7 +262,7 @@ export function generateCustomColumns({ return titles.map((title, index) => ({ id: `custom${index + 1}`, title, - type: ColumnTypes.Number, + type: Column.Number, style: "normal", width, editable: true, @@ -298,16 +297,21 @@ export function generateDataColumns( return []; } -// TODO add docs + refactor +/** + * Determines the aggregate types based on the provided configuration. + * + * @param aggregateConfig - The configuration for aggregates. + * @returns An array of AggregateType. + */ export function getAggregateTypes( aggregateConfig: AggregateConfig, ): AggregateType[] { if (aggregateConfig === "stats") { - return [Aggregates.Avg, Aggregates.Min, Aggregates.Max]; + return [Aggregate.Avg, Aggregate.Min, Aggregate.Max]; } if (aggregateConfig === "all") { - return [Aggregates.Min, Aggregates.Max, Aggregates.Avg, Aggregates.Total]; + return [Aggregate.Min, Aggregate.Max, Aggregate.Avg, Aggregate.Total]; } if (Array.isArray(aggregateConfig)) { @@ -317,6 +321,13 @@ export function getAggregateTypes( return []; } +/** + * Calculates matrix aggregates based on the provided matrix and aggregate types. + * + * @param matrix - The input matrix of numbers. + * @param aggregateTypes - The types of aggregates to calculate. + * @returns An object containing the calculated aggregates. + */ export function calculateMatrixAggregates( matrix: number[][], aggregateTypes: AggregateType[], @@ -324,39 +335,29 @@ export function calculateMatrixAggregates( const aggregates: Partial<MatrixAggregates> = {}; matrix.forEach((row) => { - if (aggregateTypes.includes(Aggregates.Min)) { - if (!aggregates.min) { - aggregates.min = []; - } + if (aggregateTypes.includes(Aggregate.Min)) { + aggregates.min = aggregates.min || []; aggregates.min.push(Math.min(...row)); } - if (aggregateTypes.includes(Aggregates.Max)) { - if (!aggregates.max) { - aggregates.max = []; - } + if (aggregateTypes.includes(Aggregate.Max)) { + aggregates.max = aggregates.max || []; aggregates.max.push(Math.max(...row)); } if ( - aggregateTypes.includes(Aggregates.Avg) || - aggregateTypes.includes(Aggregates.Total) + aggregateTypes.includes(Aggregate.Avg) || + aggregateTypes.includes(Aggregate.Total) ) { - const sum = row.reduce((sum, num) => sum + num, 0); - - if (aggregateTypes.includes(Aggregates.Avg)) { - if (!aggregates.avg) { - aggregates.avg = []; - } + const sum = row.reduce((acc, num) => acc + num, 0); + if (aggregateTypes.includes(Aggregate.Avg)) { + aggregates.avg = aggregates.avg || []; aggregates.avg.push(Number((sum / row.length).toFixed())); } - if (aggregateTypes.includes(Aggregates.Total)) { - if (!aggregates.total) { - aggregates.total = []; - } - + if (aggregateTypes.includes(Aggregate.Total)) { + aggregates.total = aggregates.total || []; aggregates.total.push(Number(sum.toFixed())); } }