From 92717f547ccee60cc34899ad810c8bb6dbb23461 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 10 Sep 2024 09:13:20 +0200 Subject: [PATCH] feat: add allCoordinateSystemCoordinates --- src/Plate/utils.test.tsx | 41 +++++++++++++++++++++++++++++++++++++++- src/Plate/utils.ts | 17 +++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/Plate/utils.test.tsx b/src/Plate/utils.test.tsx index 0af50a4..9b389dc 100644 --- a/src/Plate/utils.test.tsx +++ b/src/Plate/utils.test.tsx @@ -2,8 +2,10 @@ import { COORDINATE_SYSTEM_96_WELL, CoordinateSystem96Well, } from './coordinateSystem96Well'; -import { Coordinates } from './types'; +import { Coordinates, CoordinateSystem } from './types'; import { + allCoordinateSystemCoordinates, + allCoordinateSystemPositions, areEqualCoordinates, columnForPosition, convertPositionFromColumnToRowFlow, @@ -175,3 +177,40 @@ describe('areEqualCoordinates', () => { expect(areEqualCoordinates(a, { ...b, foo: 'bar' })).toBe(false); }); }); + +const COORDINATE_SYSTEM_2_BY_2 = { + rows: ['A', 'B'], + columns: [1, 2], +} as const satisfies CoordinateSystem; + +describe('allCoordinateSystemPositions', () => { + it('returns an array of all positions in a coordinate systems', () => { + expect(allCoordinateSystemPositions(COORDINATE_SYSTEM_2_BY_2)).toEqual([ + 1, 2, 3, 4, + ]); + }); +}); + +describe('allCoordinateSystemCoordinates', () => { + it('returns an array of all coordinates in column flow', () => { + expect( + allCoordinateSystemCoordinates(COORDINATE_SYSTEM_2_BY_2, 'column'), + ).toEqual([ + { row: 'A', column: 1 }, + { row: 'A', column: 2 }, + { row: 'B', column: 1 }, + { row: 'B', column: 2 }, + ]); + }); + + it('returns an array of all coordinates in row flow', () => { + expect( + allCoordinateSystemCoordinates(COORDINATE_SYSTEM_2_BY_2, 'row'), + ).toEqual([ + { row: 'A', column: 1 }, + { row: 'B', column: 1 }, + { row: 'A', column: 2 }, + { row: 'B', column: 2 }, + ]); + }); +}); diff --git a/src/Plate/utils.ts b/src/Plate/utils.ts index 06737c7..e50cfed 100644 --- a/src/Plate/utils.ts +++ b/src/Plate/utils.ts @@ -172,3 +172,20 @@ export function allCoordinateSystemPositions( coordinateSystem.rows.length * coordinateSystem.columns.length + 1, ); } + +export function allCoordinateSystemCoordinates< + TCoordinateSystem extends CoordinateSystem, +>( + coordinateSystem: TCoordinateSystem, + flowDirection: FlowDirection, +): Array> { + if (flowDirection === 'column') { + return coordinateSystem.rows + .map((row) => coordinateSystem.columns.map((column) => ({ row, column }))) + .flat(); + } + + return coordinateSystem.columns + .map((column) => coordinateSystem.rows.map((row) => ({ row, column }))) + .flat(); +}