Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / request / node_modules / qs / lib / parse.js
1 // Load modules
2
3 var Utils = require('./utils');
4
5
6 // Declare internals
7
8 var internals = {
9     delimiter: '&',
10     depth: 5,
11     arrayLimit: 20,
12     parameterLimit: 1000,
13     strictNullHandling: false,
14     plainObjects: false,
15     allowPrototypes: false,
16     allowDots: false
17 };
18
19
20 internals.parseValues = function (str, options) {
21
22     var obj = {};
23     var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);
24
25     for (var i = 0, il = parts.length; i < il; ++i) {
26         var part = parts[i];
27         var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1;
28
29         var key, val;
30         if (pos === -1) {
31             key = Utils.decode(part);
32             val = options.strictNullHandling ? null : '';
33         } else {
34             key = Utils.decode(part.slice(0, pos));
35             val = Utils.decode(part.slice(pos + 1));
36         }
37         if (Object.prototype.hasOwnProperty.call(obj, key)) {
38             obj[key] = [].concat(obj[key]).concat(val);
39         } else {
40             obj[key] = val;
41         }
42     }
43
44     return obj;
45 };
46
47
48 internals.parseObject = function (chain, val, options) {
49
50     if (!chain.length) {
51         return val;
52     }
53
54     var root = chain.shift();
55
56     var obj;
57     if (root === '[]') {
58         obj = [];
59         obj = obj.concat(internals.parseObject(chain, val, options));
60     }
61     else {
62         obj = options.plainObjects ? Object.create(null) : {};
63         var cleanRoot = root[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 1) : root;
64         var index = parseInt(cleanRoot, 10);
65         var indexString = '' + index;
66         if (!isNaN(index) &&
67             root !== cleanRoot &&
68             indexString === cleanRoot &&
69             index >= 0 &&
70             (options.parseArrays &&
71              index <= options.arrayLimit)) {
72
73             obj = [];
74             obj[index] = internals.parseObject(chain, val, options);
75         }
76         else {
77             obj[cleanRoot] = internals.parseObject(chain, val, options);
78         }
79     }
80
81     return obj;
82 };
83
84
85 internals.parseKeys = function (key, val, options) {
86
87     if (!key) {
88         return;
89     }
90
91     // Transform dot notation to bracket notation
92
93     if (options.allowDots) {
94         key = key.replace(/\.([^\.\[]+)/g, '[$1]');
95     }
96
97     // The regex chunks
98
99     var parent = /^([^\[\]]*)/;
100     var child = /(\[[^\[\]]*\])/g;
101
102     // Get the parent
103
104     var segment = parent.exec(key);
105
106     // Stash the parent if it exists
107
108     var keys = [];
109     if (segment[1]) {
110         // If we aren't using plain objects, optionally prefix keys
111         // that would overwrite object prototype properties
112         if (!options.plainObjects &&
113             Object.prototype.hasOwnProperty(segment[1])) {
114
115             if (!options.allowPrototypes) {
116                 return;
117             }
118         }
119
120         keys.push(segment[1]);
121     }
122
123     // Loop through children appending to the array until we hit depth
124
125     var i = 0;
126     while ((segment = child.exec(key)) !== null && i < options.depth) {
127
128         ++i;
129         if (!options.plainObjects &&
130             Object.prototype.hasOwnProperty(segment[1].replace(/\[|\]/g, ''))) {
131
132             if (!options.allowPrototypes) {
133                 continue;
134             }
135         }
136         keys.push(segment[1]);
137     }
138
139     // If there's a remainder, just add whatever is left
140
141     if (segment) {
142         keys.push('[' + key.slice(segment.index) + ']');
143     }
144
145     return internals.parseObject(keys, val, options);
146 };
147
148
149 module.exports = function (str, options) {
150
151     options = options || {};
152     options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter;
153     options.depth = typeof options.depth === 'number' ? options.depth : internals.depth;
154     options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : internals.arrayLimit;
155     options.parseArrays = options.parseArrays !== false;
156     options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : internals.allowDots;
157     options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : internals.plainObjects;
158     options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : internals.allowPrototypes;
159     options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit;
160     options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling;
161
162     if (str === '' ||
163         str === null ||
164         typeof str === 'undefined') {
165
166         return options.plainObjects ? Object.create(null) : {};
167     }
168
169     var tempObj = typeof str === 'string' ? internals.parseValues(str, options) : str;
170     var obj = options.plainObjects ? Object.create(null) : {};
171
172     // Iterate over the keys and setup the new object
173
174     var keys = Object.keys(tempObj);
175     for (var i = 0, il = keys.length; i < il; ++i) {
176         var key = keys[i];
177         var newObj = internals.parseKeys(key, tempObj[key], options);
178         obj = Utils.merge(obj, newObj, options);
179     }
180
181     return Utils.compact(obj);
182 };