Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
NB10328 authored and NB10328 committed Mar 3, 2022
1 parent f447c46 commit 5d4245e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 48 deletions.
78 changes: 78 additions & 0 deletions ascii-table3.d.ts
Original file line number Diff line number Diff line change
@@ -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
};
64 changes: 17 additions & 47 deletions ascii-table3.js
Original file line number Diff line number Diff line change
@@ -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
*/

/**
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 = '...';
Expand Down Expand Up @@ -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 = '';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ascii-table3",
"version": "0.7.2",
"version": "0.7.3",
"author": "João Simões <[email protected]> (https://github.com/AllMightySauron)",
"description": "Javascript ASCII renderer for beautiful console-based tables",
"repository": {
Expand Down

0 comments on commit 5d4245e

Please sign in to comment.