forked from dmonad/lib0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.js
37 lines (34 loc) · 875 Bytes
/
url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Utility module to work with urls.
*
* @module url
*/
import * as object from './object.js'
/**
* Parse query parameters from an url.
*
* @param {string} url
* @return {Object<string,string>}
*/
export const decodeQueryParams = url => {
/**
* @type {Object<string,string>}
*/
const query = {}
const urlQuerySplit = url.split('?')
const pairs = urlQuerySplit[urlQuerySplit.length - 1].split('&')
for (var i = 0; i < pairs.length; i++) {
const item = pairs[i]
if (item.length > 0) {
const pair = item.split('=')
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '')
}
}
return query
}
/**
* @param {Object<string,string>} params
* @return {string}
*/
export const encodeQueryParams = params =>
object.map(params, (val, key) => `${encodeURIComponent(key)}=${encodeURIComponent(val)}`).join('&')