Skip to content

Commit

Permalink
fix(*): Account for TableCell content potentially being undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Jun 18, 2021
1 parent 712d2f5 commit 758b6ea
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/issues/188-type-coercion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ test('188-type-coercion-weirdness', async () => {
}`
const table = (await jsonCodec.load(json)) as schema.Table

expect(table?.rows?.[0].cells?.[0].content[0]).toBe(true)
expect(table?.rows?.[0].cells?.[1].content[0]).toBe(2)
expect(table?.rows?.[0].cells?.[0].content?.[0]).toBe(true)
expect(table?.rows?.[0].cells?.[1].content?.[0]).toBe(2)

expect(await mdCodec.dump(table)).toMatchFile(
snapshot('188-type-coercion.md')
Expand Down
6 changes: 5 additions & 1 deletion src/codecs/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2267,7 +2267,11 @@ function encodeTableCell(
state: EncodeState,
tag: 'td' | 'th' = 'td'
): HTMLTableDataCellElement {
return h(tag, { attrs: microdata(cell) }, encodeNodes(cell.content, state))
return h(
tag,
{ attrs: microdata(cell) },
encodeNodes(cell.content ?? [], state)
)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/codecs/jats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1981,7 +1981,7 @@ function encodeTable(table: stencila.Table, state: EncodeState): [xml.Element] {
return elem(
'th',
...row.cells.map((cell) => {
return encodeDefault('th', cell.content, state)
return encodeDefault('th', cell.content ?? [], state)
})
)
})
Expand All @@ -1992,7 +1992,7 @@ function encodeTable(table: stencila.Table, state: EncodeState): [xml.Element] {
return elem(
'tr',
...row.cells.map((cell) => {
return encodeDefault('td', cell.content, state)
return encodeDefault('td', cell.content ?? [], state)
})
)
})
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/md/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ function encodeTable(table: stencila.Table): MDAST.Table | Extension {
type: 'tableRow',
children: row.cells.map(
(cell: stencila.TableCell): MDAST.TableCell => {
const content = ensureInlineContentArray(cell.content)
const content = ensureInlineContentArray(cell.content ?? [])
// If there is only one node in the table cell and it is
// a primitive e.g. a number, or boolean then encode it
// as a string rather than as the special `!number` etc inline
Expand Down
4 changes: 2 additions & 2 deletions src/codecs/pandoc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,15 +858,15 @@ function encodeTable(table: stencila.Table): Pandoc.Table {
let head: Pandoc.TableCell[] = []
if (table.rows.length > 0) {
head = table.rows[0].cells.map((cell) =>
encodeBlocks(ensureBlockContentArray(cell.content))
encodeBlocks(ensureBlockContentArray(cell.content ?? []))
)
}

let rows: Pandoc.TableCell[][] = []
if (table.rows.length > 1) {
rows = table.rows.slice(1).map((row: stencila.TableRow) => {
return row.cells.map((cell) =>
encodeBlocks(ensureBlockContentArray(cell.content))
encodeBlocks(ensureBlockContentArray(cell.content ?? []))
)
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/xlsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const encodeTable = (table: stencila.Table): xlsx.WorkSheet =>
table.rows.reduce(
(sheet: xlsx.WorkSheet, row) => {
row.cells.forEach((cell: stencila.TableCell) => {
if (cell.name) {
if (cell.name && cell.content !== undefined) {
sheet[cell.name] = encodeCell(cell.content[0])
}
})
Expand Down

0 comments on commit 758b6ea

Please sign in to comment.