Thanks to visit codestin.com
Credit goes to github.com

Skip to content

RangeError is thrown when the column width is float #94

@norbertcyran

Description

@norbertcyran

Problem Summary

If any column width is evaluated to float, RangeError will be thrown on that line:

let emptySpace = columnWidth - lineLength
const padRemainder = emptySpace % 2
...
Array(padBoth + 1 + padRemainder).join(" ")

Array constructor doesn't accept float as a parameter, therefore that exception is thrown:

RangeError: Invalid array length

Expected Result (screen shot if possible)

No exception is thrown. Column widths are always coerced to ints.

Actual Result (screen shot if possible)

Column widths can be floats, exception is thrown

Environment Information

  • OS: all

  • Node Version: reproduced on 8.x and 16.x

Steps to Reproduce

  1. Set headers with percentage widths not divisible by total table width and not exceeding the table width when summed up
  2. Try to create a table

Example affected headers:

const baseHeaders = [
  { alias: "Fixed header", width: "25%" },
  { alias: "Auto header" }
]

Error is not present when columns exceed viewport width, because in that case widths are coerced in Format.getColumnWidths, more specifically:

  if (totalWidth > availableWidth || config.FIXED_WIDTH) {
    // proportion wont be exact fit, but this method keeps us safe
    const proportion = (availableWidth / totalWidth).toFixed(2) - 0.01
    const relativeWidths = widths.map(value => Math.max(2, Math.floor(proportion * value)))
    if (config.FIXED_WIDTH) return relativeWidths

    // when proportion < 0 column cant be resized and totalWidth must overflow viewport
    if (proportion > 0) {
      const totalRelativeWidths = relativeWidths.reduce((prev, current) => prev + current)
      widths = (totalRelativeWidths < totalWidth) ? relativeWidths : widths
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions