From a85c41d40ebda85515f883097a60641c44fc81ba Mon Sep 17 00:00:00 2001 From: alex-e-leon Date: Sat, 28 Jan 2017 02:51:54 +1100 Subject: [PATCH] Add new `pad` option Closes GH-9. --- index.js | 64 +++++++++++++++++++++++++++++++--------------------- package.json | 3 ++- readme.md | 4 ++++ test.js | 32 ++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index c9c916a..1098bc4 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ var DOT = '.'; var NULL = ''; var ALLIGNMENT = [LEFT, RIGHT, CENTER, DOT, NULL]; +var MIN_CELL_SIZE = 3; /* Characters. */ var COLON = ':'; @@ -75,7 +76,7 @@ function markdownTable(table, options) { position = row[index] ? dotindex(row[index]) : null; if (!sizes[index]) { - sizes[index] = 3; + sizes[index] = MIN_CELL_SIZE; } if (position > sizes[index]) { @@ -117,11 +118,7 @@ function markdownTable(table, options) { while (++index < cellCount) { value = row[index]; - if (value === null || value === undefined) { - value = ''; - } else { - value = String(value); - } + value = stringify(value); if (alignment[index] === DOT) { position = dotindex(value); @@ -151,7 +148,7 @@ function markdownTable(table, options) { value = cells[index]; if (!sizes[index]) { - sizes[index] = 3; + sizes[index] = MIN_CELL_SIZE; } size = calculateStringLength(value); @@ -169,31 +166,33 @@ function markdownTable(table, options) { index = -1; - 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; } rows[rowIndex] = cells.join(delimiter); @@ -204,11 +203,20 @@ function markdownTable(table, options) { rule = []; 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]; /* When `align` is left, don't add colons. */ 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; rule[index] = value; @@ -220,6 +228,10 @@ function markdownTable(table, options) { return start + rows.join(end + NEW_LINE + start) + end; } +function stringify(value) { + return (value === null || value === undefined) ? '' : String(value); +} + /* Get the length of `value`. */ function lengthNoop(value) { return String(value).length; diff --git a/package.json b/package.json index b491fc0..876130a 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,8 @@ "xo": { "space": true, "rules": { - "complexity": "off" + "complexity": "off", + "max-depth": "off" }, "ignores": [ "markdown-table.js" diff --git a/readme.md b/readme.md index d623d43..dfe54d7 100644 --- a/readme.md +++ b/readme.md @@ -116,6 +116,10 @@ The following options are available: * `options.stringLength` ([`Function`][length], default: `s => s.length`) — 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)` diff --git a/test.js b/test.js index 7aa82cc..2c608fe 100644 --- a/test.js +++ b/test.js @@ -182,6 +182,38 @@ test('table()', function (t) { 'should create a table delimited by `delimiter`' ); + t.equal( + table([ + ['Branch', 'Commit'], + ['master', '0123456789abcdef'], + ['staging', 'fedcba9876543210'] + ], {pad: false}), + [ + '| Branch | Commit |', + '| ------ | ------ |', + '| master | 0123456789abcdef |', + '| staging | fedcba9876543210 |' + ].join('\n'), + 'should create a table without padding' + ); + + t.equal( + table([ + ['A'], + ['', '0123456789abcdef'], + ['staging', 'fedcba9876543210'], + ['develop'] + ], {pad: false}), + [ + '| A | |', + '| --- | --- |', + '| | 0123456789abcdef |', + '| staging | fedcba9876543210 |', + '| develop | |' + ].join('\n'), + 'handles short rules and missing elements for tables without padding' + ); + t.test( table([ ['Branch', 'Commit'],