Bug: string hash keys starting with a digit are not properly quoted
Reproduction
const hex = '0a17ceb403339ce6e39ad3b2273ccd18'
db.hexTest = {}
db.hexTest[hex] = 'this should work'
This creates, in db.hexText:
_ = {}
_[0a17ceb403339ce6e39ad3b2273ccd18] = 'this should work'
(Note the missing quotes around the key.)
When db
is opened/read, this will crash with the following error:
SyntaxError: Invalid or unexpected token
Analysis
The problem is with the algorithm used in lib/Util.js to determine whether a value is a number or not:
export function quoteKeyIfNotNumeric (key) {
// If a key is non-numeric, surrounds it in quotes.
// Otherwise, leave it be.
return isNaN(parseInt(key)) ? `'${key}'` : key
}
Specifically, parseInt('0a17ceb403339ce6e39ad3b2273ccd18')
returns 0
, which is seen as a number so the value is not quoted when being persisted to the database.
Fix
Improve the number detection algorithm.