Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Invalid regular expression: /(^.{1,0}(\s+|$))|(^.{1,-1}(\\|/|_|\.|,|;|-))/: numbers out of order in {} quantifier #182

Closed
sebinsua opened this issue Sep 1, 2021 · 1 comment · Fixed by #197

Comments

@sebinsua
Copy link

sebinsua commented Sep 1, 2021

The following error can be produced by setting wrapWord on a column that only contains empty cells.

1) drawTable
       should not break when wrapWord is set on a column with empty cells:
     SyntaxError: Invalid regular expression: /(^.{1,0}(\s+|$))|(^.{1,-1}(\\|/|_|\.|,|;|-))/: numbers out of order in {} quantifier
      at new RegExp (<anonymous>)
      at calculateStringLengths (src/wrapWord.ts:10:14)
      at Object.wrapWord (src/wrapWord.ts:41:3)
      at Object.wrapCell (src/wrapCell.ts:27:20)
      at Object.calculateCellHeight (src/calculateCellHeight.ts:9:10)
      at /Users/sebinsua/dev/table/src/calculateRowHeights.ts:17:26
      at Array.forEach (<anonymous>)
      at /Users/sebinsua/dev/table/src/calculateRowHeights.ts:16:9
      at Array.map (<anonymous>)
      at Object.calculateRowHeights (src/calculateRowHeights.ts:13:15)
      at Object.table (src/table.ts:44:22)
      at Context.<anonymous> (test/table.ts:29:7)
      at processImmediate (node:internal/timers:464:21)

A test to show this:

describe("drawTable", () => {
  it("should not break when wrapWord is set on a column with empty cells", () => {
    const config: TableUserConfig = {
      columnDefault: {
        wrapWord: true,
      },
    };

    expect(
      table(
        [
          ["this", ""],
          ["breaks", ""],
          ["badly", ""],
        ],
        config
      )
    ).to.be.deep.equal(
      `
╔════════╤══╗
║ this   │  ║
╟────────┼──╢
║ breaks │  ║
╟────────┼──╢
║ badly  │  ║
╚════════╧══╝
`.trimLeft()
    );
  });
});

The error message appears to come from this function:

table/src/wrapWord.ts

Lines 4 to 35 in 0553391

const calculateStringLengths = (input: string, size: number): Array<[Length:number, Offset: number]> => {
let subject = stripAnsi(input);
const chunks: Array<[number, number]> = [];
// https://regex101.com/r/gY5kZ1/1
const re = new RegExp('(^.{1,' + String(size) + '}(\\s+|$))|(^.{1,' + String(size - 1) + '}(\\\\|/|_|\\.|,|;|-))');
do {
let chunk: string;
const match = re.exec(subject);
if (match) {
chunk = match[0];
subject = subject.slice(chunk.length);
const trimmedLength = chunk.trim().length;
const offset = chunk.length - trimmedLength;
chunks.push([trimmedLength, offset]);
} else {
chunk = subject.slice(0, size);
subject = subject.slice(size);
chunks.push([chunk.length, 0]);
}
} while (subject.length);
return chunks;
};

I haven't spent a lot of time with this codebase but could we Math.max the right side of the quantifier so it's at least as large as the left-side?

e.g.

-  const re = new RegExp('(^.{1,' + String(size) + '}(\\s+|$))|(^.{1,' + String(size - 1) + '}(\\\\|/|_|\\.|,|;|-))');
+  const re = new RegExp('(^.{1,' + String(Math.max(size, 1)) + '}(\\s+|$))|(^.{1,' + String(Math.max(size - 1, 1)) + '}(\\\\|/|_|\\.|,|;|-))');  

Or does it make sense to immediately bail while returning an empty list of chunks?

If you're happy with either of these fixes, or have your own suggestion, I can send you a PR.

@nam-hle
Copy link
Collaborator

nam-hle commented Sep 2, 2021

Yes, please. Any suggestions are welcome :)

@nam-hle nam-hle linked a pull request Dec 1, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants