Skip to content

Commit

Permalink
v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
quickredfox committed Jun 15, 2012
1 parent acc95f0 commit adca8b7
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 70 deletions.
54 changes: 48 additions & 6 deletions README.md
@@ -1,4 +1,4 @@
jQuery Ajax Reviver Plugin - v1.0 - 06/13/2012
jQuery Ajax Reviver Plugin - v1.1 - 06/13/2012
==============================================

Copyright (c) 2012 Francois Lafortune, @quickredfox
Expand Down Expand Up @@ -89,10 +89,11 @@ Simply pass ``revivers: true`` within your ajax options.
}
);

Need to pass in non-blogbal revivers? That's easy, the "revivers" option supports either a reviver function mapping object,
Need to pass in non-global revivers? That's easy, the "revivers" option supports either a reviver function mapping object,
an array of reviver functions or a lone reviver function, like so:

#### Per/call revivers as object mapping

$.ajax(
{
dataType: 'json',
Expand All @@ -104,6 +105,7 @@ an array of reviver functions or a lone reviver function, like so:
);

#### Per/call revivers as array

$.ajax(
{
dataType: 'json',
Expand All @@ -118,6 +120,7 @@ an array of reviver functions or a lone reviver function, like so:
);

#### Per/call single reviver as function

$.ajax(
{
dataType: 'json',
Expand All @@ -130,14 +133,53 @@ an array of reviver functions or a lone reviver function, like so:
);


*Note: The value of "this" inside the reviver function is the json object or nested object being revived.*
*Note: The value of "this" inside the reviver function is the json object or nested object who owns the key currently being passed to the reviver function.*

Caveats
-------
Get funky!
----------

- If you override the 'text json' converter, this will not work.
Version 1.1 has been slightly refactored for the sake of adding some robustness. A fun side-effect is that we can now register revivers in a couple more funky ways.

#### Multiple revivers for same key.

// Why you'd want to do this? No one knows, but it works!
$.ajaxReviver({
created_at: [
function( v ){ return new Date(v); },
function( v ){
return v.getTime();
}
]
});

#### Same as above, different syntactic approach.

$.ajaxReviver([
'created_at',
[
function( v ){ return new Date(v); },
function( v ){ return v.getTime(); }
]
]);

#### Mixed sytax! Aka: I dont know what I'm doing, but it seems to work.

$.ajaxReviver([
'created_at',
function( v ){ return new Date(v); },
{ published_at: [
function( v ){ return new Date(v); },
function( v ){ return v.getTime(); }
] },
function(key,value){
if( key === 'nested') return {};
return value;
}
]);

Caveats
-------

- If you override the 'text json' converter, <del>this will not work</del> <ins>It will be processed before revivers but adds overhead as it must be re-stringified for JSON.parse to "revive" it again. This behavior may change in subsequent versions.</ins>


104 changes: 52 additions & 52 deletions jquery-ajax-reviver-1.1.js
Expand Up @@ -17,70 +17,70 @@

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;
};


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' ){

};
};

};
, args = Array.prototype.slice.call( arguments )
, arg;
if( args.length === 0 ) return fns;
arg = args.shift();
switch( $.type( arg ) ){
case 'function' : fns.push( arg ); break;
case 'string' :
var key = arg
, fn = args.shift()
switch( $.type( fn ) ){
case 'function' :
var f = function( k, v ) { return k === key ? fn.call( this, v ) : v; };
fns.push( f )
break;
case 'array' :
fn.forEach( function( f ){
fns.push( cast.call(null, key, f )[0] );
} )
break;
default: throw new Error( 'Argument Error' ); break;
};
break;
case 'array':
Array.prototype.push.apply( fns, arg );
break;
case 'object':
Object.keys(arg).forEach( function( key ) {
fns.push( cast.call( null, key, arg[key] )[0] );
});
break;
}
return fns;
}

add = function( collection, reviver, fn ) {
if( $.type( reviver ) === 'function'){
collection.push( reviver );
}else if( $.type( reviver) === 'string' && $.type( fn ) === 'function'){
collection.push( function(k, v) {
return k === reviver ? fn( v ) : v
});
}else if( $.type( reviver ) === 'array' ){
reviver.reduce( function( revivers, fn ) {
return add( revivers, fn );
}, collection );
}else if( $.type( reviver ) === 'object' ){
Object.keys( reviver ).reduce( function( revivers, k ) {
return add( revivers, k, reviver[k]);
} , collection );
};
return collection


add = function( collection /* ... */ ) {
var args = Array.prototype.slice.call( arguments )
, collection = args.shift();
return Array.prototype.push.apply( collection, cast.apply( null, args ) );
}

// Capture 'json' dataType requests and tack-on revivers if wanted.
$.ajaxPrefilter( 'json', function(options, original, xhr) {
if (original.revivers) {
options.revivers = $.ajaxSettings.revivers;
add( options.revivers, original.revivers )
if( original.revivers !== true ){
add( options.revivers, original.revivers );
};
var converter = options.converters['text json'];
return options.converters['text json'] = function( data ) {
if ($.type(data ) !== 'string') return null;
else return JSON.parse.length === 2 ? JSON.parse( data, function( key, value ) {
if ($.type(data ) !== 'string') return data;
if( $.type(converter) === 'function'){
data = JSON.stringify( converter.call( this, data ) );
};
return JSON.parse( data, function( key, value ) {
var context = this;
return options.revivers.reduce( function( newvalue, reviver ) {
return reviver.call( data, key, newvalue );
return reviver.call( context, key, newvalue );
}, value );
} ) : revive( JSON.parse(value), options.revivers );
});
};
}
});
Expand Down
1 change: 1 addition & 0 deletions test/test-simple.json
@@ -0,0 +1 @@
{ "created_at": "Thu Jun 14 2012 13:06:45 GMT-0400 (EDT)"}

0 comments on commit adca8b7

Please sign in to comment.