Skip to content

Commit

Permalink
feat: add allCoordinateSystemCoordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Sep 10, 2024
1 parent 6b156f2 commit 92717f5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/Plate/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 },
]);
});
});
17 changes: 17 additions & 0 deletions src/Plate/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Coordinates<TCoordinateSystem>> {
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();
}

0 comments on commit 92717f5

Please sign in to comment.