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