Good points about the zip code, and running $.query() all the time, Mike.

Maybe the two approaches can be combined? Run the code to munge the 
query string the first time $.query() is called. Cache two sets of 
results -- a 'raw' set and a 'type cast' set -- in a closure, and return 
one of the cached objects on subsequent calls to $.query()

(function(){
        
        var after_type_cast = {};
        var before_type_cast = {};
        var cached = false;
        
        jQuery.query = function(cast) {
                if(!cached) {
                        // remove leading ? and trailing &
                        var q = 
location.search.replace(/^\?/,'').replace(/\&$/,'').split('&'); 
                        for( var i = q.length - 1; i >= 0; i-- ) {
                                var p = q[i].split('='), key = p[0], val = p[1];
                                before_type_cast[key] = val;
                                // convert floats
                                if(/^[0-9.]+$/.test(val))
                                        val = parseFloat(val);
                                // convert booleans
                                if(/^(true|false)$/.test(val))
                                        val = (val == 'true');
                                // ingnore empty values
                                if(val)
                                        after_type_cast[key] = val;
                        }
                        cached = true;
                }
                return cast === false ? before_type_cast : after_type_cast;
        };

})();

I haven't tested the above, and it can probably be condensed/simplified 
a bit, but you get the idea ...

So, for http://www.example.com/test?special=&zip=00125, pass false to 
$.query() to get the raw values ...

$.query().zip -> 125 (number)
$.query(false).zip -> 00125 (string)

$.query().hasOwnProperty('special') -> false
$.query(false).hasOwnProperty('special') -> true

Luke

Michael Geary wrote:
> In Jörn's version of the code, you wouldn't call $.query() at all. Instead,
> $.query would be the resulting object that you would then use directly. Note
> that jQuery and $ are references to the same object, so jQuery.query and
> $.query are the same thing.
> 
> I'm not sure I like this idea since it means the query parsing would be done
> whether you need it or not. But maybe it doesn't take enough time to worry
> about. If it does run all the time, it would be worth optimizing the code by
> using a conventional for loop instead of jQuery.each and combining the
> repeated "split" calls.
> 
> I'd also remove the code that does the float conversion and ignores empty
> values. Consider this URL:
> 
> http://www.example.com/test?special=&zip=00125
> 
> I may want to know about that "special" parameter, and I probably don't want
> the Zip code turned into the number 125.
> 
> Here's how I would code it...
> 
> For the "jQuery.query is an object" version:
> 
>    (function() {
>       var r = jQuery.query = {};
>       var params = location.search.replace(/^\?/,'').split('&');
>       for( var i = params.length-1;  i >= 0;  i-- ) {
>          var p = params[i].split('='), key = p[0];
>          if( key ) r[key] = p[1];
>       }
>    })();
> 
> For the "jQuery.query is a function":
> 
>    jQuery.query = function() {
>       var r = {};
>       var params = location.search.replace(/^\?/,'').split('&');
>       for( var i = params.length-1;  i >= 0;  i-- ) {
>          var p = params[i].split('='), key = p[0];
>          if( key ) r[key] = p[1];
>       }
>       return r;
>    };
> 
> All untested, of course. :-)
> 
> -Mike



_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to