markdown-table
Advanced tools
Comparing version 1.1.1 to 1.1.2
219
index.js
@@ -1,84 +0,84 @@ | ||
'use strict'; | ||
'use strict' | ||
/* Expose. */ | ||
module.exports = markdownTable; | ||
module.exports = markdownTable | ||
/* Expressions. */ | ||
var EXPRESSION_DOT = /\./; | ||
var EXPRESSION_LAST_DOT = /\.[^.]*$/; | ||
var EXPRESSION_DOT = /\./ | ||
var EXPRESSION_LAST_DOT = /\.[^.]*$/ | ||
/* Allowed alignment values. */ | ||
var LEFT = 'l'; | ||
var RIGHT = 'r'; | ||
var CENTER = 'c'; | ||
var DOT = '.'; | ||
var NULL = ''; | ||
var LEFT = 'l' | ||
var RIGHT = 'r' | ||
var CENTER = 'c' | ||
var DOT = '.' | ||
var NULL = '' | ||
var ALLIGNMENT = [LEFT, RIGHT, CENTER, DOT, NULL]; | ||
var MIN_CELL_SIZE = 3; | ||
var ALLIGNMENT = [LEFT, RIGHT, CENTER, DOT, NULL] | ||
var MIN_CELL_SIZE = 3 | ||
/* Characters. */ | ||
var COLON = ':'; | ||
var DASH = '-'; | ||
var PIPE = '|'; | ||
var SPACE = ' '; | ||
var NEW_LINE = '\n'; | ||
var COLON = ':' | ||
var DASH = '-' | ||
var PIPE = '|' | ||
var SPACE = ' ' | ||
var NEW_LINE = '\n' | ||
/* Create a table from a matrix of strings. */ | ||
function markdownTable(table, options) { | ||
var settings = options || {}; | ||
var delimiter = settings.delimiter; | ||
var start = settings.start; | ||
var end = settings.end; | ||
var alignment = settings.align; | ||
var calculateStringLength = settings.stringLength || lengthNoop; | ||
var cellCount = 0; | ||
var rowIndex = -1; | ||
var rowLength = table.length; | ||
var sizes = []; | ||
var align; | ||
var rule; | ||
var rows; | ||
var row; | ||
var cells; | ||
var index; | ||
var position; | ||
var size; | ||
var value; | ||
var spacing; | ||
var before; | ||
var after; | ||
var settings = options || {} | ||
var delimiter = settings.delimiter | ||
var start = settings.start | ||
var end = settings.end | ||
var alignment = settings.align | ||
var calculateStringLength = settings.stringLength || lengthNoop | ||
var cellCount = 0 | ||
var rowIndex = -1 | ||
var rowLength = table.length | ||
var sizes = [] | ||
var align | ||
var rule | ||
var rows | ||
var row | ||
var cells | ||
var index | ||
var position | ||
var size | ||
var value | ||
var spacing | ||
var before | ||
var after | ||
alignment = alignment ? alignment.concat() : []; | ||
alignment = alignment ? alignment.concat() : [] | ||
if (delimiter === null || delimiter === undefined) { | ||
delimiter = SPACE + PIPE + SPACE; | ||
delimiter = SPACE + PIPE + SPACE | ||
} | ||
if (start === null || start === undefined) { | ||
start = PIPE + SPACE; | ||
start = PIPE + SPACE | ||
} | ||
if (end === null || end === undefined) { | ||
end = SPACE + PIPE; | ||
end = SPACE + PIPE | ||
} | ||
while (++rowIndex < rowLength) { | ||
row = table[rowIndex]; | ||
row = table[rowIndex] | ||
index = -1; | ||
index = -1 | ||
if (row.length > cellCount) { | ||
cellCount = row.length; | ||
cellCount = row.length | ||
} | ||
while (++index < cellCount) { | ||
position = row[index] ? dotindex(row[index]) : null; | ||
position = row[index] ? dotindex(row[index]) : null | ||
if (!sizes[index]) { | ||
sizes[index] = MIN_CELL_SIZE; | ||
sizes[index] = MIN_CELL_SIZE | ||
} | ||
if (position > sizes[index]) { | ||
sizes[index] = position; | ||
sizes[index] = position | ||
} | ||
@@ -89,71 +89,72 @@ } | ||
if (typeof alignment === 'string') { | ||
alignment = pad(cellCount, alignment).split(''); | ||
alignment = pad(cellCount, alignment).split('') | ||
} | ||
/* Make sure only valid alignments are used. */ | ||
index = -1; | ||
index = -1 | ||
while (++index < cellCount) { | ||
align = alignment[index]; | ||
align = alignment[index] | ||
if (typeof align === 'string') { | ||
align = align.charAt(0).toLowerCase(); | ||
align = align.charAt(0).toLowerCase() | ||
} | ||
if (ALLIGNMENT.indexOf(align) === -1) { | ||
align = NULL; | ||
align = NULL | ||
} | ||
alignment[index] = align; | ||
alignment[index] = align | ||
} | ||
rowIndex = -1; | ||
rows = []; | ||
rowIndex = -1 | ||
rows = [] | ||
while (++rowIndex < rowLength) { | ||
row = table[rowIndex]; | ||
row = table[rowIndex] | ||
index = -1; | ||
cells = []; | ||
index = -1 | ||
cells = [] | ||
while (++index < cellCount) { | ||
value = row[index]; | ||
value = row[index] | ||
value = stringify(value); | ||
value = stringify(value) | ||
if (alignment[index] === DOT) { | ||
position = dotindex(value); | ||
position = dotindex(value) | ||
size = sizes[index] + | ||
size = | ||
sizes[index] + | ||
(EXPRESSION_DOT.test(value) ? 0 : 1) - | ||
(calculateStringLength(value) - position); | ||
(calculateStringLength(value) - position) | ||
cells[index] = value + pad(size - 1); | ||
cells[index] = value + pad(size - 1) | ||
} else { | ||
cells[index] = value; | ||
cells[index] = value | ||
} | ||
} | ||
rows[rowIndex] = cells; | ||
rows[rowIndex] = cells | ||
} | ||
sizes = []; | ||
rowIndex = -1; | ||
sizes = [] | ||
rowIndex = -1 | ||
while (++rowIndex < rowLength) { | ||
cells = rows[rowIndex]; | ||
cells = rows[rowIndex] | ||
index = -1; | ||
index = -1 | ||
while (++index < cellCount) { | ||
value = cells[index]; | ||
value = cells[index] | ||
if (!sizes[index]) { | ||
sizes[index] = MIN_CELL_SIZE; | ||
sizes[index] = MIN_CELL_SIZE | ||
} | ||
size = calculateStringLength(value); | ||
size = calculateStringLength(value) | ||
if (size > sizes[index]) { | ||
sizes[index] = size; | ||
sizes[index] = size | ||
} | ||
@@ -163,44 +164,44 @@ } | ||
rowIndex = -1; | ||
rowIndex = -1 | ||
while (++rowIndex < rowLength) { | ||
cells = rows[rowIndex]; | ||
cells = rows[rowIndex] | ||
index = -1; | ||
index = -1 | ||
if (settings.pad !== false) { | ||
while (++index < cellCount) { | ||
value = cells[index]; | ||
value = cells[index] | ||
position = sizes[index] - (calculateStringLength(value) || 0); | ||
spacing = pad(position); | ||
position = sizes[index] - (calculateStringLength(value) || 0) | ||
spacing = pad(position) | ||
if (alignment[index] === RIGHT || alignment[index] === DOT) { | ||
value = spacing + value; | ||
value = spacing + value | ||
} else if (alignment[index] === CENTER) { | ||
position /= 2; | ||
position /= 2 | ||
if (position % 1 === 0) { | ||
before = position; | ||
after = position; | ||
before = position | ||
after = position | ||
} else { | ||
before = position + 0.5; | ||
after = position - 0.5; | ||
before = position + 0.5 | ||
after = position - 0.5 | ||
} | ||
value = pad(before) + value + pad(after); | ||
value = pad(before) + value + pad(after) | ||
} else { | ||
value += spacing; | ||
value += spacing | ||
} | ||
cells[index] = value; | ||
cells[index] = value | ||
} | ||
} | ||
rows[rowIndex] = cells.join(delimiter); | ||
rows[rowIndex] = cells.join(delimiter) | ||
} | ||
if (settings.rule !== false) { | ||
index = -1; | ||
rule = []; | ||
index = -1 | ||
rule = [] | ||
@@ -210,27 +211,27 @@ while (++index < cellCount) { | ||
if (settings.pad === false) { | ||
value = table[0][index]; | ||
spacing = calculateStringLength(stringify(value)); | ||
spacing = spacing > MIN_CELL_SIZE ? spacing : MIN_CELL_SIZE; | ||
value = table[0][index] | ||
spacing = calculateStringLength(stringify(value)) | ||
spacing = spacing > MIN_CELL_SIZE ? spacing : MIN_CELL_SIZE | ||
} else { | ||
spacing = sizes[index]; | ||
spacing = sizes[index] | ||
} | ||
align = alignment[index]; | ||
align = alignment[index] | ||
/* When `align` is left, don't add colons. */ | ||
value = align === RIGHT || align === NULL ? DASH : COLON; | ||
value += pad(spacing - 2, DASH); | ||
value += align !== LEFT && align !== NULL ? COLON : DASH; | ||
value = align === RIGHT || align === NULL ? DASH : COLON | ||
value += pad(spacing - 2, DASH) | ||
value += align !== LEFT && align !== NULL ? COLON : DASH | ||
rule[index] = value; | ||
rule[index] = value | ||
} | ||
rows.splice(1, 0, rule.join(delimiter)); | ||
rows.splice(1, 0, rule.join(delimiter)) | ||
} | ||
return start + rows.join(end + NEW_LINE + start) + end; | ||
return start + rows.join(end + NEW_LINE + start) + end | ||
} | ||
function stringify(value) { | ||
return (value === null || value === undefined) ? '' : String(value); | ||
return value === null || value === undefined ? '' : String(value) | ||
} | ||
@@ -240,3 +241,3 @@ | ||
function lengthNoop(value) { | ||
return String(value).length; | ||
return String(value).length | ||
} | ||
@@ -246,3 +247,3 @@ | ||
function pad(length, character) { | ||
return Array(length + 1).join(character || SPACE); | ||
return new Array(length + 1).join(character || SPACE) | ||
} | ||
@@ -252,5 +253,5 @@ | ||
function dotindex(value) { | ||
var match = EXPRESSION_LAST_DOT.exec(value); | ||
var match = EXPRESSION_LAST_DOT.exec(value) | ||
return match ? match.index + 1 : value.length; | ||
return match ? match.index + 1 : value.length | ||
} |
{ | ||
"name": "markdown-table", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Markdown/ASCII tables", | ||
@@ -26,34 +26,44 @@ "license": "MIT", | ||
"devDependencies": { | ||
"browserify": "^14.0.0", | ||
"browserify": "^16.0.0", | ||
"chalk": "^2.0.0", | ||
"esmangle": "^1.0.0", | ||
"nyc": "^11.0.0", | ||
"remark-cli": "^3.0.0", | ||
"remark-preset-wooorm": "^3.0.0", | ||
"prettier": "^1.12.1", | ||
"remark-cli": "^5.0.0", | ||
"remark-preset-wooorm": "^4.0.0", | ||
"strip-ansi": "^4.0.0", | ||
"tape": "^4.4.0", | ||
"xo": "^0.18.0" | ||
"xo": "^0.20.0" | ||
}, | ||
"scripts": { | ||
"build-md": "remark . -qfo", | ||
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix", | ||
"build-bundle": "browserify index.js -s markdownTable > markdown-table.js", | ||
"build-mangle": "esmangle markdown-table.js > markdown-table.min.js", | ||
"build": "npm run build-md && npm run build-bundle && npm run build-mangle", | ||
"lint": "xo", | ||
"build": "npm run build-bundle && npm run build-mangle", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
"test": "npm run format && npm run build && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
"remarkConfig": { | ||
"plugins": [ | ||
"preset-wooorm" | ||
] | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
}, | ||
"xo": { | ||
"space": true, | ||
"prettier": true, | ||
"esnext": false, | ||
"rules": { | ||
"complexity": "off", | ||
"max-depth": "off" | ||
"max-depth": "off", | ||
"object-shorthand": "off", | ||
"prefer-arrow-callback": "off", | ||
"no-var": "off" | ||
}, | ||
@@ -64,7 +74,8 @@ "ignores": [ | ||
}, | ||
"remarkConfig": { | ||
"plugins": [ | ||
"preset-wooorm" | ||
] | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
} | ||
} |
@@ -18,3 +18,3 @@ # markdown-table [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
```javascript | ||
var table = require('markdown-table'); | ||
var table = require('markdown-table') | ||
@@ -25,3 +25,3 @@ table([ | ||
['staging', 'fedcba9876543210'] | ||
]); | ||
]) | ||
``` | ||
@@ -41,11 +41,14 @@ | ||
```javascript | ||
table([ | ||
['Beep', 'No.', 'Boop'], | ||
['beep', '1024', 'xyz'], | ||
['boop', '3388450', 'tuv'], | ||
['foo', '10106', 'qrstuv'], | ||
['bar', '45', 'lmno'] | ||
], { | ||
align: ['l', 'c', 'r'] | ||
}); | ||
table( | ||
[ | ||
['Beep', 'No.', 'Boop'], | ||
['beep', '1024', 'xyz'], | ||
['boop', '3388450', 'tuv'], | ||
['foo', '10106', 'qrstuv'], | ||
['bar', '45', 'lmno'] | ||
], | ||
{ | ||
align: ['l', 'c', 'r'] | ||
} | ||
) | ||
``` | ||
@@ -67,11 +70,5 @@ | ||
```javascript | ||
table([ | ||
['No.'], | ||
['0.1.2'], | ||
['11.22.33'], | ||
['5.6.'], | ||
['1.22222'], | ||
], { | ||
table([['No.'], ['0.1.2'], ['11.22.33'], ['5.6.'], ['1.22222']], { | ||
align: '.' | ||
}); | ||
}) | ||
``` | ||
@@ -134,6 +131,6 @@ | ||
```javascript | ||
var strip = require('strip-ansi'); | ||
var strip = require('strip-ansi') | ||
function stringLength(cell) { | ||
return strip(cell).length; | ||
return strip(cell).length | ||
} | ||
@@ -140,0 +137,0 @@ ``` |
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
11792
191
10
168
1