Skip to content

Commit 5d4245e

Browse files
NB10328NB10328
authored andcommitted
code cleanup
1 parent f447c46 commit 5d4245e

File tree

3 files changed

+96
-48
lines changed

3 files changed

+96
-48
lines changed

ascii-table3.d.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Type definition for table section style.
3+
* @typedef {Object} SectionStyle
4+
* @property {string} left The left border character.
5+
* @property {string} center The center border character.
6+
* @property {string} right The right border character.
7+
* @property {string} colSeparator The column separator character.
8+
*/
9+
export type SectionStyle = {
10+
left: string,
11+
center: string,
12+
right: string,
13+
colSeparator: string
14+
};
15+
16+
/**
17+
* Borders style definition.
18+
* @typedef {Object} Borders
19+
* @property {SectionStyle} top The style for top section borders (above heading).
20+
* @property {SectionStyle} middle The style for middle section borders (between heading and data).
21+
* @property {SectionStyle} bottom The style for bottom section borders (below data).
22+
* @property {SectionStyle} data The style for data row borders.
23+
*/
24+
export type Borders = {
25+
top: SectionStyle,
26+
middle: SectionStyle,
27+
bottom: SectionStyle,
28+
data: SectionStyle
29+
};
30+
31+
/**
32+
* Type definition for a table style.
33+
* @typedef {Object} Style
34+
* @property {string} name Style name.
35+
* @property {Borders} borders The border styles for each section.
36+
*/
37+
export type Style = {
38+
name: string,
39+
borders: Borders
40+
};
41+
42+
/**
43+
* @typedef {object} ColumnFormatJSON
44+
* @property {number[]} aligns Alignment setting for each data column.
45+
* @property {number[]} widths Width setting for each data column.
46+
* @property {boolean[]} wrappings Wrapping setting for each data column.
47+
*/
48+
export type ColumnFormatJSON = {
49+
aligns: number[],
50+
widths: number[],
51+
wrappings: boolean[]
52+
};
53+
54+
/**
55+
* @typedef {object} FormattingJSON
56+
* @property {number} titleAlign Title alignment setting.
57+
* @property {ColumnFormatJSON} columns Format settings for each column.
58+
* @property {boolean} justify Whether to justify table columns.
59+
*/
60+
export type FormattingJSON = {
61+
titleAlign: number,
62+
columns: ColumnFormatJSON,
63+
justify: boolean
64+
};
65+
66+
/**
67+
* @typedef {object} TableJSON
68+
* @property {string} title Table title text.
69+
* @property {string[]} heading Array of table column headings.
70+
* @property {string[][]} rows Array of table rows (each row contains an array of data columns).
71+
* @property {FormattingJSON} formatting Table formatting settings.
72+
*/
73+
export type TableJSON = {
74+
title: string,
75+
heading: string[],
76+
rows: string[][],
77+
formatting: FormattingJSON
78+
};

ascii-table3.js

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,13 @@
11
/*jshint esversion: 6 */
22

33
/**
4-
* Type definition for table section style.
5-
* @typedef {Object} SectionStyle
6-
* @property {string} left The left border character.
7-
* @property {string} center The center border character.
8-
* @property {string} right The right border character.
9-
* @property {string} colSeparator The column separator character.
10-
*/
11-
12-
/**
13-
* Borders style definition.
14-
* @typedef {Object} Borders
15-
* @property {SectionStyle} top The style for top section borders (above heading).
16-
* @property {SectionStyle} middle The style for middle section borders (between heading and data).
17-
* @property {SectionStyle} bottom The style for bottom section borders (below data).
18-
* @property {SectionStyle} data The style for data row borders.
19-
*/
20-
21-
/**
22-
* Type definition for a table style.
23-
* @typedef {Object} Style
24-
* @property {string} name Style name.
25-
* @property {Borders} borders The border styles for each section.
26-
*/
27-
28-
/**
29-
* @typedef {object} ColumnFormatJSON
30-
* @property {number[]} aligns Alignment setting for each data column.
31-
* @property {number[]} widths Width setting for each data column.
32-
* @property {boolean[]} wrappings Wrapping setting for each data column.
33-
*/
34-
35-
/**
36-
* @typedef {object} FormattingJSON
37-
* @property {number} titleAlign Title alignment setting.
38-
* @property {ColumnFormatJSON} columns Format settings for each column.
39-
* @property {boolean} justify Whether to justify table columns.
40-
*/
41-
42-
/**
43-
* @typedef {object} TableJSON
44-
* @property {string} title Table title text.
45-
* @property {string[]} heading Array of table column headings.
46-
* @property {string[][]} rows Array of table rows (each row contains an array of data columns).
47-
* @property {FormattingJSON} formatting Table formatting settings.
4+
* Type imports.
5+
* @typedef { import("./ascii-table3").SectionStyle } SectionStyle
6+
* @typedef { import("./ascii-table3").Borders } Borders
7+
* @typedef { import("./ascii-table3").Style } Style
8+
* @typedef { import("./ascii-table3").ColumnFormatJSON } ColumnFormatJSON
9+
* @typedef { import("./ascii-table3").FormattingJSON } FormattingJSON
10+
* @typedef { import("./ascii-table3").TableJSON } TableJSON
4811
*/
4912

5013
/**
@@ -239,6 +202,12 @@ class AsciiTable3 {
239202
}
240203
}
241204

205+
/**
206+
* Wraps a string into multiple lines of a limited width.
207+
* @param {string} str The string to wrap.
208+
* @param {num} maxWidth The maximum width for the wrapped string.
209+
* @returns {string} The wrapped string.
210+
*/
242211
static wordWrap(str, maxWidth) {
243212
// partition string
244213
const partArray = partition(String(str));
@@ -269,7 +238,7 @@ class AsciiTable3 {
269238
}
270239

271240
/**
272-
* Wraps a string into multiple lines of a limited width.
241+
* Wraps a string into multiple lines of a limited width (simple string, no ANSI chars).
273242
* @param {string} str The string to wrap.
274243
* @param {num} maxWidth The maximum width for the wrapped string.
275244
* @returns {string} The wrapped string.
@@ -307,10 +276,11 @@ class AsciiTable3 {
307276

308277
/**
309278
* Truncates a string up to a maximum number of characters (if needed).
279+
* In case of truncation, '...' are appended to the end of the string.
310280
* @static
311281
* @param {string} str The string to truncate.
312282
* @param {number} maxSize The string maximum size.
313-
* @returns {string} The truncated string (in case of truncation, '...' are appended to the end).
283+
* @returns {string} The truncated string.
314284
*/
315285
static truncateString(str, maxSize) {
316286
const SUFIX = '...';
@@ -1296,7 +1266,7 @@ class AsciiTable3 {
12961266

12971267
// full table width
12981268
const maxWidth =
1299-
colsWidth.reduce(function(a, b) { return a + b; }, 0) + // data column sizes
1269+
colsWidth.reduce(function(a, b) { return a + b; }, 0) + // data column sizes
13001270
(colsWidth.length - 1) * strlen(style.borders.data.colSeparator); // mid column separators
13011271

13021272
var result = '';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ascii-table3",
3-
"version": "0.7.2",
3+
"version": "0.7.3",
44
"author": "João Simões <[email protected]> (https://github.com/AllMightySauron)",
55
"description": "Javascript ASCII renderer for beautiful console-based tables",
66
"repository": {

0 commit comments

Comments
 (0)