Skip to content

Commit

Permalink
dependency bump & typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
AllMightySauron authored and AllMightySauron committed Nov 25, 2022
1 parent 64d86cb commit 16889b2
Show file tree
Hide file tree
Showing 10 changed files with 675 additions and 43 deletions.
514 changes: 514 additions & 0 deletions dist/ascii-table3.d.ts

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions ascii-table3.d.ts → dist/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* @property {string} right The right border character.
* @property {string} colSeparator The column separator character.
*/
export type SectionStyle = {
export type SectionStyle = {
left: string,
center: string,
right: string,
colSeparator: string
};
}

/**
* Borders style definition.
Expand All @@ -26,7 +26,7 @@ export type SectionStyle = {
middle: SectionStyle,
bottom: SectionStyle,
data: SectionStyle
};
}

/**
* Type definition for a table style.
Expand All @@ -37,7 +37,7 @@ export type SectionStyle = {
export type Style = {
name: string,
borders: Borders
};
}

/**
* @typedef {object} ColumnFormatJSON
Expand All @@ -49,7 +49,7 @@ export type ColumnFormatJSON = {
aligns: number[],
widths: number[],
wrappings: boolean[]
};
}

/**
* @typedef {object} FormattingJSON
Expand All @@ -61,7 +61,7 @@ export type FormattingJSON = {
titleAlign: number,
columns: ColumnFormatJSON,
justify: boolean
};
}

/**
* @typedef {object} TableJSON
Expand All @@ -75,4 +75,4 @@ export type TableJSON = {
heading: string[],
rows: string[][],
formatting: FormattingJSON
};
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./ascii-table3');
module.exports = require('./src/ascii-table3');
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ascii-table3",
"version": "0.7.8",
"version": "0.8.0",
"author": "João Simões <[email protected]> (https://github.com/AllMightySauron)",
"description": "Javascript ASCII renderer for beautiful console-based tables",
"repository": {
Expand All @@ -20,17 +20,21 @@
}
],
"main": "index.js",
"types": "dist/ascii-table3.d.ts",
"engines": {
"node": ">=11.14.0"
},
"scripts": {
"qa": "npm run hint && npm run coverage",
"hint": "jshint ./",
"test": "mocha -R spec",
"coverage": "nyc mocha"
"coverage": "nyc mocha",
"build": "copyfiles -u 1 \"src/**/*.d.ts\" dist && npx tsc"
},
"devDependencies": {
"jshint": "^2.12.0",
"typescript": "^4.9.3",
"copyfiles": "^2.4.1",
"mocha": "^9.2.2",
"nyc": "^15.1.0"
},
Expand Down
15 changes: 12 additions & 3 deletions sample_list_dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// load modules
const fs = require('fs');
const chalk = require('chalk');
const { AsciiTable3 } = require('./ascii-table3');
const { AsciiTable3 } = require('./src/ascii-table3');

// get path to list from command line
const path = process.argv.length <= 2 ? '.' : process.argv[2];

// build table
const dirTable =
new AsciiTable3(`Directory: ${path}`)
.setHeading(chalk.red('Type'), 'Name', 'Size (bytes)', 'Last change')
.setHeading('Type', 'Name', 'Size (bytes)', 'Last change')
.setAlignRight(3).setWidth(4, 30);

