Javascript String toQueryParams(separator)

Description

Javascript String toQueryParams(separator)


String.prototype.toQueryParams = function(separator) {
 var match = this.strip().match(/([^?#]*)(#.*)?$/);
 if (!match) return { };

 return match[1].split(separator || '&').inject({ }, function(hash, pair) {
  if ((pair = pair.split('='))[0]) {
   var key = decodeURIComponent(pair.shift());
   var value = pair.length > 1 ? pair.join('=') : pair[0];
   if (value != undefined) value = decodeURIComponent(value);

   if (key in hash) {
    if (!Object.isArray(hash[key])) hash[key] = [hash[key]];
    hash[key].push(value);/*from  w  w w  . j  a v  a 2  s. co  m*/
   }
   else hash[key] = value;
  }
  return hash;
 });
};



PreviousNext

Related