Skip to content

Commit

Permalink
Add new pad option
Browse files Browse the repository at this point in the history
Closes GH-9.
  • Loading branch information
alex-e-leon authored and wooorm committed Jan 27, 2017
1 parent c8590f8 commit a85c41d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 27 deletions.
64 changes: 38 additions & 26 deletions index.js
Expand Up @@ -15,6 +15,7 @@ var DOT = '.';
var NULL = '';

var ALLIGNMENT = [LEFT, RIGHT, CENTER, DOT, NULL];
var MIN_CELL_SIZE = 3;

/* Characters. */
var COLON = ':';
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -54,7 +54,8 @@
"xo": {
"space": true,
"rules": {
"complexity": "off"
"complexity": "off",
"max-depth": "off"
},
"ignores": [
"markdown-table.js"
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Expand Up @@ -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)`

Expand Down
32 changes: 32 additions & 0 deletions test.js
Expand Up @@ -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'],
Expand Down

0 comments on commit a85c41d

Please sign in to comment.