diff --git a/package.json b/package.json index 1c7c029..b491eb9 100644 --- a/package.json +++ b/package.json @@ -16,5 +16,8 @@ "bugs": { "url": "https://github.com/flynx/serialize.js/issues" }, - "homepage": "https://github.com/flynx/serialize.js#readme" + "homepage": "https://github.com/flynx/serialize.js#readme", + "dependencies": { + "ig-test": "^1.5.9" + } } diff --git a/serialize2.js b/serialize2.js index 9ec7151..ce5655b 100644 --- a/serialize2.js +++ b/serialize2.js @@ -19,6 +19,24 @@ var RECURSIVE = '' +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +module.DEBUG = false + +var DEBUG_PREFIX = '---' + +var debug = { + context: 16, + + log: function(...args){ + if(!module.DEBUG){ + return } + return console.log(DEBUG_PREFIX, ...arguments) }, + lex: function(name, str, i, line){ + return this.log(name +':', str.slice(i, i+this.context) +'...') }, +} + + //--------------------------------------------------------------------- // @@ -158,6 +176,9 @@ module.eJSON = { '{': 'object', }, words: { + true: true, + false: false, + Infinity: 'number', 'Set(': 'set', @@ -236,6 +257,7 @@ module.eJSON = { // -> [value, i, line] // number: function(state, path, match, str, i, line){ + debug.lex('number', str, i, line) // special cases.., if(match == 'Infinity'){ return [Infinity, i, line] } @@ -252,6 +274,7 @@ module.eJSON = { return [ str.slice(i, j)*1, j, line ] }, // XXX TEST count \\n string: function(state, path, match, str, i, line){ + debug.lex('string', str, i, line) var j = i+1 while(j < str.length && str[j] != match){ @@ -274,6 +297,7 @@ module.eJSON = { this.error('Unexpected end of input wile looking fot "'+ match +'".', str, i, line) } return [ str.slice(i+1, j), j+1, line ] }, identifier: function(state, path, match, str, i, line){ + debug.lex('identifier', str, i, line) if(!/[a-zA-Z_]/.test(str[i])){ this.error('Not an identifier: "'+ str[i] +'"', str, i, line) } var j = i+1 @@ -303,7 +327,7 @@ module.eJSON = { initial instanceof Array && initial.length++ continue } - if(str.slice(i, EMPTY.length) == EMPTY){ + if(str.slice(i, i+EMPTY.length) == EMPTY){ i += EMPTY.length // XXX this feels hackish -- can this be deligated to the handler??? initial instanceof Array @@ -326,6 +350,7 @@ module.eJSON = { this.error('Unexpected end of input wile looking for "'+ end +'".', str, i, line) }, array: function(state, path, match, str, i, line){ + debug.lex('array', str, i, line) return this.sequence( state, path, str, i+1, line, ']', @@ -335,6 +360,7 @@ module.eJSON = { res[index] = obj return [res, i, line] }) }, object: function(state, path, match, str, i, line){ + debug.lex('object', str, i, line) return this.sequence( state, path, str, i+1, line, '}', @@ -359,6 +385,7 @@ module.eJSON = { {}) }, set: function(state, path, match, str, i, line){ + debug.lex('set', str, i, line) i += match.length ;[res, i, line] = this.sequence( state, path, @@ -369,33 +396,35 @@ module.eJSON = { ;[obj, i, line] = this.value( state, [...path, index], - str, i+match.length, line) + str, i, line) res.add(obj) return [res, i, line] }, new Set()) if(str[i] != ')'){ this.error('Expected ")", got "'+ str[i] +'".', str, i, line) } - return [res, i, line] }, + return [res, i+1, line] }, // XXX need to destinguish between key and value in path... map: function(state, path, match, str, i, line){ + debug.lex('map', str, i, line) i += match.length ;[res, i, line] = this.sequence( state, path, str, i+1, line, ']', function(res, index, str, i, line){ + debug.lex(' map-content', str, i, line) var obj ;[[key, value], i, line] = this.value( state, // XXX need a way to index path or value... [...path, index], - str, i+match.length, line) + str, i, line) res.set(key, value) return [res, i, line] }, - new Set()) + new Map()) if(str[i] != ')'){ this.error('Expected ")", got "'+ str[i] +'".', str, i, line) } - return [res, i, line] }, + return [res, i+1, line] }, // XXX should this be done inline or on a separate stage? // for inline we need these: @@ -413,6 +442,7 @@ module.eJSON = { // stage two... (XXX TEST) // ...need to use serialized paths as keys... recursive: function(state, path, match, str, i, line){ + debug.lex('recursive', str, i, line) return this.sequence( state, path, str, i+match.length, line, '>', @@ -428,6 +458,7 @@ module.eJSON = { }, value: function(state, path, str, i=0, line=0){ + //debug.lex('value', str, i, line) ;[i, line] = this.skipWhitespace(str, i, line) diff --git a/test.js b/test.js new file mode 100755 index 0000000..e3f2f40 --- /dev/null +++ b/test.js @@ -0,0 +1,85 @@ +#!/usr/bin/env node +/********************************************************************** +* +* +* +**********************************************************************/ +((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) +(function(require){ var module={} // make module AMD/node compatible... +/*********************************************************************/ + +var test = require('ig-test') + +var eJSON = require('./serialize2') + + +//--------------------------------------------------------------------- +// XXX split into pure and extended JSON... + +var setups = test.Setups({ + number: function(assert){ + return '123' }, + string: function(assert){ + return '"string"' }, + 'true': function(assert){ + return 'true' }, + 'false': function(assert){ + return 'false' }, + + 'null': function(assert){ + return 'null' }, + 'undefined': function(assert){ + return 'undefined' }, + 'NaN': function(assert){ + return 'NaN' }, + + 'array-empty': function(assert){ + return '[]' }, + // XXX BUG: trailing gets duplicated... + 'array-sparse': function(assert){ + return '[1,,,2,]' }, + + 'object-empty': function(assert){ + return '{}' }, + + 'set-empty': function(assert){ + return 'Set([])' }, + + 'map-empty': function(assert){ + return 'Map([])' }, + + // XXX +}) + +test.Modifiers({ + 'array-stuffed': function(assert, setup){ + return `[${ setup }]` }, + 'object-stuffed': function(assert, setup){ + return `{"key":${ setup }}` }, + 'map-stuffed': function(assert, setup){ + return `Map([["key",${ setup }],[${ setup },"value"]])` }, + 'set-stuffed': function(assert, setup){ + return `Set([${ setup }])` }, +}) + +test.Tests({ + 'self-apply': function(assert, setup){ + var res + assert( + setup == (res = eJSON.serialize(eJSON.deserialize(setup))), + 'serialize(deserialize( setup )) == setup: expected:', setup, 'got:', res) }, + +}) + + +//--------------------------------------------------------------------- + +typeof(__filename) != 'undefined' + && __filename == (require.main || {}).filename + && test.run() + + + + +/********************************************************************** +* vim:set ts=4 sw=4 nowrap : */ return module })