Skip to content

Commit

Permalink
Reduce complexity in compress method
Browse files Browse the repository at this point in the history
  • Loading branch information
tcorral committed Oct 25, 2013
1 parent 9c1b8d8 commit 60a46f1
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions src/JSONC.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,43 +109,56 @@
}
return aKeys;
}
function compressArray ( json, aKeys )
{
var nIndex,
nLenKeys;

for ( nIndex = 0, nLenKeys = json.length; nIndex < nLenKeys; nIndex++ )
{
json[nIndex] = JSONC.compress( json[nIndex], aKeys );
}
}
function compressOther ( json, aKeys )
{
var oKeys,
aKey,
str,
nLenKeys,
nIndex,
obj;
aKeys = _getKeys( json, aKeys );
aKeys = aKeys.unique();
oKeys = biDimensionalArrayToObject( aKeys );

str = JSON.stringify( json );
nLenKeys = aKeys.length;

for ( nIndex = 0; nIndex < nLenKeys; nIndex++ )
{
aKey = aKeys[nIndex];
str = str.replace( new RegExp( RegExp.escape( '"' + aKey[1] + '"' ), "g" ), '"' + aKey[0] + '"' );
}

obj = JSON.parse( str );
obj._ = oKeys;
return obj;
}
JSONC.compress = function ( json, optKeys )
{
if(!optKeys)
{
_nCode = -1;
}
var aKeys = optKeys || [],
oKeys,
aKey,
obj,
str,
nIndex,
nLenKeys;
obj;
if ( _isArray( json ) )
{
for ( nIndex = 0, nLenKeys = json.length; nIndex < nLenKeys; nIndex++ )
{
json[nIndex] = this.compress( json[nIndex], aKeys );
}
compressArray( json, aKeys );
}
else
{
aKeys = _getKeys( json, aKeys );
aKeys = aKeys.unique();
oKeys = biDimensionalArrayToObject( aKeys );

str = JSON.stringify( json );
nLenKeys = aKeys.length;

for ( nIndex = 0; nIndex < nLenKeys; nIndex++ )
{
aKey = aKeys[nIndex];
str = str.replace( new RegExp( RegExp.escape( '"' + aKey[1] + '"' ), "g" ), '"' + aKey[0] + '"' );
}

obj = JSON.parse( str );
obj._ = oKeys;
obj = compressOther ( json, aKeys );
}
return obj || json;
};
Expand Down

0 comments on commit 60a46f1

Please sign in to comment.