Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

markdown-table

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

markdown-table - npm Package Compare versions

Comparing version 2.0.0 to 3.0.0

index.d.ts

225

index.js

@@ -1,52 +0,54 @@

'use strict'
import repeat from 'repeat-string'
var repeat = require('repeat-string')
/**
* @typedef MarkdownTableOptions
* @property {string|string[]} [align]
* @property {boolean} [padding=true]
* @property {boolean} [delimiterStart=true]
* @property {boolean} [delimiterStart=true]
* @property {boolean} [delimiterEnd=true]
* @property {boolean} [alignDelimiters=true]
* @property {(value: string) => number} [stringLength]
*/
module.exports = markdownTable
var trailingWhitespace = / +$/
// Characters.
var space = ' '
var lineFeed = '\n'
var dash = '-'
var colon = ':'
var verticalBar = '|'
var x = 0
var C = 67
var L = 76
var R = 82
var c = 99
var l = 108
var r = 114
// Create a table from a matrix of strings.
function markdownTable(table, options) {
/**
* Create a table from a matrix of strings.
*
* @param {string[][]} table
* @param {MarkdownTableOptions} [options]
* @returns {string}
*/
export function markdownTable(table, options) {
var settings = options || {}
var padding = settings.padding !== false
var start = settings.delimiterStart !== false
var end = settings.delimiterEnd !== false
var align = (settings.align || []).concat()
var alignDelimiters = settings.alignDelimiters !== false
var stringLength = settings.stringLength || defaultStringLength
/** @type {number[]} Character codes as symbols for alignment per column. */
var alignments = []
var stringLength = settings.stringLength || defaultStringLength
var rowIndex = -1
var rowLength = table.length
/** @type {string[][]} Cells per row. */
var cellMatrix = []
/** @type {number[][]} Sizes of each cell per row. */
var sizeMatrix = []
var row = []
var sizes = []
/** @type {number[]} */
var longestCellByColumn = []
var mostCellsPerRow = 0
var cells
/** @type {number} */
var columnIndex
var columnLength
var largest
/** @type {string[]} Cells of current row */
var row
/** @type {number[]} Sizes of current row */
var sizes
/** @type {number} Sizes of current cell */
var size
/** @type {string} Current cell */
var cell
/** @type {string[]} */
var lines
/** @type {string[]} Chunks of current line. */
var line
/** @type {string} */
var before
/** @type {string} */
var after
/** @type {number} */
var code

@@ -56,23 +58,22 @@

// do superfluous work when aligning, so optimize for aligning.
while (++rowIndex < rowLength) {
cells = table[rowIndex]
while (++rowIndex < table.length) {
columnIndex = -1
columnLength = cells.length
row = []
sizes = []
if (columnLength > mostCellsPerRow) {
mostCellsPerRow = columnLength
if (table[rowIndex].length > mostCellsPerRow) {
mostCellsPerRow = table[rowIndex].length
}
while (++columnIndex < columnLength) {
cell = serialize(cells[columnIndex])
while (++columnIndex < table[rowIndex].length) {
cell = serialize(table[rowIndex][columnIndex])
if (alignDelimiters === true) {
if (settings.alignDelimiters !== false) {
size = stringLength(cell)
sizes[columnIndex] = size
largest = longestCellByColumn[columnIndex]
if (largest === undefined || size > largest) {
if (
longestCellByColumn[columnIndex] === undefined ||
size > longestCellByColumn[columnIndex]
) {
longestCellByColumn[columnIndex] = size

@@ -91,6 +92,5 @@ }

columnIndex = -1
columnLength = mostCellsPerRow
if (typeof align === 'object' && 'length' in align) {
while (++columnIndex < columnLength) {
while (++columnIndex < mostCellsPerRow) {
alignments[columnIndex] = toAlignment(align[columnIndex])

@@ -101,3 +101,3 @@ }

while (++columnIndex < columnLength) {
while (++columnIndex < mostCellsPerRow) {
alignments[columnIndex] = code

@@ -109,7 +109,6 @@ }

columnIndex = -1
columnLength = mostCellsPerRow
row = []
sizes = []
while (++columnIndex < columnLength) {
while (++columnIndex < mostCellsPerRow) {
code = alignments[columnIndex]

@@ -119,22 +118,23 @@ before = ''

if (code === l) {
before = colon
} else if (code === r) {
after = colon
} else if (code === c) {
before = colon
after = colon
if (code === 99 /* `c` */) {
before = ':'
after = ':'
} else if (code === 108 /* `l` */) {
before = ':'
} else if (code === 114 /* `r` */) {
after = ':'
}
// There *must* be at least one hyphen-minus in each alignment cell.
size = alignDelimiters
? Math.max(
1,
longestCellByColumn[columnIndex] - before.length - after.length
)
: 1
size =
settings.alignDelimiters === false
? 1
: Math.max(
1,
longestCellByColumn[columnIndex] - before.length - after.length
)
cell = before + repeat(dash, size) + after
cell = before + repeat('-', size) + after
if (alignDelimiters === true) {
if (settings.alignDelimiters !== false) {
size = before.length + size + after.length

@@ -157,13 +157,11 @@

rowIndex = -1
rowLength = cellMatrix.length
lines = []
while (++rowIndex < rowLength) {
while (++rowIndex < cellMatrix.length) {
row = cellMatrix[rowIndex]
sizes = sizeMatrix[rowIndex]
columnIndex = -1
columnLength = mostCellsPerRow
line = []
while (++columnIndex < columnLength) {
while (++columnIndex < mostCellsPerRow) {
cell = row[columnIndex] || ''

@@ -173,36 +171,36 @@ before = ''

if (alignDelimiters === true) {
if (settings.alignDelimiters !== false) {
size = longestCellByColumn[columnIndex] - (sizes[columnIndex] || 0)
code = alignments[columnIndex]
if (code === r) {
before = repeat(space, size)
} else if (code === c) {
if (size % 2 === 0) {
before = repeat(space, size / 2)
if (code === 114 /* `r` */) {
before = repeat(' ', size)
} else if (code === 99 /* `c` */) {
if (size % 2) {
before = repeat(' ', size / 2 + 0.5)
after = repeat(' ', size / 2 - 0.5)
} else {
before = repeat(' ', size / 2)
after = before
} else {
before = repeat(space, size / 2 + 0.5)
after = repeat(space, size / 2 - 0.5)
}
} else {
after = repeat(space, size)
after = repeat(' ', size)
}
}
if (start === true && columnIndex === 0) {
line.push(verticalBar)
if (settings.delimiterStart !== false && !columnIndex) {
line.push('|')
}
if (
padding === true &&
settings.padding !== false &&
// Don’t add the opening space if we’re not aligning and the cell is
// empty: there will be a closing space.
!(alignDelimiters === false && cell === '') &&
(start === true || columnIndex !== 0)
!(settings.alignDelimiters === false && cell === '') &&
(settings.delimiterStart !== false || columnIndex)
) {
line.push(space)
line.push(' ')
}
if (alignDelimiters === true) {
if (settings.alignDelimiters !== false) {
line.push(before)

@@ -213,27 +211,32 @@ }

if (alignDelimiters === true) {
if (settings.alignDelimiters !== false) {
line.push(after)
}
if (padding === true) {
line.push(space)
if (settings.padding !== false) {
line.push(' ')
}
if (end === true || columnIndex !== columnLength - 1) {
line.push(verticalBar)
if (
settings.delimiterEnd !== false ||
columnIndex !== mostCellsPerRow - 1
) {
line.push('|')
}
}
line = line.join('')
if (end === false) {
line = line.replace(trailingWhitespace, '')
}
lines.push(line)
lines.push(
settings.delimiterEnd === false
? line.join('').replace(/ +$/, '')
: line.join('')
)
}
return lines.join(lineFeed)
return lines.join('\n')
}
/**
* @param {string|null|undefined} [value]
* @returns {string}
*/
function serialize(value) {

@@ -243,2 +246,6 @@ return value === null || value === undefined ? '' : String(value)

/**
* @param {string} value
* @returns {number}
*/
function defaultStringLength(value) {

@@ -248,12 +255,16 @@ return value.length

/**
* @param {string} value
* @returns {number}
*/
function toAlignment(value) {
var code = typeof value === 'string' ? value.charCodeAt(0) : x
var code = typeof value === 'string' ? value.charCodeAt(0) : 0
return code === L || code === l
? l
: code === R || code === r
? r
: code === C || code === c
? c
: x
return code === 67 /* `C` */ || code === 99 /* `c` */
? 99 /* `c` */
: code === 76 /* `L` */ || code === 108 /* `l` */
? 108 /* `l` */
: code === 82 /* `R` */ || code === 114 /* `r` */
? 114 /* `r` */
: 0
}
{
"name": "markdown-table",
"version": "2.0.0",
"version": "3.0.0",
"description": "Markdown tables",

@@ -17,5 +17,5 @@ "license": "MIT",

"funding": {
"type": "github",
"url": "https://github.com/sponsors/wooorm"
},
"type": "github",
"url": "https://github.com/sponsors/wooorm"
},
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",

@@ -25,28 +25,36 @@ "contributors": [

],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/repeat-string": "^1.0.0",
"repeat-string": "^1.0.0"
},
"devDependencies": {
"browserify": "^16.0.0",
"chalk": "^3.0.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"chalk": "^4.0.0",
"nyc": "^15.0.0",
"prettier": "^1.0.0",
"remark-cli": "^7.0.0",
"remark-preset-wooorm": "^6.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"strip-ansi": "^6.0.0",
"tape": "^4.0.0",
"tinyify": "^2.0.0",
"xo": "^0.25.0"
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.38.0"
},
"scripts": {
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"build-bundle": "browserify . -s markdownTable -o markdown-table.js",
"build-mangle": "browserify . -s markdownTable -p tinyify -o markdown-table.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run build && npm run test-coverage"
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run build && npm run format && npm run test-coverage"
},

@@ -68,16 +76,13 @@ "remarkConfig": {

"prettier": true,
"esnext": false,
"rules": {
"complexity": "off"
},
"ignores": [
"markdown-table.js"
]
"complexity": "off",
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}

@@ -12,2 +12,5 @@ # markdown-table

This package is ESM only: Node 12+ is needed to use it and it must be `import`ed
instead of `require`d.
[npm][]:

@@ -24,7 +27,7 @@

```js
var table = require('markdown-table')
import {markdownTable} from 'markdown-table'
table([
markdownTable([
['Branch', 'Commit'],
['master', '0123456789abcdef'],
['main', '0123456789abcdef'],
['staging', 'fedcba9876543210']

@@ -39,3 +42,3 @@ ])

| ------- | ---------------- |
| master | 0123456789abcdef |
| main | 0123456789abcdef |
| staging | fedcba9876543210 |

@@ -47,3 +50,3 @@ ```

```js
table(
markdownTable(
[

@@ -73,2 +76,5 @@ ['Beep', 'No.', 'Boop'],

This package exports the following identifiers: `markdownTable`.
There is no default export.
### `markdownTable(table[, options])`

@@ -83,3 +89,3 @@

One style for all columns, or styles for their respective columns (`string` or
`Array.<string>`).
`string[]`).
Each style is either `'l'` (left), `'r'` (right), or `'c'` (center).

@@ -188,3 +194,3 @@ Other values are treated as `''`, which doesn’t place the colon in the alignment

```js
table([
markdownTable([
['Alpha', 'Bravo'],

@@ -208,5 +214,5 @@ ['中文', 'Charlie'],

```js
var width = require('string-width')
import stringWidth from 'string-width'
table(
markdownTable(
[

@@ -241,5 +247,5 @@ ['Alpha', 'Bravo'],

[build-badge]: https://img.shields.io/travis/wooorm/markdown-table.svg
[build-badge]: https://github.com/wooorm/markdown-table/workflows/main/badge.svg
[build]: https://travis-ci.org/wooorm/markdown-table
[build]: https://github.com/wooorm/markdown-table/actions

@@ -246,0 +252,0 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/markdown-table.svg

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc