Skip to content

Commit

Permalink
Remove support for custom delimiters
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 23, 2020
1 parent b021b7a commit 83d164b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 36 deletions.
23 changes: 6 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ var minCellSize = 3
// Create a table from a matrix of strings.
function markdownTable(table, options) {
var settings = options || {}
var delimiter = settings.delimiter
var start = settings.start
var end = settings.end
var padding = settings.padding === false ? '' : space
var between = padding + verticalBar + padding
var start = settings.delimiterStart === false ? '' : verticalBar + padding
var end = settings.delimiterEnd === false ? '' : padding + verticalBar
var alignment = settings.align
var calculateStringLength = settings.stringLength || lengthNoop
var cellCount = 0
Expand All @@ -41,18 +42,6 @@ function markdownTable(table, options) {

alignment = alignment ? alignment.concat() : []

if (delimiter === null || delimiter === undefined) {
delimiter = space + verticalBar + space
}

if (start === null || start === undefined) {
start = verticalBar + space
}

if (end === null || end === undefined) {
end = space + verticalBar
}

while (++rowIndex < rowLength) {
row = table[rowIndex]

Expand Down Expand Up @@ -171,7 +160,7 @@ function markdownTable(table, options) {
}
}

rows[rowIndex] = cells.join(delimiter)
rows[rowIndex] = cells.join(between)
}

index = -1
Expand All @@ -197,7 +186,7 @@ function markdownTable(table, options) {
rule[index] = value
}

rows.splice(1, 0, rule.join(delimiter))
rows.splice(1, 0, rule.join(between))

return start + rows.join(end + lineFeed + start) + end
}
Expand Down
62 changes: 55 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,66 @@ Other values are treated as `''`, which doesn’t place the colon in the alignme
row but does align left.
*Only the lowercased first character is used, so `Right` is fine.*

###### `options.delimiter`
###### `options.padding`

Value to insert around cells (`string`, default: `' | '`).
*Careful, setting this to a non-pipe breaks Markdown*.
Whether to add a space of padding between delimiters and cells (`boolean`,
default: `true`).

###### `options.start`
When `true`, there is padding:

Value to insert at the beginning of every row (`string`, default: `'| '`).
```markdown
| Alpha | B |
| ----- | ----- |
| C | Delta |
```

When `false`, there is no padding:

```markdown
|Alpha|B|
|-|-|
|C|Delta|
```

###### `options.end`
###### `options.delimiterStart`

Whether to begin each row with the delimiter (`boolean`, default: `true`).

When `true`, there are starting delimiters:

```markdown
| Alpha | B |
| ----- | ----- |
| C | Delta |
```

Value to insert at the end of every row (`string`, default: `' |'`).
When `false`, there are no starting delimiters:

```markdown
Alpha | B |
----- | ----- |
C | Delta |
```

###### `options.delimiterEnd`

Whether to end each row with the delimiter (`boolean`, default: `true`).

When `true`, there are ending delimiters:

```markdown
| Alpha | B |
| ----- | ----- |
| C | Delta |
```

When `false`, there are no ending delimiters:

```markdown
| Alpha | B
| ----- | -----
| C | Delta
```

###### `options.stringLength`

Expand Down
24 changes: 12 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ test('table()', function(t) {
['master', '0123456789abcdef'],
['staging', 'fedcba9876543210']
],
{delimiter: ' - '}
{padding: false}
),
[
'| Branch - Commit |',
'| ------- - ---------------- |',
'| master - 0123456789abcdef |',
'| staging - fedcba9876543210 |'
'|Branch |Commit |',
'|-------|----------------|',
'|master |0123456789abcdef|',
'|staging|fedcba9876543210|'
].join('\n'),
'should create a table delimited by `delimiter`'
'should create a table without padding'
)

t.equal(
Expand All @@ -170,7 +170,7 @@ test('table()', function(t) {
'| master | 0123456789abcdef |',
'| staging | fedcba9876543210 |'
].join('\n'),
'should create a table without padding'
'should create a table without aligned delimiters'
)

t.equal(
Expand All @@ -190,7 +190,7 @@ test('table()', function(t) {
'| staging | fedcba9876543210 |',
'| develop | |'
].join('\n'),
'handles short rules and missing elements for tables without padding'
'handles short rules and missing elements for tables w/o aligned delimiters'
)

t.test(
Expand All @@ -200,15 +200,15 @@ test('table()', function(t) {
['master', '0123456789abcdef'],
['staging', 'fedcba9876543210']
],
{start: ''}
{delimiterStart: false}
),
[
'Branch | Commit |',
'------- | ---------------- |',
'master | 0123456789abcdef |',
'staging | fedcba9876543210 |'
].join('\n'),
'should create a table without starting border'
'should create rows without starting delimiter'
)

t.test(
Expand All @@ -218,15 +218,15 @@ test('table()', function(t) {
['master', '0123456789abcdef'],
['staging', 'fedcba9876543210']
],
{end: ''}
{delimiterEnd: false}
),
[
'| Branch | Commit ',
'| ------- | ----------------',
'| master | 0123456789abcdef',
'| staging | fedcba9876543210'
].join('\n'),
'should create a table without ending border'
'should create rows without ending delimiter'
)

t.test(
Expand Down

0 comments on commit 83d164b

Please sign in to comment.