-
Notifications
You must be signed in to change notification settings - Fork 1
/
analysis-str.js
65 lines (61 loc) · 1.6 KB
/
analysis-str.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
// 字符串解析,要求
// 相同key的多个值用数组封装
// 如果能用JSON解析则作为json对象
// 没有value,赋值false的boolean值
// base64编码的,需要转换成字符串
// input: name=adam&name=bob&obj={a:1,b:2}&use&encodeStr=%20
// output:
// {
// name: ['adam', 'bob'],
// obj: {a:1, b:2},
// use: false,
// encodeStr: ' '
// }
function analysisStr(str) {
const list = str.split('&');
const re = {};
for (const item of list) {
const [key, value] = item.split('=');
//相同的key,转成数组
if (re[key]) {
if (re[key] instanceof Array) {
re[key].push(value);
} else {
re[key] = [re[key], value];
}
} else if (!value) {
//没有value,赋值false的boolean值
re[key] = false;
} else if (value[0] === '{' && value[value.length-1] === '}') {
// 如果能用JSON解析则作为json对象
const arr = value.substring(1, value.length - 1).split(',')
const obj = {}
for(const item of arr) {
const [key,value] = item.split(':')
obj[key] = value
}
re[key] = obj
} else if (isBase64(value)) {
// base64编码的,需要转换成字符串
re[key] = decodeURI(value);
} else {
re[key] = value;
}
}
return re;
}
function isJsonString(str) {
try {
return typeof JSON.parse(str) === 'object';
} catch (e) {
return false;
}
}
function isBase64(str) {
try {
return encodeURI(decodeURI(str)) === str;
} catch (e) {
return false;
}
}
console.log(analysisStr('name=adam&name=bob&obj={a:1,b:2}&use&encodeStr=%20'));