table-layout
Advanced tools
Comparing version 2.1.0 to 3.0.0
{ | ||
"name": "table-layout", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Stylable text tables, handling ansi colour. Useful for console output.", | ||
"repository": "https://github.com/75lb/table-layout.git", | ||
"repository": "https://github.com/75lb/table-layout", | ||
"license": "MIT", | ||
@@ -15,32 +15,54 @@ "keywords": [ | ||
"json", | ||
"command line" | ||
"command line", | ||
"table", | ||
"view" | ||
], | ||
"main": "index.mjs", | ||
"type": "module", | ||
"exports": { | ||
"import": "./index.js", | ||
"require": "./dist/index.cjs" | ||
}, | ||
"bin": "./bin/cli.js", | ||
"engines": { | ||
"node": ">=14" | ||
"node": ">=12.17" | ||
}, | ||
"scripts": { | ||
"test": "npm run dist && test-runner test/*.mjs", | ||
"docs": "jsdoc2md -c jsdoc.conf -t README.hbs index.mjs -p list --member-index-format list > README.md; echo", | ||
"cover": "nyc npm test && nyc report --reporter=text-lcov | coveralls", | ||
"dist": "rollup -c" | ||
"test": "npm run dist && npm run test:ci", | ||
"test:ci": "test-runner 'test/**/*.js'", | ||
"docs": "jsdoc2md index.js -p list --member-index-format list > docs/API.md", | ||
"cover": "c8 npm test", | ||
"dist": "rollup -c", | ||
"examples": "for FILE in example/*.json; do echo \"# $FILE\"; echo; cat $FILE | node bin/cli.js; done" | ||
}, | ||
"dependencies": { | ||
"array-back": "^5.0.0", | ||
"typical": "^6.0.1", | ||
"wordwrapjs": "^5.0.1" | ||
"@75lb/deep-merge": "^1.1.0", | ||
"array-back": "^6.2.0", | ||
"command-line-args": "^5.2.0", | ||
"command-line-usage": "^6.1.1", | ||
"stream-read-all": "^3.0.1", | ||
"typical": "^7.1.1", | ||
"wordwrapjs": "^5.1.0" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^3.1.0", | ||
"jsdoc-to-markdown": "^7.0.0", | ||
"nyc": "^15.1.0", | ||
"rollup": "^2.42.3", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"test-runner": "^0.8.14" | ||
"@rollup/plugin-commonjs": "^20.0.0", | ||
"@rollup/plugin-node-resolve": "^13.0.4", | ||
"ansi-escape-sequences": "^6.2.0", | ||
"c8": "^7.8.0", | ||
"jsdoc-to-markdown": "^7.0.1", | ||
"rollup": "^2.56.3", | ||
"test-runner": "^0.9.8" | ||
}, | ||
"files": [ | ||
"index.mjs", | ||
"lib/*.mjs", | ||
"dist" | ||
] | ||
"index.js", | ||
"lib", | ||
"dist", | ||
"bin" | ||
], | ||
"standard": { | ||
"ignore": [ | ||
"tmp", | ||
"dist" | ||
], | ||
"envs": [] | ||
} | ||
} |
378
README.md
@@ -5,4 +5,3 @@ [![view on npm](https://badgen.net/npm/v/table-layout)](https://www.npmjs.org/package/table-layout) | ||
[![Gihub package dependents](https://badgen.net/github/dependents-pkg/75lb/table-layout)](https://github.com/75lb/table-layout/network/dependents?dependent_type=PACKAGE) | ||
[![Build Status](https://travis-ci.org/75lb/table-layout.svg?branch=master)](https://travis-ci.org/75lb/table-layout) | ||
[![Coverage Status](https://coveralls.io/repos/github/75lb/table-layout/badge.svg)](https://coveralls.io/github/75lb/table-layout) | ||
[![Node.js CI](https://github.com/75lb/table-layout/actions/workflows/node.js.yml/badge.svg)](https://github.com/75lb/table-layout/actions/workflows/node.js.yml) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard) | ||
@@ -12,11 +11,218 @@ | ||
Generates plain-text tables from JSON recordset input (array of objects). Useful for presenting text in column layout or data in table layout in text-based user interfaces. Also [available as a command-line tool](https://github.com/75lb/table-layout-cli). | ||
Styleable plain-text table generator. Useful for formatting console output. Available as both a command-line tool and isomorphic Javascript library. | ||
## Synopsis | ||
Where input looks like this: | ||
Install the `table-layout` command-line app: | ||
``` | ||
$ npm install --global table-layout | ||
``` | ||
As input, table-layout takes a JSON file containing an array of objects. | ||
```json | ||
[ | ||
{ | ||
"country": "USA", | ||
"GDP": "$19,485,394,000,000", | ||
"population": "325,084,756" | ||
}, | ||
{ | ||
"country": "China", | ||
"GDP": "$12,237,700,479,375", | ||
"population": "1,421,021,791" | ||
}, | ||
{ | ||
"country": "Japan", | ||
"GDP": "$4,872,415,104,315", | ||
"population": "127,502,725" | ||
} | ||
] | ||
``` | ||
The output of table-layout is a configurable plain-text table. | ||
``` | ||
$ cat example/gdp.json | table-layout | ||
USA $19,485,394,000,000 325,084,756 | ||
China $12,237,700,479,375 1,421,021,791 | ||
Japan $4,872,415,104,315 127,502,725 | ||
``` | ||
## Examples | ||
Please see below for example usage on the command-line. Programmatic examples are further down the page. | ||
### View Github issues | ||
A quick way to see the latest issues on a repository (this example requires [jq](https://stedolan.github.io/jq/)). | ||
```sh | ||
$ curl -s https://api.github.com/repos/npm/npm/issues \ | ||
| jq 'map({ number, title, login:.user.login, comments })' \ | ||
| table-layout | ||
``` | ||
``` | ||
10263 npm run start Slepperpon 4 | ||
10262 npm-shrinkwrap.json being ignored for a dependency of a maxkorp 0 | ||
dependency (2.14.9, 3.3.10) | ||
10261 EPROTO Error Installing Packages azkaiart 2 | ||
10260 ENOENT during npm install with npm v3.3.6/v3.3.12 and lencioni 2 | ||
node v5.0.0 | ||
10259 npm install failed geraldvillorente 1 | ||
10258 npm moves common dependencies under a dependency on trygveaa 2 | ||
install | ||
10257 [NPM3] Missing top level dependencies after npm install naholyr 0 | ||
10256 Yo meanjs app creation problem nrjkumar41 0 | ||
10254 sapnwrfc is not installing RamprasathS 0 | ||
10253 npm install deep dependence folder "node_modules" duyetvv 2 | ||
10251 cannot npm login w0ps 2 | ||
10250 Update npm-team.md louislarry 0 | ||
10248 cant install module I created nousacademy 4 | ||
10247 Cannot install passlib nicola883 3 | ||
10246 Error installing Gulp AlanIsrael0 1 | ||
10245 cannot install packages through NPM RoyGeagea 11 | ||
10244 Remove arguments from npm-dedupe.md bengotow 0 | ||
etc. | ||
etc. | ||
``` | ||
### Format an article into columns | ||
Formatting long chunks of text into columns for display side by side. | ||
```json | ||
[ | ||
{ | ||
"column 1": "The Kingdom of Scotland was a state in north-west Europe traditionally said to have been founded in 843, which joined with the Kingdom of England to form a unified Kingdom of Great Britain in 1707. Its territories expanded and shrank, but it came to occupy the northern third of the island of Great Britain, sharing a land border to the south with the Kingdom of England. ", | ||
"column 2": "Operation Barbarossa (German: Unternehmen Barbarossa) was the code name for Nazi Germany's invasion of the Soviet Union during World War II, which began on 22 June 1941. Over the course of the operation, about four million soldiers of the Axis powers invaded Soviet Russia along a 2,900 kilometer front, the largest invasion force in the history of warfare. In addition to troops, the Germans employed some 600,000 motor vehicles and between 600–700,000 horses." | ||
} | ||
] | ||
``` | ||
Output: | ||
``` | ||
$ cat example/two-columns.json | table-layout | ||
The Kingdom of Scotland was a state in Operation Barbarossa (German: Unternehmen | ||
north-west Europe traditionally said to Barbarossa) was the code name for Nazi | ||
have been founded in 843, which joined Germany's invasion of the Soviet Union | ||
with the Kingdom of England to form a during World War II, which began on 22 | ||
unified Kingdom of Great Britain in 1707. June 1941. Over the course of the | ||
Its territories expanded and shrank, but operation, about four million soldiers of | ||
it came to occupy the northern third of the Axis powers invaded Soviet Russia | ||
the island of Great Britain, sharing a along a 2,900 kilometer front, the | ||
land border to the south with the Kingdom largest invasion force in the history of | ||
of England. warfare. In addition to troops, the | ||
Germans employed some 600,000 motor | ||
vehicles and between 600–700,000 horses. | ||
``` | ||
Notice the columns above have equal width - this is the default style. You can give one or more columns a specific width using the `--width` option. In the example below we give "column 2" a specific width of 55 characters: | ||
``` | ||
$ cat example/two-columns.json | table-layout --width "column 2: 55" | ||
The Kingdom of Scotland was a Operation Barbarossa (German: Unternehmen Barbarossa) | ||
state in north-west Europe was the code name for Nazi Germany's invasion of the | ||
traditionally said to have Soviet Union during World War II, which began on 22 | ||
been founded in 843, which June 1941. Over the course of the operation, about | ||
joined with the Kingdom of four million soldiers of the Axis powers invaded | ||
England to form a unified Soviet Russia along a 2,900 kilometer front, the | ||
Kingdom of Great Britain in largest invasion force in the history of warfare. In | ||
1707. Its territories addition to troops, the Germans employed some 600,000 | ||
expanded and shrank, but it motor vehicles and between 600–700,000 horses. | ||
came to occupy the northern | ||
third of the island of Great | ||
Britain, sharing a land | ||
border to the south with the | ||
Kingdom of England. | ||
``` | ||
## Include configuration options with the input | ||
Table-layout expects an array of objects as the input. However, if the input data is a plain object with the fields `options` and `data` defined then these properties will be used as the table options and data respectively. Example: | ||
``` | ||
{ | ||
"options": { | ||
"noTrim": true, | ||
"columns": [ | ||
{ "name": "name", "width": 25 }, | ||
{ "name": "description", "width": 20 }, | ||
{ "name": "html_url", "width": 20, "break": true }, | ||
{ "name": "stargazers_count", "width": 5 } | ||
] | ||
}, | ||
"data": [ | ||
... | ||
] | ||
} | ||
``` | ||
Please see [this folder]() for examples of input containing both options and data. | ||
## Full command-line usage guide: | ||
``` | ||
table-layout | ||
Styleable plain-text table generator. Useful for formatting console output. | ||
Synopsis | ||
$ cat json-file | table-layout [options] | ||
$ table-layout [options] json-file | ||
Options | ||
--file string A JSON input file to read. If not present, table-layout will look for input | ||
on stdin. | ||
-w, --width widths specify a list of column widths in the format '<column>:<width>', for | ||
example: | ||
$ cat <file> | table-layout --width "column 1: 10" "column 2: 30" | ||
-l, --padding-left string One or more characters to pad the left of each column. Defaults to ' '. | ||
-r, --padding-right string One or more characters to pad the right of each column. Defaults to ' '. | ||
-h, --help | ||
``` | ||
## Programmatic Usage | ||
Using the table-layout API directly from Javascript gives you a little more power and flexibility over the output. | ||
### Install as a library | ||
Add table-output to your project: | ||
``` | ||
$ npm install --save table-layout | ||
``` | ||
### Display an array of objects as a table | ||
Trivial example. Read a JSON file from disk and output a table with a maximum width (in characters) of 60. | ||
```js | ||
import Table from 'table-layout' | ||
import { promises as fs } from 'fs' | ||
const issues = await fs.readFile('./issues.json', 'utf8') | ||
const table = new Table(JSON.parse(issues), { maxWidth: 60 }) | ||
console.log(table.toString()) | ||
``` | ||
An example input file: | ||
```json | ||
[ | ||
{ | ||
"number": 15134, | ||
@@ -48,14 +254,5 @@ "title": "Coveralls has no source available ", | ||
This code... | ||
This is the output: | ||
```js | ||
const Table = require('table-layout') | ||
const issues = require('./issues.json') | ||
const table = new Table(issues, { maxWidth: 60 }) | ||
console.log(table.toString()) | ||
``` | ||
...produces this output: | ||
``` | ||
15134 Coveralls has no source available ndelangen 0 | ||
@@ -78,110 +275,103 @@ 15133 Fixing --preserve-symlinks. phestermcs 0 | ||
## Load anywhere | ||
### Cherry-picked and computed values | ||
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation. | ||
Sometimes, your input data might contain a deeper structure or you want to transform or compute some values. Some example input data with structural depth and large numbers you'd like to reformat: | ||
Node.js: | ||
```js | ||
const TableLayout = require('table-layout') | ||
```json | ||
[ | ||
{ | ||
"country": { "name": "USA" }, | ||
"GDP": 19485394000000, | ||
"population": 325084756 | ||
}, | ||
{ | ||
"country": { "name": "China" }, | ||
"GDP": 12237700479375, | ||
"population": 1421021791 | ||
}, | ||
{ | ||
"country": { "name": "Japan" }, | ||
"GDP": 4872415104315, | ||
"population": 127502725 | ||
} | ||
] | ||
``` | ||
Within Node.js with ECMAScript Module support enabled: | ||
Example usage of the column getter function: | ||
```js | ||
import TableLayout from 'table-layout' | ||
``` | ||
import Table from 'table-layout' | ||
import { promises as fs } from 'fs' | ||
Within an modern browser ECMAScript Module: | ||
const rows = JSON.parse(await fs.readFile('./example/deep-data/gdp.json', 'utf8')) | ||
const germanCurrency = new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }) | ||
const germanNumber = new Intl.NumberFormat('de-DE', { notation: 'compact', maximumSignificantDigits: 3, maximumFractionDigits: 0 }) | ||
```js | ||
import TableLayout from './node_modules/table-layout/dist/index.mjs' | ||
const table = new Table(rows, { | ||
maxWidth: 60, | ||
columns: [ | ||
{ | ||
name: 'country', | ||
get: (cellValue) => cellValue.name | ||
}, | ||
{ | ||
name: 'GDP', | ||
get: (cellValue) => germanCurrency.format(cellValue) | ||
}, | ||
{ | ||
name: 'population', | ||
get: (cellValue) => germanNumber.format(cellValue) | ||
}, | ||
] | ||
}) | ||
console.log(table.toString()) | ||
``` | ||
Old browser (adds `window.TableLayout`): | ||
Output. | ||
```html | ||
<script nomodule src="./node_modules/table-layout/dist/index.js"></script> | ||
``` | ||
$ node example/computed-values.js | ||
## API Reference | ||
USA 19.485.394.000.000,00 € 325 Mio. | ||
China 12.237.700.479.375,00 € 1,42 Mrd. | ||
Japan 4.872.415.104.315,00 € 128 Mio. | ||
``` | ||
* [table-layout](#module_table-layout) | ||
* [Table](#exp_module_table-layout--Table) ⏏ | ||
* [new Table(data, [options])](#new_module_table-layout--Table_new) | ||
* [table.renderLines()](#module_table-layout--Table+renderLines) ⇒ <code>Array.<string></code> | ||
* [table.toString()](#module_table-layout--Table+toString) ⇒ <code>string</code> | ||
* [Table~columnOption](#module_table-layout--Table..columnOption) | ||
### Colour-scale conditional formatting | ||
<a name="exp_module_table-layout--Table"></a> | ||
See [this file](https://github.com/75lb/table-layout/blob/master/example/colour-scale-formatting.js) for a example of colour-scale formatting (increasing intensity of red/green for more positive/negative values). | ||
### Table ⏏ | ||
Recordset data in (array of objects), text table out. | ||
<img src="https://s9.gifyu.com/images/Screenshot-2021-09-04-at-17.53.16.png" width="600px" title="Colour-scale"> | ||
**Kind**: Exported class | ||
<a name="new_module_table-layout--Table_new"></a> | ||
### API Reference | ||
#### new Table(data, [options]) | ||
**Params** | ||
For the full API documentation, see [here](https://github.com/75lb/table-layout/blob/master/docs/API.md). | ||
- data <code>Array.<object></code> - input data | ||
- [options] <code>object</code> - optional settings | ||
- [.maxWidth] <code>number</code> - maximum width of layout | ||
- [.noWrap] <code>boolean</code> - disable wrapping on all columns | ||
- [.noTrim] <code>boolean</code> - disable line-trimming | ||
- [.break] <code>boolean</code> - enable word-breaking on all columns | ||
- [.columns] [<code>columnOption</code>](#module_table-layout--Table..columnOption) - array of column-specific options | ||
- [.ignoreEmptyColumns] <code>boolean</code> - if set, empty columns or columns containing only whitespace are not rendered. | ||
- [.padding] <code>object</code> - Padding values to set on each column. Per-column overrides can be set in the `options.columns` array. | ||
- [.left] <code>string</code> - Defaults to a single space. | ||
- [.right] <code>string</code> - Defaults to a single space. | ||
- [.eol] <code>string</code> - EOL character used. Defaults to `\n`. | ||
## Load anywhere | ||
**Example** | ||
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation. | ||
Within a Node.js CommonJS Module: | ||
```js | ||
> Table = require('table-layout') | ||
> jsonData = [{ | ||
col1: 'Some text you wish to read in table layout', | ||
col2: 'And some more text in column two. ' | ||
}] | ||
> table = new Table(jsonData, { maxWidth: 30 }) | ||
> console.log(table.toString()) | ||
Some text you And some more | ||
wish to read text in | ||
in table column two. | ||
layout | ||
const TableLayout = require('table-layout') | ||
``` | ||
<a name="module_table-layout--Table+renderLines"></a> | ||
#### table.renderLines() ⇒ <code>Array.<string></code> | ||
Identical to `.toString()` with the exception that the result will be an array of lines, rather than a single, multi-line string. | ||
Within a Node.js ECMAScript Module: | ||
**Kind**: instance method of [<code>Table</code>](#exp_module_table-layout--Table) | ||
<a name="module_table-layout--Table+toString"></a> | ||
```js | ||
import TableLayout from 'table-layout' | ||
``` | ||
#### table.toString() ⇒ <code>string</code> | ||
Returns the input data as a text table. | ||
Within a modern browser ECMAScript Module: | ||
**Kind**: instance method of [<code>Table</code>](#exp_module_table-layout--Table) | ||
<a name="module_table-layout--Table..columnOption"></a> | ||
```js | ||
import TableLayout from './node_modules/table-layout/dist/index.mjs' | ||
``` | ||
#### Table~columnOption | ||
**Kind**: inner typedef of [<code>Table</code>](#exp_module_table-layout--Table) | ||
**Properties** | ||
* * * | ||
| Name | Type | Description | | ||
| --- | --- | --- | | ||
| name | <code>string</code> | column name, must match a property name in the input | | ||
| [width] | <code>number</code> | A specific column width. Supply either this or a min and/or max width. | | ||
| [minWidth] | <code>number</code> | column min width | | ||
| [maxWidth] | <code>number</code> | column max width | | ||
| [nowrap] | <code>boolean</code> | disable wrapping for this column | | ||
| [break] | <code>boolean</code> | enable word-breaking for this columns | | ||
| [padding] | <code>object</code> | padding options | | ||
| [padding.left] | <code>string</code> | a string to pad the left of each cell (default: `' '`) | | ||
| [padding.right] | <code>string</code> | a string to pad the right of each cell (default: `' '`) | | ||
© 2015-21 [Lloyd Brookes](https://github.com/75lb) \<75pound@gmail.com\>. | ||
* * * | ||
© 2015-21 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). | ||
Isomorphic test suite by [test-runner](https://github.com/test-runner-js/test-runner) and [web-runner](https://github.com/test-runner-js/web-runner). Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
118806
16
3578
374
Yes
7
7
2
+ Added@75lb/deep-merge@^1.1.0
+ Addedcommand-line-args@^5.2.0
+ Addedcommand-line-usage@^6.1.1
+ Addedstream-read-all@^3.0.1
+ Added@75lb/deep-merge@1.1.2(transitive)
+ Addedansi-styles@3.2.1(transitive)
+ Addedarray-back@3.1.04.0.26.2.2(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedcommand-line-args@5.2.1(transitive)
+ Addedcommand-line-usage@6.1.3(transitive)
+ Addeddeep-extend@0.6.0(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedfind-replace@3.0.0(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlodash.camelcase@4.3.0(transitive)
+ Addedreduce-flatten@2.0.0(transitive)
+ Addedstream-read-all@3.0.1(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedtable-layout@1.0.2(transitive)
+ Addedtypical@4.0.05.2.07.2.0(transitive)
+ Addedwordwrapjs@4.0.1(transitive)
- Removedarray-back@5.0.0(transitive)
- Removedtypical@6.0.1(transitive)
Updatedarray-back@^6.2.0
Updatedtypical@^7.1.1
Updatedwordwrapjs@^5.1.0