Comparing version 1.2.1 to 1.2.2
@@ -10,3 +10,3 @@ { | ||
"args": [ | ||
"${workspaceFolder}/tests/encode_arr_test.js" | ||
"${workspaceFolder}/tests/datahandler_test.js" | ||
], | ||
@@ -13,0 +13,0 @@ "internalConsoleOptions": "openOnSessionStart" |
{ | ||
"name": "nimnjs", | ||
"version": "1.2.1", | ||
"description": "Schema aware compression of JSON data", | ||
"version": "1.2.2", | ||
"description": "Schema aware compression of JS object/JSON data", | ||
"main": "src/nimn.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -17,4 +17,2 @@ var char = require("./util").char; | ||
emptyValue: char(178), | ||
//yesChar : char(217), | ||
//noChar : char(218), | ||
boundryChar : char(186), | ||
@@ -33,4 +31,2 @@ arrayEnd: char(197), | ||
chars.emptyChar, | ||
//chars.yesChar, | ||
//chars.noChar, | ||
chars.arrayEnd, | ||
@@ -37,0 +33,0 @@ chars.objStart, |
@@ -22,2 +22,4 @@ /** | ||
} | ||
this.charcodes = Object.keys(charset); | ||
} | ||
@@ -27,2 +29,3 @@ if(treatAsUnique){ | ||
} | ||
//this.treatAsUnique = treatAsUnique; | ||
@@ -51,7 +54,3 @@ } | ||
DataHandler.prototype.getCharCodes =function(){ | ||
if(this.char2val){ | ||
return Object.keys(this.char2val); | ||
}else{ | ||
return []; | ||
} | ||
return this.charcodes; | ||
} | ||
@@ -58,0 +57,0 @@ |
var chars = require("./chars").chars; | ||
var getKey = require("./util").getKey; | ||
var dataType = require("./schema").dataType; | ||
decoder.prototype._d = function(schema){ | ||
if(this.currentChar() === chars.nilChar || this.currentChar() === chars.nilPremitive){ | ||
if(ifNil(this.currentChar())){ | ||
this.index++; | ||
return null; | ||
}else if(this.currentChar() === chars.missingChar || this.currentChar() === chars.missingPremitive){ | ||
}else if(ifMissing(this.currentChar())){ | ||
this.index++; | ||
return undefined; | ||
}else{ | ||
if(typeof schema === "string"){//premitive | ||
return this.readPremitiveValue(schema); | ||
}else if(typeof schema.type === "string"){//premitive | ||
return this.readPremitiveValue(schema); | ||
}else if(Array.isArray(schema)){ | ||
if(this.currentChar() === chars.emptyChar){ | ||
this.index++; | ||
return []; | ||
}else if(this.currentChar() !== chars.arrStart){ | ||
throw Error("Parsing error: Array start char was expected"); | ||
}else{ | ||
if(Array.isArray(schema)){ | ||
if(this.currentChar() === chars.emptyChar){ | ||
this.index++; | ||
return []; | ||
}else if(this.currentChar() !== chars.arrStart){ | ||
throw Error("Parsing error: Array start char was expected"); | ||
}else{ | ||
this.index++;//skip array start char | ||
var item = schema[0]; | ||
var obj = [] | ||
do{ | ||
var r = this._d(item) ; | ||
if(r !== undefined){ | ||
obj.push(r); | ||
} | ||
}while(this.dataToDecode[this.index] !== chars.arrayEnd); | ||
++this.index; | ||
return obj; | ||
this.index++;//skip array start char | ||
var item = schema[0]; | ||
var obj = [] | ||
do{ | ||
var r = this._d(item) ; | ||
if(r !== undefined){ | ||
obj.push(r); | ||
} | ||
}else{//object | ||
if(this.currentChar() === chars.emptyChar){ | ||
this.index++; | ||
return {}; | ||
}else if(this.currentChar() !== chars.objStart){ | ||
throw Error("Parsing error: Object start char was expected : " + this.currentChar()); | ||
}else{ | ||
this.index++;//skip object start char | ||
var keys = Object.keys(schema); | ||
var obj = {}; | ||
for(var i in keys){ | ||
var r = this._d(schema[keys[i]]) ; | ||
if(r !== undefined){ | ||
obj[keys[i]] = r; | ||
} | ||
} | ||
return obj; | ||
}while(this.dataToDecode[this.index] !== chars.arrayEnd); | ||
++this.index; | ||
return obj; | ||
} | ||
}else{//object | ||
if(this.currentChar() === chars.emptyChar){ | ||
this.index++; | ||
return {}; | ||
}else if(this.currentChar() !== chars.objStart){ | ||
throw Error("Parsing error: Object start char was expected : " + this.currentChar()); | ||
}else{ | ||
this.index++;//skip object start char | ||
var keys = Object.keys(schema); | ||
var len = keys.length; | ||
var obj = {}; | ||
for(var i=0; i< len; i++){ | ||
var r = this._d(schema[keys[i]]) ; | ||
if(r !== undefined){ | ||
obj[keys[i]] = r; | ||
} | ||
} | ||
return obj; | ||
} | ||
} | ||
} | ||
function ifNil(ch){ | ||
return ch === chars.nilChar || ch === chars.nilPremitive; | ||
} | ||
function ifMissing(ch){ | ||
return ch === chars.missingChar || ch === chars.missingPremitive; | ||
} | ||
/** | ||
@@ -67,14 +69,5 @@ * returns character index pointing to | ||
decoder.prototype.readPremitiveValue = function(schemaOfCurrentKey){ | ||
var val = ""; | ||
//if(schemaOfCurrentKey.readUntil){ | ||
val = this.readFieldValue(schemaOfCurrentKey.readUntil); | ||
/* }else{ | ||
val = this.currentChar(); | ||
this.index += val.length; | ||
} */ | ||
if(val === chars.emptyValue){ | ||
val = ""; | ||
} | ||
var val = this.readFieldValue(schemaOfCurrentKey); | ||
if(this.currentChar() === chars.boundryChar) this.index++; | ||
var dh = this.dataHandlers[schemaOfCurrentKey]; | ||
var dh = this.dataHandlers[schemaOfCurrentKey.type]; | ||
return dh.parseBack(val); | ||
@@ -85,17 +78,18 @@ } | ||
* Read characters until app supported char is found | ||
* @param {string} str | ||
* @param {number} i | ||
*/ | ||
decoder.prototype.readFieldValue = function(until){ | ||
until = this.handledChars; | ||
if(until.indexOf(this.dataToDecode[this.index]) !== -1 ){ | ||
decoder.prototype.readFieldValue = function(schemaOfCurrentKey){ | ||
if(schemaOfCurrentKey.readUntil){ | ||
if(this.currentChar() === chars.emptyValue){ | ||
this.index++; | ||
return ""; | ||
}else{ | ||
var until = schemaOfCurrentKey.readUntil; | ||
var len = this.dataToDecode.length; | ||
var start = this.index; | ||
for(;this.index < len && until.indexOf(this.currentChar()) === -1;this.index++); | ||
return this.dataToDecode.substr(start, this.index-start); | ||
} | ||
}else{ | ||
return this.dataToDecode[this.index++]; | ||
}else{ | ||
var val = ""; | ||
var len = this.dataToDecode.length; | ||
var start = this.index; | ||
for(;this.index < len && until.indexOf(this.dataToDecode[this.index]) === -1;this.index++); | ||
return this.dataToDecode.substr(start, this.index-start); | ||
} | ||
@@ -112,8 +106,6 @@ } | ||
function decoder(schema,dataHandlers,charArr){ | ||
function decoder(schema,dataHandlers){ | ||
this.schema = schema; | ||
this.handledChars = charArr; | ||
this.dataHandlers = dataHandlers; | ||
} | ||
module.exports = decoder; |
var chars = require("./chars").chars; | ||
var getKey = require("./util").getKey; | ||
var dataType = require("./schema").dataType; | ||
var DataType = require("./schema").DataType; | ||
var charsArr = require("./chars").charsArr; | ||
var appCharsArr = require("./chars").charsArr; | ||
Encoder.prototype._e = function(jObj,e_schema){ | ||
if(typeof e_schema === "string"){//premitive | ||
return this.getValue(jObj,e_schema); | ||
if(typeof e_schema.type === "string"){//premitive | ||
return this.getValue(jObj,e_schema.type); | ||
}else{ | ||
@@ -22,5 +19,2 @@ var hasValidData = hasData(jObj); | ||
str = this.processValue(str,r); | ||
/* if(arr_len > ++arr_i){ | ||
str += chars.arraySepChar; | ||
} */ | ||
} | ||
@@ -58,7 +52,8 @@ str += chars.arrayEnd;//indicates that next item is not array item | ||
Encoder.prototype.getValue= function(a,type){ | ||
if(a === undefined) return chars.missingPremitive; | ||
else if(a === null) return chars.nilPremitive; | ||
else if( a === "") return chars.emptyValue; | ||
else return this.dataHandlers[type].parse(a); | ||
//else return type.parse(a); | ||
switch(a){ | ||
case undefined: return chars.missingPremitive; | ||
case null: return chars.nilPremitive; | ||
case "": return chars.emptyValue; | ||
default: return this.dataHandlers[type].parse(a); | ||
} | ||
} | ||
@@ -90,3 +85,5 @@ | ||
this.dataHandlers = dHandlers; | ||
this.handledChars = charArr; | ||
this.handledChars = appCharsArr.slice(); | ||
this.handledChars = this.handledChars.concat(charArr); | ||
this.schema = schema; | ||
@@ -93,0 +90,0 @@ } |
var boolean = require("./parsers/boolean"); | ||
var numParser = require("./parsers/number"); | ||
var dataType = require("./schema").dataType; | ||
var chars = require("./chars").chars; | ||
var appCharsArr = require("./chars").charsArr; | ||
var helper = require("./helper"); | ||
var schemaMarker = require("./schemaMarker"); | ||
var Decoder = require("./decoder"); | ||
@@ -12,3 +12,3 @@ var Encoder = require("./encoder"); | ||
function nimn() { | ||
this.handledChars = appCharsArr.slice(); | ||
this.handledChars = [];//appCharsArr.slice(); | ||
this.dataHandlers = {}; | ||
@@ -41,3 +41,4 @@ this.addDataHandler("boolean",null,null,boolean.charset,true); | ||
this.schema = JSON.parse(JSON.stringify(schema)); | ||
helper.validateSchema(schema,this.dataHandlers); | ||
new schemaMarker(this.dataHandlers).markNextPossibleChars(this.schema); | ||
//helper.validateSchema(schema,this.dataHandlers); | ||
this.encoder = new Encoder(this.schema,this.dataHandlers,this.handledChars); | ||
@@ -64,6 +65,6 @@ } | ||
* @param {Object} charset - map of charset and fixed values | ||
* @param {boolean} [dontSeparateWIthBoundaryChar=false] - if true encoder will not separate given type's value with boundary char | ||
* @param {boolean} [noBoundaryChar=false] - if true encoder will not separate given type's value with boundary char | ||
*/ | ||
nimn.prototype.addDataHandler = function(type,parseWith,parseBackWith,charset,dontSeparateWIthBoundaryChar){ | ||
var dataHandler = new DataHandler(type,/* parseWith,parseBackWith, */charset,dontSeparateWIthBoundaryChar); | ||
nimn.prototype.addDataHandler = function(type,parseWith,parseBackWith,charset,noBoundaryChar){ | ||
var dataHandler = new DataHandler(type,/* parseWith,parseBackWith, */charset,noBoundaryChar); | ||
if(parseWith) dataHandler.parse = parseWith; | ||
@@ -73,3 +74,3 @@ if(parseBackWith) dataHandler.parseBack = parseBackWith; | ||
//unque charset don't require boundary char. Hence check them is they are already added | ||
if(dontSeparateWIthBoundaryChar && charset){ | ||
if(noBoundaryChar && charset){ | ||
var keys = Object.keys(charset); | ||
@@ -79,4 +80,4 @@ | ||
var ch = keys[k]; | ||
if(this.handledChars.indexOf(ch) !== -1){ | ||
throw Error("Charset Error: "+ ch +" is not allowed. Either it is reserved or being used by another data handler"); | ||
if(this.handledChars.indexOf(ch) !== -1 || appCharsArr.indexOf(ch) !== -1){ | ||
throw Error("DataHandler Error: "+ ch +" is not allowed. Either it is reserved or being used by another data handler"); | ||
}else{ | ||
@@ -96,5 +97,5 @@ this.handledChars.push(ch); | ||
nimn.prototype.decode= function(encodedVal){ | ||
var decoder = new Decoder(this.schema,this.dataHandlers,this.handledChars); | ||
var decoder = new Decoder(this.schema,this.dataHandlers); | ||
return decoder.decode(encodedVal); | ||
} | ||
module.exports = nimn; |
var chars = require("../chars").chars; | ||
var char = require("../util").char; | ||
function parse(val){ | ||
return val ? yes : no; | ||
} | ||
function parseBack(val){ | ||
return val === yes ? true : false; | ||
} | ||
var yes = char(217); | ||
@@ -19,4 +11,2 @@ var no = char(218); | ||
exports.parse = parse; | ||
exports.parseBack = parseBack; | ||
exports.charset = booleanCharset; |
@@ -15,7 +15,17 @@ /** | ||
*/ | ||
function getKey(obj,i){ | ||
/* function getKey(obj,i){ | ||
return obj[Object.keys(obj)[i]]; | ||
} | ||
*/ | ||
/* function indexOf(arr,searchedID) { | ||
var arrayLen = arr.length; | ||
var c = 0; | ||
while (c < arrayLen) { | ||
if (arr[c] === searchedID) return c; | ||
c++; | ||
} | ||
return -1; | ||
} */ | ||
exports.char = char; | ||
exports.getKey = getKey; | ||
//exports.indexOf = indexOf; |
24336
544