Skip to content

Commit

Permalink
Full code under test using karma-coverage.
Browse files Browse the repository at this point in the history
Adding grunt to do all the jobs.
Adding new versions.
To use JSONC with travis
  • Loading branch information
tcorral committed Oct 27, 2013
1 parent 49335c2 commit 40bbbd6
Show file tree
Hide file tree
Showing 8 changed files with 513 additions and 81 deletions.
40 changes: 21 additions & 19 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,13 @@ module.exports = function (grunt) {
configFile: 'karma.conf.js'
}
},
compress: {
main: {
options: {
mode: 'gzip'
},
expand: true,
cwd: 'versions/',
src: ['jsonc.min.js'],
dest: 'versions/'
}
},
copy: {
main: {
files: [
{expand: true, cwd: 'src/', src: ['jsonc.js'], dest: 'versions/'}
]
concat: {
options: {
separator: ';'
},
dist: {
src: ['vendor/lz-string-1.0.2.js', 'src/JSONC.js'],
dest: 'versions/jsonc.js'
}
},
uglify: {
Expand Down Expand Up @@ -66,20 +57,31 @@ module.exports = function (grunt) {
}
},
build: {
src: 'src/JSONC.js',
src: ['versions/jsonc.js'],
dest: 'versions/jsonc.min.js'
}
},
compress: {
main: {
options: {
mode: 'gzip'
},
expand: true,
cwd: 'versions/',
src: ['jsonc.min.js'],
dest: 'versions/'
}
}
});

// Load the plugins
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.loadNpmTasks('grunt-contrib-copy');

// Default task(s).
grunt.registerTask('default', ['jshint', 'karma', 'uglify', 'compress', 'copy']);
grunt.registerTask('default', ['jshint', 'karma', 'concat', 'uglify', 'compress']);

};
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
JSONC
=====
# Update to version 0.1.0

[![Build Status](https://travis-ci.org/tcorral/JSONC.png)](https://travis-ci.org/tcorral/JSONC)

[Changelog](https://raw.github.com/tcorral/JSONC/master/changelog.txt)

JSON compressor-decompressor is a library to compress big JSON data.

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
"grunt-contrib-uglify": "~0.2.1",
"grunt-contrib-jshint": "~0.5.4",
"grunt-contrib-compress": "~0.5.0",
"grunt-contrib-copy": "~0.4.1"
"grunt-contrib-copy": "~0.4.1",
"grunt-contrib-concat": "~0.3.0",
"karma-junit-reporter": "~0.1.0"
},
"github": "https://github.com/tcorral/JSONC"
}
168 changes: 139 additions & 29 deletions src/JSONC.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
_nCode = -1,
toString = {}.toString;

/**
* Checks if the value exist in the array.
* @param arr
* @param v
* @returns {boolean}
*/
function contains( arr, v )
{
var nIndex,
Expand All @@ -18,7 +24,11 @@
}
return false;
}

/**
* Removes duplicated values in an array
* @param oldArray
* @returns {Array}
*/
function unique( oldArray ) {
var nIndex,
nLen = oldArray.length,
Expand All @@ -31,19 +41,42 @@
}
return aArr;
}
/**
* Escapes a RegExp
* @param text
* @returns {*}
*/
function escapeRegExp( text )
{
return text.replace( /[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&' );
}
/**
* Returns if the obj is an object or not.
* @param obj
* @returns {boolean}
* @private
*/
function _isObject ( obj )
{
return toString.call( obj ) === '[object Object]';
}
/**
* Returns if the obj is an array or not
* @param obj
* @returns {boolean}
* @private
*/
function _isArray ( obj )
{
return toString.call( obj ) === '[object Array]';
}
function biDimensionalArrayToObject( aArr )
/**
* Converts a bidimensional array to object
* @param aArr
* @returns {{}}
* @private
*/
function _biDimensionalArrayToObject( aArr )
{
var obj = {},
nIndex,
Expand All @@ -56,6 +89,15 @@
}
return obj;
}

/**
* Convert a number to their ascii code/s.
* @param index
* @param totalChar
* @param offset
* @returns {Array}
* @private
*/
function _numberToKey ( index, totalChar, offset ){
var aArr = [],
currentChar = index;
Expand All @@ -69,10 +111,25 @@
aArr.push(currentChar + offset);
return aArr.reverse();
}

/**
* Returns the string using an array of ASCII values
* @param aKeys
* @returns {string}
* @private
*/
function _getSpecialKey ( aKeys )
{
return String.fromCharCode.apply(String, aKeys);
}

/**
* Traverse all the objects looking for keys and set an array with the new keys
* @param json
* @param aKeys
* @returns {*}
* @private
*/
function _getKeys ( json, aKeys )
{
var aKey,
Expand Down Expand Up @@ -103,7 +160,14 @@
}
return aKeys;
}
function compressArray( json, aKeys )

