I'm working on an isomorphic javascript module (query-hash) to handle query strings and base64 tokens. It's essentially a simple key-value object with methods for taking in data and giving it back in the format you ask for.
function queryToObject(queryString) {
return queryString.split('&')
.map(kv => kv.split('='))
.reduce((p, kv) => {
p[kv[0]] = decodeURIComponent(kv[1] || '').replace(/\+/g, ' ');
return p;
}, {});
}
For the "functional approach", mapping the array created by split()
seems like unnecessary function calls. Should I be splitting the string in the reduce()
method? Is there a better approach?