From cdc535abeb27ece631f6496c920a3b87301efe2a Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 23 Jan 2020 13:42:41 +0100 Subject: [PATCH] Remove `dot` alignment --- index.js | 39 ++++----------------------------------- readme.md | 22 ++-------------------- test.js | 52 ---------------------------------------------------- 3 files changed, 6 insertions(+), 107 deletions(-) diff --git a/index.js b/index.js index 0249ecc..9ac013e 100644 --- a/index.js +++ b/index.js @@ -2,14 +2,10 @@ module.exports = markdownTable -var dotRe = /\./ -var lastDotRe = /\.[^.]*$/ - // Characters. var space = ' ' var lineFeed = '\n' var dash = '-' -var dot = '.' var colon = ':' var lowercaseC = 'c' var lowercaseL = 'l' @@ -67,7 +63,7 @@ function markdownTable(table, options) { } while (++index < cellCount) { - position = row[index] ? dotindex(row[index]) : null + position = row[index] ? row[index].length : null if (!sizes[index]) { sizes[index] = minCellSize @@ -93,12 +89,7 @@ function markdownTable(table, options) { align = align.charAt(0).toLowerCase() } - if ( - align !== lowercaseL && - align !== lowercaseR && - align !== lowercaseC && - align !== dot - ) { + if (align !== lowercaseL && align !== lowercaseR && align !== lowercaseC) { align = '' } @@ -115,22 +106,7 @@ function markdownTable(table, options) { cells = [] while (++index < cellCount) { - value = row[index] - - value = stringify(value) - - if (alignment[index] === dot) { - position = dotindex(value) - - size = - sizes[index] + - (dotRe.test(value) ? 0 : 1) - - (calculateStringLength(value) - position) - - cells[index] = value + pad(size - 1) - } else { - cells[index] = value - } + cells[index] = stringify(row[index]) } rows[rowIndex] = cells @@ -173,7 +149,7 @@ function markdownTable(table, options) { position = sizes[index] - (calculateStringLength(value) || 0) spacing = pad(position) - if (alignment[index] === lowercaseR || alignment[index] === dot) { + if (alignment[index] === lowercaseR) { value = spacing + value } else if (alignment[index] === lowercaseC) { position /= 2 @@ -241,10 +217,3 @@ function lengthNoop(value) { function pad(length, character) { return new Array(length + 1).join(character || space) } - -// Get the position of the last dot in `value`. -function dotindex(value) { - var match = lastDotRe.exec(value) - - return match ? match.index + 1 : value.length -} diff --git a/readme.md b/readme.md index be51f52..6391207 100644 --- a/readme.md +++ b/readme.md @@ -64,23 +64,6 @@ Yields: | bar | 45 | lmno | ``` -Align on dots: - -```js -table([['No.'], ['0.1.2'], ['11.22.33'], ['5.6.'], ['1.22222']], {align: '.'}) -``` - -Yields: - -```markdown -| No. | -| :---------: | -| 0.1.2 | -| 11.22.33 | -| 5.6. | -| 1.22222 | -``` - ## API ### `markdownTable(table[, options])` @@ -93,15 +76,14 @@ Turns a given matrix of strings (an array of arrays of strings) into a table. One style for all columns, or styles for their respective columns (`string` or `Array.`). -Each style is either `'l'` (left), `'r'` (right), `'c'` (centre), or `'.'` -(dot). +Each style is either `'l'` (left), `'r'` (right), or `'c'` (center). Other values are treated as `''`, which doesn’t place the colon but does align left. *Only the lowercased first character is used, so `Right` is fine.* ###### `options.delimiter` -Value to insert between cells (`string`, default: `' | '`). +Value to insert around cells (`string`, default: `' | '`). *Careful, setting this to a non-pipe breaks Markdown*. ###### `options.start` diff --git a/test.js b/test.js index 94bd1d7..9e31d99 100644 --- a/test.js +++ b/test.js @@ -95,58 +95,6 @@ test('table()', function(t) { 'should align center' ) - t.equal( - table( - [ - ['Beep', 'No.'], - ['beep', '1024'], - ['boop', '334.212'], - ['foo', '1006'], - ['bar', '45.6'], - ['baz', '123.'] - ], - {align: ['', '.']} - ), - [ - '| Beep | No. |', - '| ---- | :------: |', - '| beep | 1024 |', - '| boop | 334.212 |', - '| foo | 1006 |', - '| bar | 45.6 |', - '| baz | 123. |' - ].join('\n'), - 'should align dots' - ) - - t.equal( - table( - [ - ['No.'], - ['0.1.2'], - ['11.22.33'], - ['5.6.7'], - ['1.22222'], - ['12345.'], - ['5555.'], - ['123'] - ], - {align: ['.']} - ), - [ - '| No. |', - '| :---------: |', - '| 0.1.2 |', - '| 11.22.33 |', - '| 5.6.7 |', - '| 1.22222 |', - '| 12345. |', - '| 5555. |', - '| 123 |' - ].join('\n'), - 'should align multiple dots in a cell' - ) - t.equal( table( [