/**
* Method to compress array objects
* @private
* @param json
* @param aKeys
*/
function _compressArray( json, aKeys )
{
var nIndex,
nLenKeys;
Expand All @@ -113,7 +177,15 @@
json[nIndex] = JSONC.compress( json[nIndex], aKeys );
}
}
function compressOther ( json, aKeys )

/**
* Method to compress anything but array
* @private
* @param json
* @param aKeys
* @returns {*}
*/
function _compressOther ( json, aKeys )
{
var oKeys,
aKey,
Expand All @@ -123,7 +195,7 @@
obj;
aKeys = _getKeys( json, aKeys );
aKeys = unique( aKeys );
oKeys = biDimensionalArrayToObject( aKeys );
oKeys = _biDimensionalArrayToObject( aKeys );

str = JSON.stringify( json );
nLenKeys = aKeys.length;
Expand All @@ -138,6 +210,51 @@
obj._ = oKeys;
return obj;
}

/**
* Method to decompress array objects
* @private
* @param json
*/
function _decompressArray( json )
{
var nIndex, nLenKeys;

for ( nIndex = 0, nLenKeys = json.length; nIndex < nLenKeys; nIndex++ )
{
json[nIndex] = JSONC.decompress( json[nIndex] );
}
}

/**
* Method to decompress anything but array
* @private
* @param jsonCopy
* @returns {*}
*/
function _decompressOther( jsonCopy )
{
var oKeys, str, sKey;

oKeys = JSON.parse( JSON.stringify( jsonCopy._ ) );
delete jsonCopy._;
str = JSON.stringify( jsonCopy );
for( sKey in oKeys)
{
if(oKeys.hasOwnProperty(sKey))
{
str = str.replace( new RegExp( '"' + sKey + '"', 'g' ), '"' + oKeys[sKey] + '"' );
}
}
return str;
}

/**
* Compress a RAW JSON
* @param json
* @param optKeys
* @returns {*}
*/
JSONC.compress = function ( json, optKeys )
{
if(!optKeys)
Expand All @@ -149,45 +266,38 @@

if ( _isArray( json ) )
{
compressArray( json, aKeys );
_compressArray( json, aKeys );
obj = json;
}
else
{
obj = compressOther ( json, aKeys );
obj = _compressOther ( json, aKeys );
}
return obj;
};
/**
* Decompress a compressed JSON
* @param json
* @returns {*}
*/
JSONC.decompress = function ( json )
{
var nIndex,
oKeys,
str,
sKey,
nLenKeys,
var str,
jsonCopy = JSON.parse( JSON.stringify( json ) );
if ( _isArray( json ) )
if ( _isArray( jsonCopy ) )
{
for ( nIndex = 0, nLenKeys = json.length; nIndex < nLenKeys; nIndex++ )
{
json[nIndex] = this.decompress( json[nIndex] );
}
_decompressArray( jsonCopy );
}
else
{
oKeys = JSON.parse( JSON.stringify( jsonCopy._ ) );
delete jsonCopy._;
str = JSON.stringify( jsonCopy );
for( sKey in oKeys)
{
if(oKeys.hasOwnProperty(sKey))
{
str = str.replace( new RegExp( '"' + sKey + '"', 'g' ), '"' + oKeys[sKey] + '"' );
}
}
str = _decompressOther( jsonCopy );
}
return str ? JSON.parse(str): json ;
return str ? JSON.parse(str): jsonCopy ;
};

/**
* Expose the object to the namespace
* @type {{}}
*/
ns.JSONC = JSONC;
}((window.Namespace = {})));
}((this.Namespace = {})));
Loading

0 comments on commit 40bbbd6

Please sign in to comment.