1 | var typecast = require('../string/typecast');
|
2 | var isArray = require('../lang/isArray');
|
3 | var hasOwn = require('../object/hasOwn');
|
4 |
|
5 | |
6 |
|
7 |
|
8 | function decode(queryStr, shouldTypecast) {
|
9 | var queryArr = (queryStr || '').replace('?', '').split('&'),
|
10 | reg = /([^=]+)=(.+)/,
|
11 | i = -1,
|
12 | obj = {},
|
13 | equalIndex, cur, pValue, pName;
|
14 |
|
15 | while ((cur = queryArr[++i])) {
|
16 | equalIndex = cur.indexOf('=');
|
17 | pName = cur.substring(0, equalIndex);
|
18 | pValue = decodeURIComponent(cur.substring(equalIndex + 1));
|
19 | if (shouldTypecast !== false) {
|
20 | pValue = typecast(pValue);
|
21 | }
|
22 | if (hasOwn(obj, pName)){
|
23 | if(isArray(obj[pName])){
|
24 | obj[pName].push(pValue);
|
25 | } else {
|
26 | obj[pName] = [obj[pName], pValue];
|
27 | }
|
28 | } else {
|
29 | obj[pName] = pValue;
|
30 | }
|
31 | }
|
32 | return obj;
|
33 | }
|
34 |
|
35 | module.exports = decode;
|
36 |
|