From 0eb45263a49694d4a692478d7625c82f92146d61 Mon Sep 17 00:00:00 2001 From: CentreMetre Date: Sun, 14 Jun 2026 20:41:10 +0100 Subject: [PATCH] Refactor constructor to an options object Introduce an exported TableOptions type and replace the trailing positional constructor parameters (displayNamesAndOrder, messageElement, trueSymbol, falseSymbol) with a single optional options object, so call sites are order-independent and named. --- abstract-table.ts | 48 +++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/abstract-table.ts b/abstract-table.ts index d96d427..87dda0b 100644 --- a/abstract-table.ts +++ b/abstract-table.ts @@ -1,3 +1,27 @@ +/** + * Optional configuration for a {@link Table}. + * @template TRow The row shape; see {@link Table}. + */ +export type TableOptions = { + /** + * A object with the same properties, but all string type for the header cell values. + * Used by passing an object with the keys of {@link TRow} and the name preferred. E.g. for a user: + * ```{ id: "ID", name: "Name" }```, this makes the columns appear in the order of ID then Name. + */ + displayNamesAndOrder?: Record; + /** + * Used to display messages related to data output. Optional. + * Omit (undefined) to use the class provided HTMLParagraphElement. + * Pass an HTMLElement to use a custom element. + * Pass null to disable automatic message output. + */ + messageElement?: HTMLElement | null; + /** The symbol used for boolean cells that are true. Optional, default "✔". */ + trueSymbol?: string; + /** The symbol used for boolean cells that are false. Optional, default "✖". */ + falseSymbol?: string; +}; + /** * @template TRow A type that the shape of a row should be. E.g. * ``` @@ -24,35 +48,23 @@ export abstract class Table { * Constructs a table based on the parameters given. * @param exampleObject An instance of the type to be displaying. Can be an object with default/zero values for that type * such as `0`, `""`, `false` or `true`. E.g. for a user ```{ id: 0; name: ""; }``` would be passed. - * @param displayNamesAndOrder A object with the same properties, but all string type for the header cell values. - * Used by passing an object with the keys of {@link TRow} and the name preferred. E.g. for a user: - * ```{ id: "ID", name: "Name" }```, this makes the columns appear in the order of ID then Name. - * @param messageElement Used to display messages related to data output. Optional. - * Omit (undefined) to use the class provided HTMLParagraphElement. - * Pass an HTMLElement to use a custom element. - * Pass null to disable automatic message output. - * @param trueSymbol The symbol used for boolean cells that are true. Optional, default "✔". - * @param falseSymbol The symbol used for boolean cells that are false. Optional, default "✖". + * @param options Optional configuration. See {@link TableOptions}. */ - constructor(exampleObject: TRow, - displayNamesAndOrder?: Record, - messageElement?: HTMLElement | undefined | null, - trueSymbol: string = "✔", - falseSymbol: string = "✖") { + constructor(exampleObject: TRow, options: TableOptions = {}) { this.table = document.createElement("table") this.tableBody = document.createElement("tbody"); - this.headers = this.createHeadersFromType(exampleObject, displayNamesAndOrder); + this.headers = this.createHeadersFromType(exampleObject, options.displayNamesAndOrder); this.setHeaderRow(); this.table.appendChild(this.tableBody); // If null it means disabled - this.messageEl = messageElement === undefined ? document.createElement("p") : messageElement; + this.messageEl = options.messageElement === undefined ? document.createElement("p") : options.messageElement; - this.trueSymbol = trueSymbol; - this.falseSymbol = falseSymbol; + this.trueSymbol = options.trueSymbol ?? "✔"; + this.falseSymbol = options.falseSymbol ?? "✖"; } /**