-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.js
86 lines (71 loc) · 1.64 KB
/
query.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* query.js is a query string conversion tool
*/
let getPureQuery = () => {
let str = location.search
return str.length ? str.slice(1) : ''
}
let isPrimitive = (val) => {
return val === null || typeof val !== 'object'
}
let basicValue2String = (val) => {
switch (typeof val) {
case 'string':
return val
break
case 'number':
return Number(val)
break
case 'boolean':
return val ? 'true' : 'false'
break
default:
return ''
}
}
// convert str to obj
let decode = () => {
let str = getPureQuery()
let obj = {}
if (!str.length) {
return obj
}
let pairs = str.split('&')
for (let i = 0; i < pairs.length; i++) {
let kv = pairs[i].split('=')
let k = decodeURIComponent(kv[0])
let v = decodeURIComponent(kv[1])
obj[k] = v
}
return obj
}
// convert obj to str
let encode = (obj) => {
if (isPrimitive(obj) || Array.isArray(obj)) {
throw new TypeError('the parameter should be an object')
}
return Object.keys(obj).map((key) => {
let head = encodeURIComponent(basicValue2String(key)) + '='
if (isPrimitive(obj[key])) {
return head + encodeURIComponent(basicValue2String(obj[key]))
}
throw new Error('the query string must not contain object value')
}).join('&')
}
// get value by key
let extract = (key) => {
if (!isPrimitive(key)) {
throw new TypeError('the parameter should not be reference type')
}
let obj = decode(getPureQuery())
let val = obj[basicValue2String(key)]
return val ? String(val) : ''
}
export default {
decode,
encode,
extract,
parse: decode,
stringify: encode,
getParaValue: extract,
}