-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathparseQuery.ts
More file actions
24 lines (22 loc) · 801 Bytes
/
parseQuery.ts
File metadata and controls
24 lines (22 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
*
* @example
* parseQuery("host=open.rocket.chat&path=channel/general/thread/meRK2nfjR99MjLn55")
* // the return will be
* {
* host: "open.rocket.chat",
* path: "channel/general/thread/meRK2nfjR99MjLn55"
* }
*/
export default function (query: string) {
if (query.startsWith('url=')) {
return { text: decodeURIComponent(query.replace('url=', '').trim()) };
}
return (/^[?#]/.test(query) ? query.slice(1) : query).split('&').reduce((params: { [key: string]: string }, param) => {
const separatorIndex = param.indexOf('=');
const key = separatorIndex >= 0 ? param.slice(0, separatorIndex) : param;
const value = separatorIndex >= 0 ? param.slice(separatorIndex + 1) : '';
params[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : '';
return params;
}, {});
}