Skip to content

Instantly share code, notes, and snippets.

@emmanuel
Forked from quirkey/param.js
Created October 13, 2009 06:17
Show Gist options
  • Select an option

  • Save emmanuel/209048 to your computer and use it in GitHub Desktop.

Select an option

Save emmanuel/209048 to your computer and use it in GitHub Desktop.

Revisions

  1. @quirkey quirkey revised this gist Feb 19, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion param.js
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ $.extend({
    jQuery.each( a[j], function(){
    add( j, this );
    });
    else if (a[j].constructor == 'Object()')
    else if (a[j].constructor == Object)
    s.push($.param(a[j], nested(j)));
    else
    add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] );
  2. @quirkey quirkey renamed this gist Feb 19, 2009. 1 changed file with 41 additions and 41 deletions.
    82 changes: 41 additions & 41 deletions snippet.txt → param.js
    Original file line number Diff line number Diff line change
    @@ -1,42 +1,42 @@
    $.extend({
    param: function( a, nest_in ) {
    var s = [ ];
    // check for nest
    if (typeof nest_in == 'undefined') nest_in = false;

    function nested(key) {
    if (nest_in)
    return nest_in + '[' + key + ']';
    else
    return key;
    }
    function add( key, value ){
    key = nested(key)
    s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
    };
    // If an array was passed in, assume that it is an array
    // of form elements
    if ( jQuery.isArray(a) || a.jquery )
    // Serialize the form elements
    jQuery.each( a, function(){
    add( this.name, this.value );
    });

    // Otherwise, assume that it's an object of key/value pairs
    else
    // Serialize the key/values
    for ( var j in a )
    // If the value is an array then the key names need to be repeated
    if ( jQuery.isArray(a[j]) )
    jQuery.each( a[j], function(){
    add( j, this );
    });
    else if (a[j].constructor == 'Object()')
    s.push($.param(a[j], nested(j)));
    else
    add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] );

    // Return the resulting serialization
    return s.join("&").replace(/%20/g, "+");
    }
    $.extend({
    param: function( a, nest_in ) {
    var s = [ ];
    // check for nest
    if (typeof nest_in == 'undefined') nest_in = false;

    function nested(key) {
    if (nest_in)
    return nest_in + '[' + key + ']';
    else
    return key;
    }
    function add( key, value ){
    key = nested(key)
    s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
    };
    // If an array was passed in, assume that it is an array
    // of form elements
    if ( jQuery.isArray(a) || a.jquery )
    // Serialize the form elements
    jQuery.each( a, function(){
    add( this.name, this.value );
    });

    // Otherwise, assume that it's an object of key/value pairs
    else
    // Serialize the key/values
    for ( var j in a )
    // If the value is an array then the key names need to be repeated
    if ( jQuery.isArray(a[j]) )
    jQuery.each( a[j], function(){
    add( j, this );
    });
    else if (a[j].constructor == 'Object()')
    s.push($.param(a[j], nested(j)));
    else
    add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] );

    // Return the resulting serialization
    return s.join("&").replace(/%20/g, "+");
    }
    });
  3. @quirkey quirkey created this gist Feb 19, 2009.
    42 changes: 42 additions & 0 deletions snippet.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    $.extend({
    param: function( a, nest_in ) {
    var s = [ ];
    // check for nest
    if (typeof nest_in == 'undefined') nest_in = false;

    function nested(key) {
    if (nest_in)
    return nest_in + '[' + key + ']';
    else
    return key;
    }
    function add( key, value ){
    key = nested(key)
    s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
    };
    // If an array was passed in, assume that it is an array
    // of form elements
    if ( jQuery.isArray(a) || a.jquery )
    // Serialize the form elements
    jQuery.each( a, function(){
    add( this.name, this.value );
    });

    // Otherwise, assume that it's an object of key/value pairs
    else
    // Serialize the key/values
    for ( var j in a )
    // If the value is an array then the key names need to be repeated
    if ( jQuery.isArray(a[j]) )
    jQuery.each( a[j], function(){
    add( j, this );
    });
    else if (a[j].constructor == 'Object()')
    s.push($.param(a[j], nested(j)));
    else
    add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] );

    // Return the resulting serialization
    return s.join("&").replace(/%20/g, "+");
    }
    });