try {
Expand All @@ -37,7 +37,16 @@ try {
const size = type == 'Directory' ? '-' : new Intl.NumberFormat().format(stat.size);

// add new table row
dirTable.addRow(chalk.green(type), chalk.blue(item), size, chalk.yellow(stat.ctime));
switch (type) {
case 'Directory':
dirTable.addRow(chalk.blue(type), chalk.blue(item), chalk.blue(size), chalk.blue(stat.ctime));
break;
case 'Socket':
dirTable.addRow(chalk.red(type), chalk.red(item), chalk.red(size), chalk.red(stat.ctime));
break;
default:
dirTable.addRow(type, item, size, stat.ctime);
}
} catch (err) {
console.log(`${err}`);
}
Expand Down
2 changes: 1 addition & 1 deletion samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// example 1: base table
console.log ('>>>>>> example 1. base table');

const { AsciiTable3 } = require('./ascii-table3');
const { AsciiTable3 } = require('./src/ascii-table3');

// create table
const table =
Expand Down
61 changes: 33 additions & 28 deletions ascii-table3.js → src/ascii-table3.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

/**
* 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
* @typedef { import("./types").SectionStyle } SectionStyle
* @typedef { import("./types").Borders } Borders
* @typedef { import("./types").Style } Style
* @typedef { import("./types").ColumnFormatJSON } ColumnFormatJSON
* @typedef { import("./types").FormattingJSON } FormattingJSON
* @typedef { import("./types").TableJSON } TableJSON
*/

/**
* Filename containing rendering styles.
*/
const STYLES_FILENAME = 'ascii-table3.styles.json';
const STYLES_FILENAME = '../ascii-table3.styles.json';

/**
* Alignment direction enum.
Expand Down Expand Up @@ -47,9 +47,10 @@ class AsciiTable3 {
}

/**
* Returns wether a value is numeric or not, irrespective of its type.
* Returns whether a value is numeric or not, irrespective of its type.
* @static
* @param {*} value Value to test.
* @returns {boolean} Whether the value is numeric or not.
*/
static isNumeric(value) {
return !isNaN(parseFloat(value)) && isFinite(value);
Expand All @@ -59,7 +60,7 @@ class AsciiTable3 {
* Pads the start of a string with a given string until the maximum length limit is reached.
* @param {string} str String to pad at the beggining.
* @param {number} maxLength The resulting string max lenght.
* @param {string} fillStr The new pad at the begginning.
* @param {string} fillStr The new pad at the begginning (optional, defaults to ' ').
* @returns {string} Start-padded string.
*/
static padStart(str, maxLength, fillStr = ' ') {
Expand Down Expand Up @@ -97,7 +98,7 @@ class AsciiTable3 {
* Pads the end of a string with a given string until the maximum length limit is reached.
* @param {string} str String to pad at the end.
* @param {number} maxLength The resulting string max lenght.
* @param {string} fillStr The new pad at the end.
* @param {string} fillStr The new pad at the end (optional, defaults to ' ').
* @returns {string} End-padded string.
*/
static padEnd(str, maxLength, fillStr = ' ') {
Expand Down Expand Up @@ -133,10 +134,11 @@ class AsciiTable3 {
/**
* Generic string alignment.
* @static
* @param {AlignmentEnum} direction The desired aligment direction according to the enum (left, right or center)
* @param {number} direction The desired aligment direction according to the enum (left, right or center).
* @param {*} value The value to align.
* @param {number} len The maximum alignment length.
* @param {string} pad The pad char (optional, defaults to ' ').
* @returns {string} Aligned string.
*/
static align(direction, value, len, pad = ' ') {
const strValue = '' + value;
Expand All @@ -158,6 +160,7 @@ class AsciiTable3 {
* @param {*} value The value to align.
* @param {number} len The maximum alignment length.
* @param {string} [pad] The pad char (optional, defaults to ' ').
* @returns {string} Left aligned string.
*/
static alignLeft(value, len, pad = ' ') {
return this.align(AlignmentEnum.LEFT, value, len, pad);
Expand All @@ -169,6 +172,7 @@ class AsciiTable3 {
* @param {*} value The value to align.
* @param {number} len The maximum alignment length.
* @param {string} [pad] The pad char (optional, defaults to ' ').
* @returns {string} Right aligned string.
*/
static alignRight(value, len, pad = ' ') {
return this.align(AlignmentEnum.RIGHT, value, len, pad);
Expand All @@ -180,6 +184,7 @@ class AsciiTable3 {
* @param {*} value The value to align.
* @param {number} len The maximum alignment length.
* @param {string} [pad] The pad char (optional, defaults to ' ').
* @returns {string} Center aligned string.
*/
static alignCenter(value, len, pad = ' ') {
return this.align(AlignmentEnum.CENTER, value, len, pad);
Expand All @@ -205,7 +210,7 @@ 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.
* @param {number} maxWidth The maximum width for the wrapped string.
* @returns {string} The wrapped string.
*/
static wordWrap(str, maxWidth) {
Expand Down Expand Up @@ -240,7 +245,7 @@ class AsciiTable3 {
/**
* 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.
* @param {number} maxWidth The maximum width for the wrapped string.
* @returns {string} The wrapped string.
*/
static wordWrapBasic(str, maxWidth) {
Expand Down Expand Up @@ -323,7 +328,7 @@ class AsciiTable3 {
/**
* Increases existing array size up to the desired limit.
* @static
* @param {Array} array The array to increase.
* @param {*[]} array The array to increase.
* @param {number} len The desired array size.
* @param {*} [value] The fill value (optional).
*/
Expand Down Expand Up @@ -427,8 +432,8 @@ class AsciiTable3 {

/**
* Title setter.
* @param {string} title The title to set.
* @returns {object} The AsciiTable3 object instance.
* @param {string} title The title to set (optional, defaults to '').
* @returns {AsciiTable3} The AsciiTable3 object instance.
*/
setTitle(title = '') {
this.title = title;
Expand All @@ -446,7 +451,7 @@ class AsciiTable3 {

/**
* Title alignment setter.
* @param {AlignmentEnum} direction The desired title aligment direction according to the enum (left, right or center)
* @param {number} direction The desired title aligment direction according to the enum (left, right or center).
* @returns {AsciiTable3} The AsciiTable3 object instance.
*/
setTitleAlign(direction) {
Expand Down Expand Up @@ -481,7 +486,7 @@ class AsciiTable3 {

/**
* Title alignment getter.
* @returns {AlignmentEnum} The table title alignment direction.
* @returns {number} The table title alignment direction.
*/
getTitleAlign() {
return this.titleAlignment;
Expand Down Expand Up @@ -514,7 +519,7 @@ class AsciiTable3 {

/**
* Heading alignment setter.
* @param {AlignmentEnum} direction The desired heading aligment direction according to the enum (left, right or center)
* @param {number} direction The desired heading aligment direction according to the enum (left, right or center).
* @returns {AsciiTable3} The AsciiTable3 object instance.
*/
setHeadingAlign(direction) {
Expand Down Expand Up @@ -549,7 +554,7 @@ class AsciiTable3 {

/**
* Heading alignment getter.
* @returns {AlignmentEnum} The instance heading alignment.
* @returns {number} The instance heading alignment.
*/
getHeadingAlign() {
return this.headingAlign;
Expand Down Expand Up @@ -608,7 +613,7 @@ class AsciiTable3 {

/**
* Bulk addRow operation.
* @param {*[]} rows Multidimensional array of rows.
* @param {*[][]} rows Multidimensional array of rows.
* @returns {AsciiTable3} The AsciiTable3 object instance.
*/
addRowMatrix(rows) {
Expand Down Expand Up @@ -729,7 +734,7 @@ class AsciiTable3 {
/**
* Sets the alignment direction for a given column.
* @param {number} idx Column index to align (starts at 1).
* @param {AlignmentEnum} direction Desired alignment direction.
* @param {number} direction Desired alignment direction.
* @returns {AsciiTable3} The AsciiTable3 object instance.
*/
setAlign(idx, direction) {
Expand All @@ -750,7 +755,7 @@ class AsciiTable3 {
/**
* Get the alignment direction for a given column.
* @param {number} idx Column index to align (starts at 1).
* @returns {AlignmentEnum} The alignment set for a column.
* @returns {number} The alignment set for a column.
*/
getAlign(idx) {
var result = AlignmentEnum.AUTO;
Expand All @@ -765,7 +770,7 @@ class AsciiTable3 {

/**
* Sets the alignment direction for all table columns.
* @param {AlignmentEnum[]} directions Desired alignment directions.
* @param {number[]} directions Desired alignment directions.
* @returns {AsciiTable3} The AsciiTable3 object instance.
*/
setAligns(directions) {
Expand All @@ -776,7 +781,7 @@ class AsciiTable3 {

/**
* Gets the alignment direction for all columns.
* @returns {AlignmentEnum[]} Array with alignment settings for all columns.
* @returns {number[]} Array with alignment settings for all columns.
*/
getAligns() {
if (!this.dataAlign) {
Expand Down Expand Up @@ -870,8 +875,8 @@ class AsciiTable3 {
}

/**
* Gets the alignment direction for all columns.
* @returns {AlignmentEnum[]} Array with alignment settings for all columns.
* Gets the wrapping settings for all columns.
* @returns {boolean[]} Array with wrapping settings for all columns.
*/
getWrappings() {
if (!this.wrapping) {
Expand Down Expand Up @@ -1120,7 +1125,7 @@ class AsciiTable3 {
* Get array of wrapped row data from a "normal" row.
* @private
* @param {*[]} row Row of data.
* @returns Array of data rows after word wrapping.
* @returns {string[]} Array of data rows after word wrapping.
*/
getWrappedRows(row) {
// setup a new wrapped row
Expand Down
Loading

0 comments on commit 16889b2

Please sign in to comment.