fix: Types and nomenclature

This commit is contained in:
Sidharth Vinod 2023-09-14 22:46:43 +05:30
parent c41594d220
commit afd7cf51cf
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
3 changed files with 17 additions and 15 deletions

View File

@ -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<PacketDiagramConfig> => {
});
};
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>, 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<Block>, 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) {

View File

@ -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<PacketDiagramConfig>
) => {
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

View File

@ -3,8 +3,8 @@ import type { DiagramDB } from '../../diagram-api/types.js';
export type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
export type Block = Pick<ArrayElement<Packet['blocks']>, 'start' | 'end' | 'label'>;
export type Word = Block[];
export type Row = Required<Block>[];
export interface PacketDB extends DiagramDB {
getPacket: () => Word[];
getPacket: () => Row[];
}