Skip to content

Commit

Permalink
Remove dot alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 23, 2020
1 parent 77ed1be commit cdc535a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 107 deletions.
39 changes: 4 additions & 35 deletions index.js
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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 = ''
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
22 changes: 2 additions & 20 deletions readme.md
Expand Up @@ -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])`
Expand All @@ -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.<string>`).
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`
Expand Down
52 changes: 0 additions & 52 deletions test.js
Expand Up @@ -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(
[
Expand Down

0 comments on commit cdc535a

Please sign in to comment.