Dev.to WebDev ๐Ÿ›  Dev ๐Ÿ‘ 2 ๐Ÿ“– 5 min read

Our fix for Table Table 1 is one regex, and it creates the opposite bug: 4 and Table 4 are two tables with the same label

Venues name their tables however they like. One pub has tables 1 to 14, another has "Bar 3", "Snug" and "The Window Seat". PubTrivia stores whatever the host typed in a text column, so somewhere between the database and

Venues name their tables however they like. One pub has tables 1 to 14, another has "Bar 3", "Snug" and "The Window Seat". PubTrivia stores whatever the host typed in a text column, so somewhere between the database and a screen, something has to decide how to say it out loud.

On 4 November I shipped a small fix inside a commit mostly about something else, whose message ends: "Table names are free text, so only bare numbers get 'Table' in front." The bug it fixed was a lobby that said Table Table 1, because the generator had stored "Table 1" and the display code had helpfully prefixed it again.

This is the whole fix, in utils/game/table-label.ts:

export function tableLabel(tableNumber: string): string {
    const name = tableNumber.trim()
    return /^\d+[a-z]?$/i.test(name) ? `Table ${name}` : name
}

Two lines of logic. A name that is digits, with at most one trailing letter, gets "Table " in front. Anything else is left alone. It has a test with six assertions and it has never been wrong in the way it was written to avoid. I wanted to know how it behaves outside those six.

Thirty inputs

I ran the function itself, unmodified, over thirty strings a host could plausibly type or paste. These are the ones worth looking at:

input         label
"4"           "Table 4"
"12b"         "Table 12b"
"12B"         "Table 12B"
" 7 "         "Table 7"
"01"          "Table 01"
"0"           "Table 0"
"4a"          "Table 4a"
"4ab"         "4ab"
"10th"        "10th"
"12-b"        "12-b"
"#4"          "#4"
"T4"          "T4"
"Table4"      "Table4"
"Table 1"     "Table 1"
"Bar 3"       "Bar 3"
"Snug"        "Snug"
"1.5"         "1.5"
"ูค"           "ูค"          (Arabic-Indic four)
"๏ผ”"          "๏ผ”"         (fullwidth four)
"4<ZWSP>"     "4<ZWSP>"    (digit, then a zero-width space)
"4<NBSP>"     "Table 4"    (digit, then a non-breaking space)
""            ""
"   "         ""

Most of that is the design working. "4ab" and "10th" are not bare numbers, so they stay as typed, which is arguably right: nobody says "Table 10th". The regex's \d is ASCII only in JavaScript, so Arabic-Indic and fullwidth digits are treated as names, which means a venue typing numerals in its own script gets no prefix, again arguably what you would want.

Two rows are less comfortable.

The zero-width space. String.prototype.trim removes whitespace and line terminators. A zero-width space is neither, so a "4" that picked up an invisible character while being pasted from a spreadsheet or a chat message is not a bare number any more and displays without its prefix. The non-breaking space next to it is trimmed fine. They look identical on screen and take different branches.

The empty string. tableLabel("") is "". On a screen that renders the venue and table as {venueName} ยท {label}, that is a trailing dot and nothing after it. Whether an empty name can be stored depends on the write path, and I read it:

  • The custom list is split on newlines, trimmed and passed through .filter(Boolean), so blank lines never become tables.
  • The generator always builds "<label> <number>".
  • The rename action, updateTable, does tableNumber.trim() and writes it. It does not check that anything is left.

So the rename box can store an empty name. It is an edge nobody has hit, I have no evidence anyone has, and it is a one-line guard. I am listing it because the other two paths both refuse blanks and this is the one that does not.

The collision

The part that surprised me is not in the function at all. It is between the function and the code that decides whether two tables are the same. When a host creates a batch, createTables removes duplicates before inserting:

tableNames = Array.from(new Set(tableNames))

That compares the stored strings. And the display function maps different stored strings to the same label:

tableLabel("4")        // "Table 4"
tableLabel("Table 4")  // "Table 4"

"4" and "Table 4" are different strings, so the Set keeps both. They are two real tables, with two QR codes, in one venue, and both appear on every screen as Table 4. A host who types a custom list that mixes the styles, say 4 and Table 4 while converting from a paper plan, gets no error and two indistinguishable tables on a leaderboard. The same happens through rename: change table "9" to "Table 4" and the dedupe never runs, because updateTable has none.

The dedupe is also narrower than it looks. It only compares names within one submission, and the tables table has no uniqueness constraint on (venue_id, table_number), so custom names typed in two separate batches can repeat exactly. The two functions disagree about identity, and each is correct by its own definition. The fix I would make is the least clever one: dedupe on the label, not the stored name.

const seen = new Set<string>()
tableNames = tableNames.filter((name) => {
    const key = tableLabel(name).toLowerCase()
    if (seen.has(key)) return false
    seen.add(key)
    return true
})

That also fixes the case sensitivity, which is the quieter relative of the same bug: Set treats "Snug" and "snug" as different tables too.

What the six assertions do not say

The existing test pins down what the function should do to the names it was written for. It does not assert anything about what two names have in common, because a single-argument function cannot. The missing test is a property over the pair of functions:

// no two stored names in one venue may share a label
expect(new Set(stored.map(tableLabel)).size).toBe(stored.length)

That test would have failed on the day the fix shipped.

Why I find this interesting

"Table Table 1" is a bug you see in thirty seconds. This one you only see when you ask what a name is for. A venue name is at once an identifier we store, a label we show, and a thing a person says across a room. The fix for one use quietly changed the other two, and it is the second reader, the person looking at a leaderboard, who gets two tables with one name.

If you want to see where table labels are used, the QR joining page explains how each table gets its own code, and the scoresheet generator is a free way to print something with table names on it, with no account.

๐Ÿ“ฐ Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.