markdown-table
Advanced tools
Comparing version 0.4.0 to 1.0.0
378
index.js
@@ -0,14 +1,19 @@ | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2014 Titus Wormer | ||
* @license MIT | ||
* @module markdown-table | ||
* @fileoverview Count syllables in English words. | ||
*/ | ||
'use strict'; | ||
/* | ||
* Useful expressions. | ||
*/ | ||
/* Expose `markdownTable`. */ | ||
module.exports = markdownTable; | ||
/* Expressions. */ | ||
var EXPRESSION_DOT = /\./; | ||
var EXPRESSION_LAST_DOT = /\.[^.]*$/; | ||
/* | ||
* Allowed alignment values. | ||
*/ | ||
/* Allowed alignment values. */ | ||
var LEFT = 'l'; | ||
@@ -22,6 +27,3 @@ var RIGHT = 'r'; | ||
/* | ||
* Characters. | ||
*/ | ||
/* Characters. */ | ||
var COLON = ':'; | ||
@@ -34,35 +36,2 @@ var DASH = '-'; | ||
/** | ||
* Get the length of `value`. | ||
* | ||
* @param {string} value | ||
* @return {number} | ||
*/ | ||
function lengthNoop(value) { | ||
return String(value).length; | ||
} | ||
/** | ||
* Get a string consisting of `length` `character`s. | ||
* | ||
* @param {number} length | ||
* @param {string} [character=' '] | ||
* @return {string} | ||
*/ | ||
function pad(length, character) { | ||
return Array(length + 1).join(character || SPACE); | ||
} | ||
/** | ||
* Get the position of the last dot in `value`. | ||
* | ||
* @param {string} value | ||
* @return {number} | ||
*/ | ||
function dotindex(value) { | ||
var match = EXPRESSION_LAST_DOT.exec(value); | ||
return match ? match.index + 1 : value.length; | ||
} | ||
/** | ||
* Create a table from a matrix of strings. | ||
@@ -81,207 +50,228 @@ * | ||
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; | ||
} | ||
if (delimiter === null || delimiter === undefined) { | ||
delimiter = SPACE + PIPE + SPACE; | ||
} | ||
if (start === null || start === undefined) { | ||
start = PIPE + SPACE; | ||
} | ||
if (start === null || start === undefined) { | ||
start = PIPE + SPACE; | ||
} | ||
if (end === null || end === undefined) { | ||
end = SPACE + PIPE; | ||
} | ||
if (end === null || end === undefined) { | ||
end = SPACE + PIPE; | ||
} | ||
while (++rowIndex < rowLength) { | ||
row = table[rowIndex]; | ||
while (++rowIndex < rowLength) { | ||
row = table[rowIndex]; | ||
index = -1; | ||
index = -1; | ||
if (row.length > cellCount) { | ||
cellCount = row.length; | ||
} | ||
if (row.length > cellCount) { | ||
cellCount = row.length; | ||
} | ||
while (++index < cellCount) { | ||
position = row[index] ? dotindex(row[index]) : null; | ||
while (++index < cellCount) { | ||
position = row[index] ? dotindex(row[index]) : null; | ||
if (!sizes[index]) { | ||
sizes[index] = 3; | ||
} | ||
if (!sizes[index]) { | ||
sizes[index] = 3; | ||
} | ||
if (position > sizes[index]) { | ||
sizes[index] = position; | ||
} | ||
} | ||
if (position > sizes[index]) { | ||
sizes[index] = position; | ||
} | ||
} | ||
} | ||
if (typeof alignment === 'string') { | ||
alignment = pad(cellCount, alignment).split(''); | ||
} | ||
if (typeof alignment === 'string') { | ||
alignment = pad(cellCount, alignment).split(''); | ||
} | ||
/* | ||
* Make sure only valid alignments are used. | ||
*/ | ||
/* Make sure only valid alignments are used. */ | ||
index = -1; | ||
index = -1; | ||
while (++index < cellCount) { | ||
align = alignment[index]; | ||
while (++index < cellCount) { | ||
align = alignment[index]; | ||
if (typeof align === 'string') { | ||
align = align.charAt(0).toLowerCase(); | ||
} | ||
if (typeof align === 'string') { | ||
align = align.charAt(0).toLowerCase(); | ||
} | ||
if (ALLIGNMENT.indexOf(align) === -1) { | ||
align = NULL; | ||
} | ||
alignment[index] = align; | ||
if (ALLIGNMENT.indexOf(align) === -1) { | ||
align = NULL; | ||
} | ||
rowIndex = -1; | ||
rows = []; | ||
alignment[index] = align; | ||
} | ||
while (++rowIndex < rowLength) { | ||
row = table[rowIndex]; | ||
rowIndex = -1; | ||
rows = []; | ||
index = -1; | ||
cells = []; | ||
while (++rowIndex < rowLength) { | ||
row = table[rowIndex]; | ||
while (++index < cellCount) { | ||
value = row[index]; | ||
index = -1; | ||
cells = []; | ||
if (value === null || value === undefined) { | ||
value = ''; | ||
} else { | ||
value = String(value); | ||
} | ||
while (++index < cellCount) { | ||
value = row[index]; | ||
if (alignment[index] !== DOT) { | ||
cells[index] = value; | ||
} else { | ||
position = dotindex(value); | ||
if (value === null || value === undefined) { | ||
value = ''; | ||
} else { | ||
value = String(value); | ||
} | ||
size = sizes[index] + | ||
(EXPRESSION_DOT.test(value) ? 0 : 1) - | ||
(calculateStringLength(value) - position); | ||
if (alignment[index] === DOT) { | ||
position = dotindex(value); | ||
cells[index] = value + pad(size - 1); | ||
} | ||
} | ||
size = sizes[index] + | ||
(EXPRESSION_DOT.test(value) ? 0 : 1) - | ||
(calculateStringLength(value) - position); | ||
rows[rowIndex] = cells; | ||
cells[index] = value + pad(size - 1); | ||
} else { | ||
cells[index] = value; | ||
} | ||
} | ||
sizes = []; | ||
rowIndex = -1; | ||
rows[rowIndex] = cells; | ||
} | ||
while (++rowIndex < rowLength) { | ||
cells = rows[rowIndex]; | ||
sizes = []; | ||
rowIndex = -1; | ||
index = -1; | ||
while (++rowIndex < rowLength) { | ||
cells = rows[rowIndex]; | ||
while (++index < cellCount) { | ||
value = cells[index]; | ||
index = -1; | ||
if (!sizes[index]) { | ||
sizes[index] = 3; | ||
} | ||
while (++index < cellCount) { | ||
value = cells[index]; | ||
size = calculateStringLength(value); | ||
if (!sizes[index]) { | ||
sizes[index] = 3; | ||
} | ||
if (size > sizes[index]) { | ||
sizes[index] = size; | ||
} | ||
} | ||
size = calculateStringLength(value); | ||
if (size > sizes[index]) { | ||
sizes[index] = size; | ||
} | ||
} | ||
} | ||
rowIndex = -1; | ||
rowIndex = -1; | ||
while (++rowIndex < rowLength) { | ||
cells = rows[rowIndex]; | ||
while (++rowIndex < rowLength) { | ||
cells = rows[rowIndex]; | ||
index = -1; | ||
index = -1; | ||
while (++index < cellCount) { | ||
value = cells[index]; | ||
while (++index < cellCount) { | ||
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; | ||
} else if (alignment[index] !== CENTER) { | ||
value = value + spacing; | ||
} else { | ||
position = position / 2; | ||
if (alignment[index] === RIGHT || alignment[index] === DOT) { | ||
value = spacing + value; | ||
} else if (alignment[index] === CENTER) { | ||
position /= 2; | ||
if (position % 1 === 0) { | ||
before = position; | ||
after = position; | ||
} else { | ||
before = position + 0.5; | ||
after = position - 0.5; | ||
} | ||
if (position % 1 === 0) { | ||
before = position; | ||
after = position; | ||
} else { | ||
before = position + 0.5; | ||
after = position - 0.5; | ||
} | ||
value = pad(before) + value + pad(after); | ||
} | ||
value = pad(before) + value + pad(after); | ||
} else { | ||
value += spacing; | ||
} | ||
cells[index] = value; | ||
} | ||
rows[rowIndex] = cells.join(delimiter); | ||
cells[index] = value; | ||
} | ||
if (settings.rule !== false) { | ||
index = -1; | ||
rule = []; | ||
rows[rowIndex] = cells.join(delimiter); | ||
} | ||
while (++index < cellCount) { | ||
align = alignment[index]; | ||
if (settings.rule !== false) { | ||
index = -1; | ||
rule = []; | ||
/* | ||
* When `align` is left, don't add colons. | ||
*/ | ||
while (++index < cellCount) { | ||
align = alignment[index]; | ||
value = align === RIGHT || align === NULL ? DASH : COLON; | ||
value += pad(sizes[index] - 2, DASH); | ||
value += align !== LEFT && align !== NULL ? COLON : DASH; | ||
/* When `align` is left, don't add colons. */ | ||
value = align === RIGHT || align === NULL ? DASH : COLON; | ||
value += pad(sizes[index] - 2, DASH); | ||
value += align !== LEFT && align !== NULL ? COLON : DASH; | ||
rule[index] = value; | ||
} | ||
rows.splice(1, 0, rule.join(delimiter)); | ||
rule[index] = value; | ||
} | ||
return start + rows.join(end + NEW_LINE + start) + end; | ||
rows.splice(1, 0, rule.join(delimiter)); | ||
} | ||
return start + rows.join(end + NEW_LINE + start) + end; | ||
} | ||
/* | ||
* Expose `markdownTable`. | ||
/** | ||
* Get the length of `value`. | ||
* | ||
* @param {string} value | ||
* @return {number} | ||
*/ | ||
function lengthNoop(value) { | ||
return String(value).length; | ||
} | ||
module.exports = markdownTable; | ||
/** | ||
* Get a string consisting of `length` `character`s. | ||
* | ||
* @param {number} length | ||
* @param {string} [character=' '] | ||
* @return {string} | ||
*/ | ||
function pad(length, character) { | ||
return Array(length + 1).join(character || SPACE); | ||
} | ||
/** | ||
* Get the position of the last dot in `value`. | ||
* | ||
* @param {string} value | ||
* @return {number} | ||
*/ | ||
function dotindex(value) { | ||
var match = EXPRESSION_LAST_DOT.exec(value); | ||
return match ? match.index + 1 : value.length; | ||
} |
{ | ||
"name": "markdown-table", | ||
"version": "0.4.0", | ||
"version": "1.0.0", | ||
"description": "Markdown/ASCII tables", | ||
@@ -15,27 +15,69 @@ "license": "MIT", | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wooorm/markdown-table.git" | ||
"dependencies": {}, | ||
"repository": "https://github.com/wooorm/markdown-table", | ||
"bugs": "https://github.com/wooorm/markdown-table/issues", | ||
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
"contributors": [ | ||
"Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)" | ||
], | ||
"engines": { | ||
"node": ">=0.11.0" | ||
}, | ||
"author": "Titus Wormer <tituswormer@gmail.com>", | ||
"files": [ | ||
"index.js" | ||
], | ||
"devDependencies": { | ||
"chalk": "^1.0.0", | ||
"eslint": "^0.18.0", | ||
"istanbul": "^0.3.0", | ||
"jscs": "^1.0.0", | ||
"jscs-jsdoc": "^0.4.0", | ||
"mocha": "^2.0.0" | ||
"browserify": "^13.0.0", | ||
"chalk": "^1.1.3", | ||
"esmangle": "^1.0.0", | ||
"nyc": "^7.1.0", | ||
"remark-cli": "^1.0.0", | ||
"remark-comment-config": "^4.0.0", | ||
"remark-github": "^5.0.0", | ||
"remark-lint": "^4.0.0", | ||
"remark-validate-links": "^4.0.0", | ||
"tape": "^4.4.0", | ||
"xo": "^0.16.0" | ||
}, | ||
"scripts": { | ||
"test-api": "_mocha --check-leaks test.js", | ||
"test-coveralls": "istanbul cover _mocha --report lcovonly -- --check-leaks test.js", | ||
"test-coverage": "istanbul cover _mocha -- -- test.js", | ||
"test-travis": "npm run test-coveralls", | ||
"test": "npm run test-api", | ||
"lint-api": "eslint index.js", | ||
"lint-test": "eslint --env mocha test.js", | ||
"lint-style": "jscs --reporter inline index.js test.js", | ||
"lint": "npm run lint-api && npm run lint-test && npm run lint-style", | ||
"make": "npm run lint && npm run test-coverage" | ||
"build-md": "remark . --quiet --frail", | ||
"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", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"xo": { | ||
"space": true, | ||
"rules": { | ||
"complexity": "off" | ||
}, | ||
"ignores": [ | ||
"markdown-table.js", | ||
"markdown-table.min.js" | ||
] | ||
}, | ||
"remarkConfig": { | ||
"output": true, | ||
"plugins": { | ||
"comment-config": null, | ||
"lint": { | ||
"heading-increment": false, | ||
"list-item-spacing": false | ||
}, | ||
"github": null, | ||
"validate-links": null | ||
}, | ||
"settings": { | ||
"bullet": "*" | ||
} | ||
} | ||
} |
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
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
164
12446
11
215
1