Verified Commit e8c2e787 authored by Aral Balkan's avatar Aral Balkan
Browse files

Fixes #11: support object keys containing non-alphanumeric characters

parent 21c8d112
......@@ -11,12 +11,12 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const JSDF = require('./JSDF')
const { needsToBeProxified } = require('./Util')
const { needsToBeProxified, quoteKeyIfNotNumeric } = require('./Util')
// Note: further circular module references at end of file:
// - IncompleteQueryProxy
// - QueryProxy
const variableReference = (id, property) => `${id}[${!isNaN(parseInt(property)) ? property : `'${property}'`}]`
const variableReference = (id, property) => `${id}[${quoteKeyIfNotNumeric(property)}]`
class DataProxy {
......
......@@ -13,6 +13,8 @@
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const { quoteKeyIfNotNumeric } = require('./Util')
class JSDF {
static serialise (value, key, parentType = null) {
......@@ -153,6 +155,9 @@ class JSDF {
}
}
// Surround all non-numeric keys in quotation marks to allow
// non-alphanumerical characters in keys.
let serialisedStatement
switch(parentType) {
case null: {
......@@ -161,7 +166,7 @@ class JSDF {
}
case 'object': {
serialisedStatement = `${key}: ${serialisedValue}`
serialisedStatement = `${quoteKeyIfNotNumeric(key)}: ${serialisedValue}`
break
}
......
......@@ -15,6 +15,12 @@
const isProxy = require('util').types.isProxy
class Util {
static quoteKeyIfNotNumeric (key) {
// If a key is non-numeric, surrounds it in quotes.
// Otherwise, leaves it be.
return isNaN(parseInt(key)) ? `'${key}'` : key
}
static needsToBeProxified (object) {
return (object !== null && !isProxy(object) && typeof object === 'object')
}
......
......@@ -145,7 +145,7 @@ test('basic persistence', t => {
//
const expectedTableSourceBeforeCompaction = `
globalThis._ = [ { name: \`aral\`, age: 44 }, { name: \`laura\`, age: 34 } ];
globalThis._ = [ { 'name': \`aral\`, 'age': 44 }, { 'name': \`laura\`, 'age': 34 } ];
(function () { if (typeof define === 'function' && define.amd) { define([], globalThis._); } else if (typeof module === 'object' && module.exports) { module.exports = globalThis._ } else { globalThis.people = globalThis._ } })();
_[0]['age'] = 21;
_[0]['age'] = 43;
......@@ -173,7 +173,7 @@ test('basic persistence', t => {
//
const expectedTableSourceAfterCompaction = `
globalThis._ = [ { name: \`aral\`, age: 43 }, { name: \`laura\`, age: 33 } ];
globalThis._ = [ { 'name': \`aral\`, 'age': 43 }, { 'name': \`laura\`, 'age': 33 } ];
(function () { if (typeof define === 'function' && define.amd) { define([], globalThis._); } else if (typeof module === 'object' && module.exports) { module.exports = globalThis._ } else { globalThis.people = globalThis._ } })();
`
......@@ -332,7 +332,7 @@ test('concurrent updates', t => {
const expectedChanges = [
`_['darkMode'] = \`always-on\`;\n`,
`_['colours'] = { red: \`#AA0000\`, green: \`#00AA00\`, magenta: \`#AA00AA\` };\n`,
`_['colours'] = { 'red': \`#AA0000\`, 'green': \`#00AA00\`, 'magenta': \`#AA00AA\` };\n`,
'delete _[\'colours\'];\n'
]
......@@ -883,6 +883,12 @@ test('JSDF', t => {
testSerialisation(t, 'Object with properties', {x: 1, y: 2, z: 3})
testSerialisation(t, 'Deep object', {x: {y: {z: 'deep'}}})
// Object with non-alphanumerical characters in the key.
// See https://github.com/small-tech/jsdb/issues/4
testSerialisation(t, 'Object with non-alphanumerical characters in key', {
"@context": ["https://www.w3.org/ns/activitystreams"],
})
//
// Arrays.
//
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment