Skip to content

Commit

Permalink
Merge pull request #2 from JamesxX/developer
Browse files Browse the repository at this point in the history
Float64 support and syntax fixes
  • Loading branch information
ARitz-Cracker authored Jun 26, 2016
2 parents 8227ee6 + 8fa73e4 commit cfdf493
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Add unit tests for any new or changed functionality. Lint and test your code.

## Release History

* 1.0.2 Added support for NaN and 64 bit floats
* 1.0.0 Added support for arrays, booleans, and nulls!
* 0.9.2 ???
* 0.9.0 Major update in module structure, and added error handling.
Expand Down
17 changes: 17 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Swift-Cardinal Object Notation
* https://github.com/JamesxX/scon
*
* Copyright (c) Aritz Beobide-Cardinal, 2016 James R Swift
* Licensed under the GNU GPLv3 license.
*/

"use strict";

var scon = require( "./index.js" );

var encoded = scon.encode( { hello: "world!!", hi: "bye", five: NaN, pi: 3.14159, object:{amazing:true,true:{"mind":"fuck"}}, six: 6 ,arr:["wan","too","free",{"for":4},[1,2,3,4,5]]});
console.log( "encoded:", encoded );

var decoded = scon.decode( encoded.result );
console.log( "decoded:", decoded.result);
14 changes: 2 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Swift Binary Tag
* Swift-Cardinal Object Notation
* https://github.com/JamesxX/scon
*
* Copyright (c) Aritz Beobide-Cardinal, 2016 James R Swift
Expand All @@ -8,22 +8,12 @@

"use strict";

var Buffer = require('buffer').Buffer;

var scon = {}
var scon = {};
var sconUtil = require( "./lib/util.js" );

sconUtil.loadSubmodule( scon, "lib/conf.js" );
sconUtil.loadSubmodule( scon, "lib/error.js" );
sconUtil.loadSubmodule( scon, "lib/encode.js" );
sconUtil.loadSubmodule( scon, "lib/decode.js" );

if ( true ){
var encoded = scon.encode( { hello: "world!!", hi: "bye", five: 5, pi: 3.14159, object:{amazing:true,true:{"mind":"fuck"}}, six: 6 ,arr:["wan","too","free",{"for":4},[1,2,3,4,5]]});
console.log( "encoded:", encoded );

var decoded = scon.decode( encoded.result );
console.log( "decoded:", JSON.stringify(decoded));
}

module.exports = scon;
5 changes: 3 additions & 2 deletions lib/conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Swift Binary Tag
* Swift-Cardinal Object Notation
* https://github.com/JamesxX/scon
*
* Copyright (c) Aritz Beobide-Cardinal, 2016 James R Swift
Expand All @@ -15,13 +15,14 @@ sconConf.bufferSize = 50 * 1024 // 50kb

sconConf.endBlock = 0;
sconConf.int32 = 1;
sconConf.float32 = 2;
sconConf.float64 = 2;
sconConf.string = 3;
sconConf.compound = 4;
sconConf.array = 5;
sconConf.null = 6;
sconConf.undefined = 7;
sconConf.boolean = 8;
sconConf.nan = 9;
sconConf.reserved = 255;

module.exports = sconConf;
17 changes: 11 additions & 6 deletions lib/decode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Swift Binary Tag
* Swift-Cardinal Object Notation
* https://github.com/JamesxX/scon
*
* Copyright (c) Aritz Beobide-Cardinal, 2016 James R Swift
Expand All @@ -26,8 +26,8 @@ scon.decodeValue = function(buf,type,_offset){
break;

// float32
case scon.float32:
result = buf.readFloatBE(_offset); _offset += 4;
case scon.float64:
result = buf.readDoubleBE(_offset); _offset += 8;
break;

// string <n>
Expand All @@ -45,11 +45,13 @@ scon.decodeValue = function(buf,type,_offset){
case scon.array:
result = [];
var len = buf.readUInt32BE(_offset); _offset += 4;
for (var i=0;i<len;i+=1){
for ( var i = 0; i < len ; i += 1 ){

var itype = buf.readUInt8(_offset); _offset += 1;
var a = scon.decodeValue(buf,itype,_offset)
result[ i ] = a[0];
_offset = a[1];

}
break;

Expand All @@ -64,6 +66,9 @@ scon.decodeValue = function(buf,type,_offset){
case scon.boolean:
result = buf.readUInt8(_offset) !== 0; _offset += 1;
break;
case scon.nan:
result = NaN;
break;

// end block
case scon.endBlock:
Expand All @@ -77,7 +82,7 @@ scon.decodeValue = function(buf,type,_offset){

return [result,_offset];

}
};

scon.decode = function ( string, verify, offset ){

Expand Down Expand Up @@ -117,6 +122,6 @@ scon.decode = function ( string, verify, offset ){

return { result: result, offset: _offset };

}
};

module.exports = scon;
25 changes: 17 additions & 8 deletions lib/encode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Swift Binary Tag
* Swift-Cardinal Object Notation
* https://github.com/JamesxX/scon
*
* Copyright (c) Aritz Beobide-Cardinal, 2016 James R Swift
Expand All @@ -25,16 +25,22 @@ scon.writeKey = function(buf,key,offset){
}

return offset;
}
};

scon.writeValue = function(buf,key,value,offset){

switch( typeof value ){

case "number":

//NaN
if (isNaN(value)){

buf.writeUInt8( scon.nan, offset ); offset += 1;
offset = scon.writeKey(buf,key,offset);

// Integer
if ( value % 1 === 0 ){
}else if ( value % 1 === 0 ){

buf.writeUInt8( scon.int32, offset ); offset += 1;
offset = scon.writeKey(buf,key,offset);
Expand All @@ -43,9 +49,9 @@ scon.writeValue = function(buf,key,value,offset){
// Float
} else {

buf.writeUInt8( scon.float32, offset ); offset += 1;
buf.writeUInt8( scon.float64, offset ); offset += 1;
offset = scon.writeKey(buf,key,offset);
buf.writeFloatBE( value, offset ); offset += 4;
buf.writeDoubleBE( value, offset ); offset += 8;

}

Expand Down Expand Up @@ -73,6 +79,7 @@ scon.writeValue = function(buf,key,value,offset){
}else if (value == null){

buf.writeUInt8( scon.null, offset ); offset += 1;
offset = scon.writeKey(buf,key,offset);

}else{

Expand All @@ -81,6 +88,7 @@ scon.writeValue = function(buf,key,value,offset){
offset = scon.encode( value, buf, offset ).offset;

}

break;

case "boolean":
Expand All @@ -91,17 +99,18 @@ scon.writeValue = function(buf,key,value,offset){

case "undefined":
buf.writeUInt8( scon.undefined, offset ); offset += 1;
offset = scon.writeKey(buf,key,offset);
break;

}

return offset;

}
};

scon.encode = function ( object, buf, offset, bsize ){

if (buf==null){
if ( buf == null ){
buf = new Buffer(bsize || scon.bufferSize);
buf.fill(255);
}
Expand All @@ -122,6 +131,6 @@ scon.encode = function ( object, buf, offset, bsize ){
buf.writeUInt8( scon.endBlock, offset ); offset += 1;
return { result: buf.toString( 'binary', 0, offset ), offset: offset };

}
};

module.exports = scon;
22 changes: 13 additions & 9 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@

"use strict";

var sconError = {}
var sconError = {};

sconError.errorCodes = {};
sconError.errorMesages = {};

sconError.defineError = function ( errorName, errorCode, errorMessage ){

sconError.errorCodes[ errorName ] = errorCode;
sconError.errorMesages[ errorCode ] = errorMessage;
}

};

sconError.Exception = function( errorCode , details){
this.message = sconError.errorMesages[ errorCode ];
if(details){
this.message += " ("+details+")";
}
this.name = "sconException";
this.code = errorCode;
}

this.message = sconError.errorMesages[ errorCode ];
if(details){
this.message += " ("+details+")";
}
this.name = "sconException";
this.code = errorCode;

};

/********************************************
Expand Down
6 changes: 3 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

"use strict";

var sconUtil = {}
var sconUtil = {};

sconUtil.mergeRecursive = function (obj1, obj2) {

Expand All @@ -33,13 +33,13 @@ sconUtil.mergeRecursive = function (obj1, obj2) {
}

return obj1;
}
};

sconUtil.loadSubmodule = function ( sconObject, moduleName ){

var submodule = require( "../" + moduleName );
sconUtil.mergeRecursive( sconObject, submodule );

}
};

module.exports = sconUtil;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scon",
"version": "1.0.0",
"version": "1.0.2",
"description": "Read and write scon files",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit cfdf493

Please sign in to comment.