refactor: improve types of shapes

Instead of being a `Record<string, ShapeHandler>`, the type now knows
every key in the variable.
This commit is contained in:
Alois Klink 2024-10-17 18:59:43 +09:00
parent 2933eb5c28
commit cfe7cce41d
2 changed files with 23 additions and 15 deletions

View File

@ -84,7 +84,7 @@ describe('Test Alias for shapes', function () {
});
it('should support alias for shadedProcess shape ', function () {
const aliases = ['lined-process', 'lined-rectangle', 'lin-proc', 'lin-rect'];
const aliases = ['lined-process', 'lined-rectangle', 'lin-proc', 'lin-rect'] as const;
for (const alias of aliases) {
expect(shapes[alias]).toBe(shapes['shaded-process']);
}

View File

@ -1,3 +1,4 @@
import type { Entries } from 'type-fest';
import type { Node, ShapeRenderOptions } from '../types.js';
import { anchor } from './shapes/anchor.js';
import { bowTieRect } from './shapes/bowTieRect.js';
@ -75,7 +76,7 @@ export interface ShapeDefinition {
handler: ShapeHandler;
}
export const shapesDefs: ShapeDefinition[] = [
export const shapesDefs = [
{
semanticName: 'Process',
name: 'Rectangle',
@ -442,11 +443,11 @@ export const shapesDefs: ShapeDefinition[] = [
aliases: ['lined-document'],
handler: linedWaveEdgedRect,
},
];
] as const satisfies ShapeDefinition[];
const generateShapeMap = () => {
// These are the shapes that didn't have documentation present
const shapeMap: Record<string, ShapeHandler> = {
const undocumentedShapes = {
// States
state,
choice,
@ -464,18 +465,25 @@ const generateShapeMap = () => {
imageSquare,
anchor,
};
} as const;
for (const shape of shapesDefs) {
for (const alias of [
shape.shortName,
...(shape.aliases ?? []),
...(shape.internalAliases ?? []),
]) {
shapeMap[alias] = shape.handler;
}
}
return shapeMap;
const entries = [
...(Object.entries(undocumentedShapes) as Entries<typeof undocumentedShapes>),
...shapesDefs.flatMap((shape) => {
const aliases = [
shape.shortName,
...('aliases' in shape ? shape.aliases : []),
...('internalAliases' in shape ? shape.internalAliases : []),
];
return aliases.map((alias) => [alias, shape.handler] as const);
}),
];
return Object.fromEntries(entries) as Record<
(typeof entries)[number][0],
(typeof entries)[number][1]
> satisfies Record<string, ShapeHandler>;
};
export const shapes = generateShapeMap();
export type ShapeID = keyof typeof shapes;