From afd7cf51cf651180707886467f945f2022500782 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Thu, 14 Sep 2023 22:46:43 +0530 Subject: [PATCH] fix: Types and nomenclature --- packages/mermaid/src/diagrams/packet/db.ts | 18 ++++++++++-------- .../mermaid/src/diagrams/packet/renderer.ts | 10 +++++----- packages/mermaid/src/diagrams/packet/types.ts | 4 ++-- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/mermaid/src/diagrams/packet/db.ts b/packages/mermaid/src/diagrams/packet/db.ts index a36fdcaf1..0773a2c0e 100644 --- a/packages/mermaid/src/diagrams/packet/db.ts +++ b/packages/mermaid/src/diagrams/packet/db.ts @@ -1,11 +1,11 @@ -import type { Block, PacketDB, Word } from './types.js'; +import type { Block, PacketDB, Row } from './types.js'; import { log } from '../../logger.js'; import type { PacketDiagramConfig } from '../../config.type.js'; import DEFAULT_CONFIG from '../../defaultConfig.js'; import { getConfig as commonGetConfig } from '../../config.js'; interface PacketData { - packet: Word[]; + packet: Row[]; } const defaultPacketData: PacketData = { @@ -22,21 +22,23 @@ export const getConfig = (): Required => { }); }; -export const getPacket = (): Word[] => data.packet; +export const getPacket = (): Row[] => data.packet; export const getNextFittingBlock = ( block: Block, row: number, bitsPerRow: number -): [Block, Block | undefined] => { - block.end = block.end ?? block.start; +): [Required, Block | undefined] => { + if (block.end === undefined) { + block.end = block.start; + } if (block.start > block.end) { throw new Error(`Block start ${block.start} is greater than block end ${block.end}.`); } if (block.end + 1 <= row * bitsPerRow) { - return [block, undefined]; + return [block as Required, undefined]; } return [ @@ -55,12 +57,12 @@ export const getNextFittingBlock = ( export const populate = ({ blocks }: { blocks: Block[] }) => { let lastByte = -1; - let word: Block[] = []; + let word: Row = []; data.packet = []; let row = 1; const { bitsPerRow } = getConfig(); for (let { start, end, label } of blocks) { - if (end < start) { + if (end && end < start) { throw new Error(`Packet block ${start} - ${end} is invalid. End must be greater than start.`); } if (start != lastByte + 1) { diff --git a/packages/mermaid/src/diagrams/packet/renderer.ts b/packages/mermaid/src/diagrams/packet/renderer.ts index 640c2e870..d6d2a3d2e 100644 --- a/packages/mermaid/src/diagrams/packet/renderer.ts +++ b/packages/mermaid/src/diagrams/packet/renderer.ts @@ -1,7 +1,7 @@ import { configureSvgSize } from '../../setupGraphViewbox.js'; import type { DrawDefinition, Group, SVG } from '../../diagram-api/types.js'; import { selectSvgElement } from '../../rendering-util/selectSvgElement.js'; -import type { PacketDB, Word } from './types.js'; +import type { PacketDB, Row } from './types.js'; import type { PacketDiagramConfig } from '../../config.type.js'; import type { Diagram } from '../../Diagram.js'; @@ -25,13 +25,13 @@ const draw: DrawDefinition = (_text, id, _version, diagram: Diagram) => { const drawWord = ( svg: SVG, - word: Word, - row: number, + row: Row, + rowNumber: number, { rowHeight, paddingX, paddingY, bitWidth, bitsPerRow }: Required ) => { const group: Group = svg.append('g'); - const wordY = row * (rowHeight + paddingY) + paddingY; - for (const block of word) { + const wordY = rowNumber * (rowHeight + paddingY) + paddingY; + for (const block of row) { const blockX = (block.start % bitsPerRow) * bitWidth + 1; const width = (block.end - block.start + 1) * bitWidth - paddingX; // Block rectangle diff --git a/packages/mermaid/src/diagrams/packet/types.ts b/packages/mermaid/src/diagrams/packet/types.ts index 9100558eb..5492d4004 100644 --- a/packages/mermaid/src/diagrams/packet/types.ts +++ b/packages/mermaid/src/diagrams/packet/types.ts @@ -3,8 +3,8 @@ import type { DiagramDB } from '../../diagram-api/types.js'; export type ArrayElement = A extends readonly (infer T)[] ? T : never; export type Block = Pick, 'start' | 'end' | 'label'>; -export type Word = Block[]; +export type Row = Required[]; export interface PacketDB extends DiagramDB { - getPacket: () => Word[]; + getPacket: () => Row[]; }