Is this query-string parser bug-free?
Is it bug free?  No.

These two corner-cases have been missed:


parameter values containing '=', i.e. 'example.com?foo==bar' (double equals) or '?foo=k=v'
cannot handle parameters called 'toString' and 'valueOf' (amongst others.)


The first may well count as malformed URL, but Chrome handles it and pass through == unencoded in location.search.  To handle this, go back to basic indexOf usage.

The second problem's just pedantic really.  You could try and work around it using !params.hasOwnProperty(n) instead of !(n in params), but you'll still get stuck if someone passes a parameter called hasOwnProperty.  The only way I see around this is to fall back to some dire array-based collection populated something like:

var keys = [], params = [];
for (...) {
    var n = ..., v = ...;
    var i = keys.indexOf(n);
    if (i &gt;= 0) {
        if (!(params[i] instanceof Array)) {
            params[i] = [params[i]];
        }
        params.push(v);
    } else {
        params[i] = v;
        keys.push(n);
    }
}


I guess you'd then have to resort to returning an array of arrays rather than an object.  i.e. each element of the array returned would either be [key, value] or [key, [values]], although client might find it easier to work with if you returned something like [key, value1, value2, ...] (which caters nicely for properties without values.)