tablemark
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -19,2 +19,3 @@ import { getColumnAlignments, getColumnTitles, getColumnWidths, line, normalizeOptions, row } from "./utilities.js"; | ||
table += row(alignments, widths, titles, config); | ||
const dashLengthOffset = config.padHeaderSeparator ? 2 : 0; | ||
// header separator | ||
@@ -24,6 +25,9 @@ table += line(alignments.map((align, i) => (align === alignmentOptions.left || align === alignmentOptions.center | ||
: "-") + | ||
"-".repeat(widths[i] - 2) + | ||
"-".repeat(widths[i] - dashLengthOffset) + | ||
(align === alignmentOptions.right || align === alignmentOptions.center | ||
? ":" | ||
: "-")), config, true); | ||
: "-")), config, { | ||
forceGutters: true, | ||
isHeaderSeparator: true | ||
}); | ||
// table body | ||
@@ -30,0 +34,0 @@ table += input |
@@ -5,4 +5,4 @@ import type { alignmentOptions } from "./index.js"; | ||
} | ||
export declare type InputData<T = LooseObject> = T[]; | ||
export declare type Alignment = keyof typeof alignmentOptions | Uppercase<keyof typeof alignmentOptions>; | ||
export type InputData<T = LooseObject> = T[]; | ||
export type Alignment = keyof typeof alignmentOptions | Uppercase<keyof typeof alignmentOptions>; | ||
export interface ColumnDescriptor { | ||
@@ -18,3 +18,3 @@ /** | ||
} | ||
export declare type ToCellText = (value: unknown) => string; | ||
export type ToCellText = (value: unknown) => string; | ||
export interface TablemarkOptions { | ||
@@ -54,4 +54,8 @@ /** | ||
lineEnding?: string; | ||
/** | ||
* Include padding on the header separator row, `true` by default. | ||
*/ | ||
padHeaderSeparator?: boolean; | ||
} | ||
export declare type TablemarkOptionsNormalized = Omit<{ | ||
export type TablemarkOptionsNormalized = Omit<{ | ||
[key in keyof TablemarkOptions]-?: TablemarkOptions[key]; | ||
@@ -58,0 +62,0 @@ }, "columns"> & { |
import type { Alignment, InputData, ToCellText, TablemarkOptions, TablemarkOptionsNormalized } from "./types.js"; | ||
export declare const pad: (alignment: Alignment, width: number, content: string) => string; | ||
export declare const toCellText: ToCellText; | ||
export declare const line: (columns: readonly string[], config: TablemarkOptionsNormalized, forceGutters?: boolean) => string; | ||
export declare const line: (columns: readonly string[], config: TablemarkOptionsNormalized, { forceGutters, isHeaderSeparator }?: { | ||
forceGutters?: boolean | undefined; | ||
isHeaderSeparator?: boolean | undefined; | ||
}) => string; | ||
export declare const row: (alignments: readonly Alignment[], widths: readonly number[], columns: readonly string[], config: TablemarkOptionsNormalized) => string; | ||
@@ -6,0 +9,0 @@ export declare const normalizeOptions: (options: TablemarkOptions) => TablemarkOptionsNormalized; |
@@ -28,7 +28,11 @@ import { sentenceCase } from "sentence-case"; | ||
}; | ||
export const line = (columns, config, forceGutters = false) => { | ||
export const line = (columns, config, { forceGutters = false, isHeaderSeparator = false } = {}) => { | ||
const gutters = forceGutters ? true : config.wrapWithGutters; | ||
return ((gutters ? "| " : " ") + | ||
columns.join(gutters ? " | " : " ") + | ||
(gutters ? " |" : " ") + | ||
let padding = " "; | ||
if (isHeaderSeparator && !config.padHeaderSeparator) { | ||
padding = ""; | ||
} | ||
return ((gutters ? `|${padding}` : ` ${padding}`) + | ||
columns.join(gutters ? `${padding}|${padding}` : `${padding} ${padding}`) + | ||
(gutters ? `${padding}|` : `${padding} `) + | ||
config.lineEnding); | ||
@@ -48,6 +52,6 @@ }; | ||
if (height === 1) { | ||
return line(first, config, true); | ||
return line(first, config, { forceGutters: true }); | ||
} | ||
const lines = new Array(height); | ||
lines[0] = line(first, config, true); | ||
lines[0] = line(first, config, { forceGutters: true }); | ||
for (let v = 1; v < height; v++) { | ||
@@ -78,3 +82,4 @@ lines[v] = new Array(width); | ||
wrapWidth: Infinity, | ||
wrapWithGutters: false | ||
wrapWithGutters: false, | ||
padHeaderSeparator: true | ||
}; | ||
@@ -81,0 +86,0 @@ Object.assign(defaults, options); |
{ | ||
"name": "tablemark", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Generate markdown tables from a list of objects or JSON data.", | ||
"author": "Bo Lingen <lingenbw@gmail.com> (https://github.com/citycide)", | ||
"author": "Bo Lingen <bo@haltcase.dev> (https://github.com/haltcase)", | ||
"license": "MIT", | ||
"repository": "https://github.com/citycide/tablemark", | ||
"homepage": "https://github.com/citycide/tablemark", | ||
"bugs": "https://github.com/citycide/tablemark/issues", | ||
"repository": "https://github.com/haltcase/tablemark", | ||
"homepage": "https://github.com/haltcase/tablemark", | ||
"bugs": "https://github.com/haltcase/tablemark/issues", | ||
"engines": { | ||
@@ -31,10 +31,2 @@ "node": ">=14.16" | ||
], | ||
"scripts": { | ||
"lint": "ts-standardx src && cd tests && ts-standardx .", | ||
"build": "tsc", | ||
"pretest": "npm run build", | ||
"test": "ava", | ||
"changelog": "changelog", | ||
"prepublishOnly": "npm run lint && npm test" | ||
}, | ||
"devDependencies": { | ||
@@ -51,3 +43,10 @@ "@citycide/changelog": "^2.0.0", | ||
"split-text-to-chunks": "^1.0.0" | ||
}, | ||
"scripts": { | ||
"lint": "ts-standardx src && cd tests && ts-standardx .", | ||
"build": "tsc", | ||
"pretest": "npm run build", | ||
"test": "ava", | ||
"changelog": "changelog" | ||
} | ||
} | ||
} |
144
readme.md
@@ -1,2 +0,2 @@ | ||
# tablemark · [![Version](https://flat.badgen.net/npm/v/tablemark)](https://www.npmjs.com/package/tablemark) [![License](https://flat.badgen.net/npm/license/tablemark)](https://www.npmjs.com/package/tablemark) [![TypeScript](https://flat.badgen.net/badge/written%20in/TypeScript/294E80)](http://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html) [![GitHub Actions](https://flat.badgen.net/github/checks/citycide/tablemark)](https://github.com/citycide/tablemark/actions) | ||
# tablemark · [![Version](https://flat.badgen.net/npm/v/tablemark)](https://www.npmjs.com/package/tablemark) [![License](https://flat.badgen.net/npm/license/tablemark)](https://www.npmjs.com/package/tablemark) [![TypeScript](https://flat.badgen.net/badge/written%20in/TypeScript/294E80)](http://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html) [![GitHub Actions](https://flat.badgen.net/github/checks/haltcase/tablemark)](https://github.com/haltcase/tablemark/actions) | ||
@@ -40,2 +40,3 @@ > Generate markdown tables from JSON data. | ||
<!-- prettier-ignore-start --> | ||
| Name | Age | Is cool | | ||
@@ -46,2 +47,3 @@ | :---- | :---- | :------ | | ||
| Lee | 23 | true | | ||
<!-- prettier-ignore-end --> | ||
@@ -56,14 +58,15 @@ ## api | ||
* `InputData` input: the data to table-ify | ||
* an array of objects or iterables | ||
* `TablemarkOptions` options: | ||
- `InputData` input: the data to table-ify | ||
- an array of objects or iterables | ||
- `TablemarkOptions` options: | ||
| key | type | default | description | | ||
| :------------: | :----------: | :--------: | ---------------------------------------------- | | ||
| `columns` | `Array<string \| ColumnDescriptor>` | - | Array of column descriptors. | | ||
| `caseHeaders` | `boolean` | `true` | Sentence case headers derived from keys. | | ||
| `toCellText` | `(input: unknown) => string` | - | Provide a custom "toString" function. | | ||
| `wrapWidth` | `number` | `Infinity` | Wrap cell text at this length. | | ||
| `wrapWithGutters` | `boolean` | `false` | Add sides (`\| <content> \|`) to wrapped rows. | | ||
| `lineEnding` | `string` | `"\n"` | String used at end-of-line. | | ||
| key | type | default | description | | ||
| :------------------: | :---------------------------------: | :--------: | --------------------------------------------------------------- | | ||
| `columns` | `Array<string \| ColumnDescriptor>` | - | Array of column descriptors. | | ||
| `caseHeaders` | `boolean` | `true` | Sentence case headers derived from keys. | | ||
| `toCellText` | `(input: unknown) => string` | - | Provide a custom "toString" function. | | ||
| `padHeaderSeparator` | `boolean` | `true` | Whether to pad gutters of the header separator (alignment) row. | | ||
| `wrapWidth` | `number` | `Infinity` | Wrap cell text at this length. | | ||
| `wrapWithGutters` | `boolean` | `false` | Add sides (`\| <content> \|`) to wrapped rows. | | ||
| `lineEnding` | `string` | `"\n"` | String used at end-of-line. | | ||
@@ -88,13 +91,16 @@ The `columns` array can either contain objects, in which case their | ||
```js | ||
tablemark([ | ||
{ name: "Bob", age: 21, isCool: false }, | ||
{ name: "Sarah", age: 22, isCool: true }, | ||
{ name: "Lee", age: 23, isCool: true } | ||
], { | ||
columns: [ | ||
"first name", | ||
{ name: "how old", align: "center" }, | ||
"are they cool" | ||
] | ||
}) | ||
tablemark( | ||
[ | ||
{ name: "Bob", age: 21, isCool: false }, | ||
{ name: "Sarah", age: 22, isCool: true }, | ||
{ name: "Lee", age: 23, isCool: true } | ||
], | ||
{ | ||
columns: [ | ||
"first name", | ||
{ name: "how old", align: "center" }, | ||
"are they cool" | ||
] | ||
} | ||
) | ||
@@ -110,2 +116,3 @@ // | first name | how old | are they cool | | ||
<!-- prettier-ignore-start --> | ||
| first name | how old | are they cool | | ||
@@ -116,20 +123,51 @@ | :--------- | :-----: | :------------ | | ||
| Lee | 23 | true | | ||
<!-- prettier-ignore-end --> | ||
### `options.padHeaderSeparator` | ||
```js | ||
tablemark( | ||
[ | ||
{ name: "Bob", age: 21, isCool: false }, | ||
{ name: "Sarah", age: 22, isCool: true }, | ||
{ name: "Lee", age: 23, isCool: true } | ||
], | ||
{ | ||
columns: [{ align: "left" }, { align: "center" }, { align: "right" }] | ||
} | ||
) | ||
// | first name | how old | are they cool | | ||
// |:-----------|:-------:|--------------:| | ||
// | Bob | 21 | false | | ||
// | Sarah | 22 | true | | ||
// | Lee | 23 | true | | ||
``` | ||
... displays as: | ||
<!-- prettier-ignore-start --> | ||
| first name | how old | are they cool | | ||
|:-----------|:-------:|--------------:| | ||
| Bob | 21 | false | | ||
| Sarah | 22 | true | | ||
| Lee | 23 | true | | ||
<!-- prettier-ignore-end --> | ||
### `options.toCellText` | ||
```js | ||
tablemark([ | ||
{ name: "Bob", pet_owner: true, studying: false }, | ||
{ name: "Sarah", pet_owner: false, studying: true }, | ||
{ name: "Sarah", pet_owner: true, studying: true } | ||
], { | ||
toCellText, | ||
columns: [ | ||
{ align: "left" }, | ||
{ align: "center" }, | ||
{ align: "center" } | ||
] | ||
}) | ||
tablemark( | ||
[ | ||
{ name: "Bob", pet_owner: true, studying: false }, | ||
{ name: "Sarah", pet_owner: false, studying: true }, | ||
{ name: "Sarah", pet_owner: true, studying: true } | ||
], | ||
{ | ||
toCellText, | ||
columns: [{ align: "left" }, { align: "center" }, { align: "center" }] | ||
} | ||
) | ||
function toCellText (v) { | ||
function toCellText(v) { | ||
if (v === true) return "✔" | ||
@@ -153,6 +191,9 @@ if (!v) return "" | ||
```js | ||
tablemark([ | ||
{ star: false, name: "Benjamin" }, | ||
{ star: true, name: "Jet Li" } | ||
], { wrapWidth: 5 }) | ||
tablemark( | ||
[ | ||
{ star: false, name: "Benjamin" }, | ||
{ star: true, name: "Jet Li" } | ||
], | ||
{ wrapWidth: 5 } | ||
) | ||
@@ -168,4 +209,4 @@ // | Star | Name | | ||
> To output valid [GitHub Flavored Markdown](https://github.github.com/gfm/) a | ||
cell must not contain newlines. Consider replacing those with `<br />` (e.g., | ||
using `options.toCellText`). | ||
> cell must not contain newlines. Consider replacing those with `<br />` (e.g., | ||
> using `options.toCellText`). | ||
@@ -177,6 +218,9 @@ ### `options.wrapWithGutters` | ||
```js | ||
tablemark([ | ||
{ star: false, name: "Benjamin" }, | ||
{ star: true, name: "Jet Li" } | ||
], { wrapWidth: 5, wrapWithGutters: true }) | ||
tablemark( | ||
[ | ||
{ star: false, name: "Benjamin" }, | ||
{ star: true, name: "Jet Li" } | ||
], | ||
{ wrapWidth: 5, wrapWithGutters: true } | ||
) | ||
@@ -193,7 +237,7 @@ // | Star | Name | | ||
* [`tablemark-cli`](https://github.com/citycide/tablemark-cli) – use this module from the command line | ||
- [`tablemark-cli`](https://github.com/haltcase/tablemark-cli) – use this module from the command line | ||
## contributing | ||
Search the [issues](https://github.com/citycide/tablemark) if you come | ||
Search the [issues](https://github.com/haltcase/tablemark) if you come | ||
across any trouble, open a new one if it hasn't been posted, or, if you're | ||
@@ -205,3 +249,3 @@ able, open a [pull request](https://help.github.com/articles/about-pull-requests/). | ||
* Thomas Jensen (**[@tjconcept](https://github.com/tjconcept)**) | ||
- Thomas Jensen (**[@tjconcept](https://github.com/tjconcept)**) | ||
@@ -212,2 +256,2 @@ Thank you! | ||
MIT © Bo Lingen / citycide | ||
MIT © Bo Lingen / haltcase |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
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
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 bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
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
18970
247
246