1c15785bfbdb8b8bac5fc55c8864c2cb42e686f2
[platform/framework/web/crosswalk-tizen.git] /
1 var typecast = require('../string/typecast');
2 var isString = require('../lang/isString');
3 var isArray = require('../lang/isArray');
4 var hasOwn = require('../object/hasOwn');
5
6     /**
7      * Decode query string into an object of keys => vals.
8      */
9     function decode(queryStr, shouldTypecast) {
10         var queryArr = (queryStr || '').replace('?', '').split('&'),
11             count = -1,
12             length = queryArr.length,
13             obj = {},
14             item, pValue, pName, toSet;
15
16         while (++count < length) {
17             item = queryArr[count].split('=');
18             pName = item[0];
19             if (!pName || !pName.length){
20                 continue;
21             }
22             pValue = shouldTypecast === false ? item[1] : typecast(item[1]);
23             toSet = isString(pValue) ? decodeURIComponent(pValue) : pValue;
24             if (hasOwn(obj,pName)){
25                 if(isArray(obj[pName])){
26                     obj[pName].push(toSet);
27                 } else {
28                     obj[pName] = [obj[pName],toSet];
29                 }
30             } else {
31                 obj[pName] = toSet;
32            }
33         }
34         return obj;
35     }
36
37     module.exports = decode;
38