Skip to content

Commit

Permalink
fallback to hand-written revive() if JSON.parse does not have 2 of ar…
Browse files Browse the repository at this point in the history
…gument length
  • Loading branch information
quickredfox committed Jun 14, 2012
1 parent 9235a4a commit acc95f0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
52 changes: 33 additions & 19 deletions jquery-ajax-reviver-1.0.js → jquery-ajax-reviver-1.1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* jQuery Ajax Reviver Plugin - v1.0 - 06/13/2012
* jQuery Ajax Reviver Plugin - v1.1 - 06/13/2012
*
* Copyright (c) 2012 "Quickredfox" Francois Lafortune
* Licensed under the same conditions as jQuery itself.
Expand All @@ -12,28 +12,42 @@
"use strict"

var revive
, cast
, add;

if( $.type( $.ajaxSettings.revivers ) !== 'array' )
$.ajaxSettings.revivers = [];

revive = function( data, revivers ) {
if( $.type( data ) === 'array'){
return data.reduce( function( revived, value, key ) {
revived[ key ] = revive( revivers.reduce( function( value, reviver ) {
return reviver.call( revived, key, value );
}, value ) , revivers );
return revived;
}, data );
}else if( $.type( data ) === 'object'){
return Object.keys(data).reduce( function( revived, key ) {
var value = data[key]
revived[ key ] = revive( revivers.reduce( function( value, reviver ) {
return reviver.call( revived, key, value );
}, value ) , revivers );
return revived;
}, data );
}else return json;
if( $.type( data ) === 'array'){
return data.reduce( function( revived, value, key ) {
revived[ key ] = revive( revivers.reduce( function( value, reviver ) {
return reviver.call( revived, key, value );
}, value ) , revivers );
return revived;
}, data );
}else if( $.type( data ) === 'object'){
return Object.keys(data).reduce( function( revived, key ) {
var value = data[key]
revived[ key ] = revive( revivers.reduce( function( value, reviver ) {
return reviver.call( revived, key, value );
}, value ) , revivers );
return revived;
}, data );
}else return json;
};

cast = function( ) {
var fns = []
, args = Array.prototype.slice.call( arguments );
for( var i =0; i< args.length; i++){
var current = args[i];
var next = args[i+1];
if( $.type( current ) === 'array' ){

};
};

};

add = function( collection, reviver, fn ) {
Expand Down Expand Up @@ -62,11 +76,11 @@
add( options.revivers, original.revivers )
return options.converters['text json'] = function( data ) {
if ($.type(data ) !== 'string') return null;
else return JSON.parse( data, function( key, value ) {
else return JSON.parse.length === 2 ? JSON.parse( data, function( key, value ) {
return options.revivers.reduce( function( newvalue, reviver ) {
return reviver.call( data, key, newvalue );
}, value );
} )
} ) : revive( JSON.parse(value), options.revivers );
};
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<link rel="stylesheet" href="qunit.css" type="text/css" media="screen" charset="utf-8">
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript" charset="utf-8"></script>
<script src="qunit.js" type="text/javascript" charset="utf-8"></script>
<script src="../jquery-ajax-reviver-1.0.js" type="text/javascript" charset="utf-8"></script>
<script src="../jquery-ajax-reviver-1.1.js" type="text/javascript" charset="utf-8"></script>
<script src="test.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
Expand Down

0 comments on commit acc95f0

Please sign in to comment.