From 5d4245eac42edfd6455935cd4d1921bc0dc7e7eb Mon Sep 17 00:00:00 2001 From: NB10328 Date: Thu, 3 Mar 2022 18:01:22 +0000 Subject: [PATCH] code cleanup --- ascii-table3.d.ts | 78 +++++++++++++++++++++++++++++++++++++++++++++++ ascii-table3.js | 64 +++++++++++--------------------------- package.json | 2 +- 3 files changed, 96 insertions(+), 48 deletions(-) create mode 100644 ascii-table3.d.ts diff --git a/ascii-table3.d.ts b/ascii-table3.d.ts new file mode 100644 index 0000000..51f5218 --- /dev/null +++ b/ascii-table3.d.ts @@ -0,0 +1,78 @@ +/** + * Type definition for table section style. + * @typedef {Object} SectionStyle + * @property {string} left The left border character. + * @property {string} center The center border character. + * @property {string} right The right border character. + * @property {string} colSeparator The column separator character. + */ +export type SectionStyle = { + left: string, + center: string, + right: string, + colSeparator: string +}; + + /** + * Borders style definition. + * @typedef {Object} Borders + * @property {SectionStyle} top The style for top section borders (above heading). + * @property {SectionStyle} middle The style for middle section borders (between heading and data). + * @property {SectionStyle} bottom The style for bottom section borders (below data). + * @property {SectionStyle} data The style for data row borders. + */ + export type Borders = { + top: SectionStyle, + middle: SectionStyle, + bottom: SectionStyle, + data: SectionStyle + }; + +/** + * Type definition for a table style. + * @typedef {Object} Style + * @property {string} name Style name. + * @property {Borders} borders The border styles for each section. + */ +export type Style = { + name: string, + borders: Borders +}; + +/** + * @typedef {object} ColumnFormatJSON + * @property {number[]} aligns Alignment setting for each data column. + * @property {number[]} widths Width setting for each data column. + * @property {boolean[]} wrappings Wrapping setting for each data column. + */ +export type ColumnFormatJSON = { + aligns: number[], + widths: number[], + wrappings: boolean[] +}; + +/** + * @typedef {object} FormattingJSON + * @property {number} titleAlign Title alignment setting. + * @property {ColumnFormatJSON} columns Format settings for each column. + * @property {boolean} justify Whether to justify table columns. + */ +export type FormattingJSON = { + titleAlign: number, + columns: ColumnFormatJSON, + justify: boolean +}; + +/** + * @typedef {object} TableJSON + * @property {string} title Table title text. + * @property {string[]} heading Array of table column headings. + * @property {string[][]} rows Array of table rows (each row contains an array of data columns). + * @property {FormattingJSON} formatting Table formatting settings. + */ +export type TableJSON = { + title: string, + heading: string[], + rows: string[][], + formatting: FormattingJSON +}; diff --git a/ascii-table3.js b/ascii-table3.js index 2562a79..b1739d0 100644 --- a/ascii-table3.js +++ b/ascii-table3.js @@ -1,50 +1,13 @@ /*jshint esversion: 6 */ /** - * Type definition for table section style. - * @typedef {Object} SectionStyle - * @property {string} left The left border character. - * @property {string} center The center border character. - * @property {string} right The right border character. - * @property {string} colSeparator The column separator character. - */ - - /** - * Borders style definition. - * @typedef {Object} Borders - * @property {SectionStyle} top The style for top section borders (above heading). - * @property {SectionStyle} middle The style for middle section borders (between heading and data). - * @property {SectionStyle} bottom The style for bottom section borders (below data). - * @property {SectionStyle} data The style for data row borders. - */ - -/** - * Type definition for a table style. - * @typedef {Object} Style - * @property {string} name Style name. - * @property {Borders} borders The border styles for each section. - */ - -/** - * @typedef {object} ColumnFormatJSON - * @property {number[]} aligns Alignment setting for each data column. - * @property {number[]} widths Width setting for each data column. - * @property {boolean[]} wrappings Wrapping setting for each data column. - */ - -/** - * @typedef {object} FormattingJSON - * @property {number} titleAlign Title alignment setting. - * @property {ColumnFormatJSON} columns Format settings for each column. - * @property {boolean} justify Whether to justify table columns. - */ - -/** - * @typedef {object} TableJSON - * @property {string} title Table title text. - * @property {string[]} heading Array of table column headings. - * @property {string[][]} rows Array of table rows (each row contains an array of data columns). - * @property {FormattingJSON} formatting Table formatting settings. + * Type imports. + * @typedef { import("./ascii-table3").SectionStyle } SectionStyle + * @typedef { import("./ascii-table3").Borders } Borders + * @typedef { import("./ascii-table3").Style } Style + * @typedef { import("./ascii-table3").ColumnFormatJSON } ColumnFormatJSON + * @typedef { import("./ascii-table3").FormattingJSON } FormattingJSON + * @typedef { import("./ascii-table3").TableJSON } TableJSON */ /** @@ -239,6 +202,12 @@ class AsciiTable3 { } } + /** + * Wraps a string into multiple lines of a limited width. + * @param {string} str The string to wrap. + * @param {num} maxWidth The maximum width for the wrapped string. + * @returns {string} The wrapped string. + */ static wordWrap(str, maxWidth) { // partition string const partArray = partition(String(str)); @@ -269,7 +238,7 @@ class AsciiTable3 { } /** - * Wraps a string into multiple lines of a limited width. + * Wraps a string into multiple lines of a limited width (simple string, no ANSI chars). * @param {string} str The string to wrap. * @param {num} maxWidth The maximum width for the wrapped string. * @returns {string} The wrapped string. @@ -307,10 +276,11 @@ class AsciiTable3 { /** * Truncates a string up to a maximum number of characters (if needed). + * In case of truncation, '...' are appended to the end of the string. * @static * @param {string} str The string to truncate. * @param {number} maxSize The string maximum size. - * @returns {string} The truncated string (in case of truncation, '...' are appended to the end). + * @returns {string} The truncated string. */ static truncateString(str, maxSize) { const SUFIX = '...'; @@ -1296,7 +1266,7 @@ class AsciiTable3 { // full table width const maxWidth = - colsWidth.reduce(function(a, b) { return a + b; }, 0) + // data column sizes + colsWidth.reduce(function(a, b) { return a + b; }, 0) + // data column sizes (colsWidth.length - 1) * strlen(style.borders.data.colSeparator); // mid column separators var result = ''; diff --git a/package.json b/package.json index 375af58..4088cd1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ascii-table3", - "version": "0.7.2", + "version": "0.7.3", "author": "João Simões (https://github.com/AllMightySauron)", "description": "Javascript ASCII renderer for beautiful console-based tables", "repository": {