Files
remark42/frontend/app/utils/parseQuery.ts
T
2019-07-15 17:57:43 -05:00

21 lines
559 B
TypeScript

/** converts widnow.location.search into object */
export function parseQuery(search: string): { [key: string]: string } {
if (search.length < 2) return {};
return search
.substr(1)
.split('&')
.map((chunk): [string, string] => {
const parts = chunk.split('=');
if (parts.length < 2) {
parts[1] = '';
} else {
parts[1] = decodeURIComponent(parts[1]);
}
return parts as [string, string];
})
.reduce<{ [key: string]: string }>((c, x) => {
c[x[0]] = x[1];
return c;
}, {});
}