markdown-table
Advanced tools
Comparing version 1.0.0 to 1.1.0
110
index.js
@@ -1,12 +0,4 @@ | ||
/** | ||
* @author Titus Wormer | ||
* @copyright 2014 Titus Wormer | ||
* @license MIT | ||
* @module markdown-table | ||
* @fileoverview Count syllables in English words. | ||
*/ | ||
'use strict'; | ||
/* Expose `markdownTable`. */ | ||
/* Expose. */ | ||
module.exports = markdownTable; | ||
@@ -26,2 +18,3 @@ | ||
var ALLIGNMENT = [LEFT, RIGHT, CENTER, DOT, NULL]; | ||
var MIN_CELL_SIZE = 3; | ||
@@ -35,15 +28,3 @@ /* Characters. */ | ||
/** | ||
* Create a table from a matrix of strings. | ||
* | ||
* @param {Array.<Array.<string>>} table | ||
* @param {Object?} options | ||
* @param {boolean?} [options.rule=true] | ||
* @param {string?} [options.delimiter=" | "] | ||
* @param {string?} [options.start="| "] | ||
* @param {string?} [options.end=" |"] | ||
* @param {Array.<string>?} options.align | ||
* @param {function(string)?} options.stringLength | ||
* @return {string} Pretty table | ||
*/ | ||
/* Create a table from a matrix of strings. */ | ||
function markdownTable(table, options) { | ||
@@ -100,3 +81,3 @@ var settings = options || {}; | ||
if (!sizes[index]) { | ||
sizes[index] = 3; | ||
sizes[index] = MIN_CELL_SIZE; | ||
} | ||
@@ -143,7 +124,3 @@ | ||
if (value === null || value === undefined) { | ||
value = ''; | ||
} else { | ||
value = String(value); | ||
} | ||
value = stringify(value); | ||
@@ -178,3 +155,3 @@ if (alignment[index] === DOT) { | ||
if (!sizes[index]) { | ||
sizes[index] = 3; | ||
sizes[index] = MIN_CELL_SIZE; | ||
} | ||
@@ -197,27 +174,29 @@ | ||
while (++index < cellCount) { | ||
value = cells[index]; | ||
if (settings.pad !== false) { | ||
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) { | ||
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; | ||
if (position % 1 === 0) { | ||
before = position; | ||
after = position; | ||
} else { | ||
before = position + 0.5; | ||
after = position - 0.5; | ||
} | ||
value = pad(before) + value + pad(after); | ||
} else { | ||
before = position + 0.5; | ||
after = position - 0.5; | ||
value += spacing; | ||
} | ||
value = pad(before) + value + pad(after); | ||
} else { | ||
value += spacing; | ||
cells[index] = value; | ||
} | ||
cells[index] = value; | ||
} | ||
@@ -233,2 +212,11 @@ | ||
while (++index < cellCount) { | ||
/* When `pad` is false, make the rule the same size as the first row. */ | ||
if (settings.pad === false) { | ||
value = table[0][index]; | ||
spacing = calculateStringLength(stringify(value)); | ||
spacing = spacing > MIN_CELL_SIZE ? spacing : MIN_CELL_SIZE; | ||
} else { | ||
spacing = sizes[index]; | ||
} | ||
align = alignment[index]; | ||
@@ -238,3 +226,3 @@ | ||
value = align === RIGHT || align === NULL ? DASH : COLON; | ||
value += pad(sizes[index] - 2, DASH); | ||
value += pad(spacing - 2, DASH); | ||
value += align !== LEFT && align !== NULL ? COLON : DASH; | ||
@@ -251,8 +239,7 @@ | ||
/** | ||
* Get the length of `value`. | ||
* | ||
* @param {string} value | ||
* @return {number} | ||
*/ | ||
function stringify(value) { | ||
return (value === null || value === undefined) ? '' : String(value); | ||
} | ||
/* Get the length of `value`. */ | ||
function lengthNoop(value) { | ||
@@ -262,9 +249,3 @@ return String(value).length; | ||
/** | ||
* Get a string consisting of `length` `character`s. | ||
* | ||
* @param {number} length | ||
* @param {string} [character=' '] | ||
* @return {string} | ||
*/ | ||
/* Get a string consisting of `length` `character`s. */ | ||
function pad(length, character) { | ||
@@ -274,8 +255,3 @@ return Array(length + 1).join(character || SPACE); | ||
/** | ||
* Get the position of the last dot in `value`. | ||
* | ||
* @param {string} value | ||
* @return {number} | ||
*/ | ||
/* Get the position of the last dot in `value`. */ | ||
function dotindex(value) { | ||
@@ -282,0 +258,0 @@ var match = EXPRESSION_LAST_DOT.exec(value); |
{ | ||
"name": "markdown-table", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Markdown/ASCII tables", | ||
@@ -29,13 +29,10 @@ "license": "MIT", | ||
"devDependencies": { | ||
"browserify": "^13.0.0", | ||
"browserify": "^14.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", | ||
"nyc": "^10.0.0", | ||
"remark-cli": "^2.0.0", | ||
"remark-preset-wooorm": "^1.0.0", | ||
"tape": "^4.4.0", | ||
"xo": "^0.16.0" | ||
"xo": "^0.17.0" | ||
}, | ||
@@ -61,7 +58,7 @@ "scripts": { | ||
"rules": { | ||
"complexity": "off" | ||
"complexity": "off", | ||
"max-depth": "off" | ||
}, | ||
"ignores": [ | ||
"markdown-table.js", | ||
"markdown-table.min.js" | ||
"markdown-table.js" | ||
] | ||
@@ -71,15 +68,4 @@ }, | ||
"output": true, | ||
"plugins": { | ||
"comment-config": null, | ||
"lint": { | ||
"heading-increment": false, | ||
"list-item-spacing": false | ||
}, | ||
"github": null, | ||
"validate-links": null | ||
}, | ||
"settings": { | ||
"bullet": "*" | ||
} | ||
"presets": "wooorm" | ||
} | ||
} |
@@ -107,3 +107,3 @@ # markdown-table [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov] | ||
* `options.delimiter` (`string`, default: `' | '`) | ||
— Value to insert between cells. Careful, setting this to a | ||
— Value to insert between cells. Careful, setting this to a | ||
non-pipe breaks GitHub Flavoured Markdown; | ||
@@ -120,6 +120,10 @@ * `options.start` (`string`, default: `'| '`) | ||
— Method to detect the length of a cell (see below). | ||
* `options.pad` (`boolean`, default: `true`) | ||
— Whether to pad the markdown for table cells to make them the same | ||
width. Setting this to false will cause the table rows to | ||
remain staggered. | ||
### `stringLength(cell)` | ||
ANSI-sequences mess up tables on terminals. To fix this, you have to | ||
ANSI-sequences mess up tables on terminals. To fix this, you have to | ||
pass in a `stringLength` option to detect the “visible” length of a | ||
@@ -139,3 +143,3 @@ cell. | ||
The original idea and basic implementation was inspired by James | ||
Halliday's [text-table][] library. | ||
Halliday’s [text-table][] library. | ||
@@ -142,0 +146,0 @@ ## License |
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
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
8
168
11815
4
190