function parse(str) {
return str.split('&').reduce((o, kv) => {
const [key, value] = kv.split('=')
if (!value) {
return o
}
deep_set(o, key, value)
return o
}, {})
}
function deep_set(o, key, value) {
const path = key.split(/[\[\]]/g).filter(x => x)
let i = 0
for (; i < path.length-1; i++) {
if (o[path[i]] === undefined) {
if (path[i+1].match(/^\d+$/)) {
o[path[i]] = []
} else {
o[path[i]] = {}
}
}
o = o[path[i]]
}
o[path[i]] = decodeURIComponent(value)
}
console.log(parse('a=1&b=2&f=hello'))
console.log(parse('a&b=&f=hello'))
console.log(parse('a[name][short]=fox&a[com]=ali&b=yy'))
console.log(parse('a[0]=fox&a[1]=ali'))
console.log(parse('color=blue%20sky'))