Skip to content

Commit

Permalink
Fixes #11: support object keys containing non-alphanumeric characters
Browse files Browse the repository at this point in the history
  • Loading branch information
aral committed Oct 29, 2020
1 parent 21c8d11 commit e8c2e78
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/DataProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
7 changes: 6 additions & 1 deletion lib/JSDF.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

const { quoteKeyIfNotNumeric } = require('./Util')

class JSDF {

static serialise (value, key, parentType = null) {
Expand Down Expand Up @@ -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: {
Expand All @@ -161,7 +166,7 @@ class JSDF {
}

case 'object': {
serialisedStatement = `${key}: ${serialisedValue}`
serialisedStatement = `${quoteKeyIfNotNumeric(key)}: ${serialisedValue}`
break
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down
12 changes: 9 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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._ } })();
`

Expand Down Expand Up @@ -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'
]

Expand Down Expand Up @@ -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.
//
Expand Down

0 comments on commit e8c2e78

Please sign in to comment.