Skip to content

Instantly share code, notes, and snippets.

@nenjiru
Last active September 23, 2015 22:08
Show Gist options
  • Save nenjiru/623468 to your computer and use it in GitHub Desktop.
Save nenjiru/623468 to your computer and use it in GitHub Desktop.
クエリーストリングをハッシュに変換
/**
* Query string convert to hash.
*
* @param {String} string ?を含んだ文字列
* @return {Object}
*
* @example
* toObject("index.html?width=500&height=auto");
* {width:500, height:auto}
*/
function toobject(string)
{
var params,
i = 0,
q,
kv,
result = {};
string = string.replace(/^\?/, '');
params = string.split('&');
for (;q = params[i++];) {
kv = q.split('=');
//ブレース[]含んでたら配列にする
if (decodeuricomponent(kv[0]).match(/\[\]/))
{
if (!result[kv[0]])
{
result[kv[0]] = [];
}
result[kv[0]].push(decodeuricomponent(kv[1]));
}
else
{
result[kv[0]] = decodeuricomponent(kv[1]);
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment