1
|
'use strict';
|
2
|
|
3
|
var utils = require('./utils');
|
4
|
|
5
|
var has = Object.prototype.hasOwnProperty;
|
6
|
|
7
|
var defaults = {
|
8
|
allowDots: false,
|
9
|
allowPrototypes: false,
|
10
|
arrayLimit: 20,
|
11
|
charset: 'utf-8',
|
12
|
charsetSentinel: false,
|
13
|
comma: false,
|
14
|
decoder: utils.decode,
|
15
|
delimiter: '&',
|
16
|
depth: 5,
|
17
|
ignoreQueryPrefix: false,
|
18
|
interpretNumericEntities: false,
|
19
|
parameterLimit: 1000,
|
20
|
parseArrays: true,
|
21
|
plainObjects: false,
|
22
|
strictNullHandling: false
|
23
|
};
|
24
|
|
25
|
var interpretNumericEntities = function (str) {
|
26
|
return str.replace(/&#(\d+);/g, function ($0, numberStr) {
|
27
|
return String.fromCharCode(parseInt(numberStr, 10));
|
28
|
});
|
29
|
};
|
30
|
|
31
|
// This is what browsers will submit when the ✓ character occurs in an
|
32
|
// application/x-www-form-urlencoded body and the encoding of the page containing
|
33
|
// the form is iso-8859-1, or when the submitted form has an accept-charset
|
34
|
// attribute of iso-8859-1. Presumably also with other charsets that do not contain
|
35
|
// the ✓ character, such as us-ascii.
|
36
|
var isoSentinel = 'utf8=%26%2310003%3B'; // encodeURIComponent('✓')
|
37
|
|
38
|
// These are the percent-encoded utf-8 octets representing a checkmark, indicating that the request actually is utf-8 encoded.
|
39
|
var charsetSentinel = 'utf8=%E2%9C%93'; // encodeURIComponent('✓')
|
40
|
|
41
|
var parseValues = function parseQueryStringValues(str, options) {
|
42
|
var obj = {};
|
43
|
var cleanStr = options.ignoreQueryPrefix ? str.replace(/^\?/, '') : str;
|
44
|
var limit = options.parameterLimit === Infinity ? undefined : options.parameterLimit;
|
45
|
var parts = cleanStr.split(options.delimiter, limit);
|
46
|
var skipIndex = -1; // Keep track of where the utf8 sentinel was found
|
47
|
var i;
|
48
|
|
49
|
var charset = options.charset;
|
50
|
if (options.charsetSentinel) {
|
51
|
for (i = 0; i < parts.length; ++i) {
|
52
|
if (parts[i].indexOf('utf8=') === 0) {
|
53
|
if (parts[i] === charsetSentinel) {
|
54
|
charset = 'utf-8';
|
55
|
} else if (parts[i] === isoSentinel) {
|
56
|
charset = 'iso-8859-1';
|
57
|
}
|
58
|
skipIndex = i;
|
59
|
i = parts.length; // The eslint settings do not allow break;
|
60
|
}
|
61
|
}
|
62
|
}
|
63
|
|
64
|
for (i = 0; i < parts.length; ++i) {
|
65
|
if (i === skipIndex) {
|
66
|
continue;
|
67
|
}
|
68
|
var part = parts[i];
|
69
|
|
70
|
var bracketEqualsPos = part.indexOf(']=');
|
71
|
var pos = bracketEqualsPos === -1 ? part.indexOf('=') : bracketEqualsPos + 1;
|
72
|
|
73
|
var key, val;
|
74
|
if (pos === -1) {
|
75
|
key = options.decoder(part, defaults.decoder, charset);
|
76
|
val = options.strictNullHandling ? null : '';
|
77
|
} else {
|
78
|
key = options.decoder(part.slice(0, pos), defaults.decoder, charset);
|
79
|
val = options.decoder(part.slice(pos + 1), defaults.decoder, charset);
|
80
|
}
|
81
|
|
82
|
if (val && options.interpretNumericEntities && charset === 'iso-8859-1') {
|
83
|
val = interpretNumericEntities(val);
|
84
|
}
|
85
|
|
86
|
if (val && options.comma && val.indexOf(',') > -1) {
|
87
|
val = val.split(',');
|
88
|
}
|
89
|
|
90
|
if (has.call(obj, key)) {
|
91
|
obj[key] = utils.combine(obj[key], val);
|
92
|
} else {
|
93
|
obj[key] = val;
|
94
|
}
|
95
|
}
|
96
|
|
97
|
return obj;
|
98
|
};
|
99
|
|
100
|
var parseObject = function (chain, val, options) {
|
101
|
var leaf = val;
|
102
|
|
103
|
for (var i = chain.length - 1; i >= 0; --i) {
|
104
|
var obj;
|
105
|
var root = chain[i];
|
106
|
|
107
|
if (root === '[]' && options.parseArrays) {
|
108
|
obj = [].concat(leaf);
|
109
|
} else {
|
110
|
obj = options.plainObjects ? Object.create(null) : {};
|
111
|
var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
|
112
|
var index = parseInt(cleanRoot, 10);
|
113
|
if (!options.parseArrays && cleanRoot === '') {
|
114
|
obj = { 0: leaf };
|
115
|
} else if (
|
116
|
!isNaN(index)
|
117
|
&& root !== cleanRoot
|
118
|
&& String(index) === cleanRoot
|
119
|
&& index >= 0
|
120
|
&& (options.parseArrays && index <= options.arrayLimit)
|
121
|
) {
|
122
|
obj = [];
|
123
|
obj[index] = leaf;
|
124
|
} else {
|
125
|
obj[cleanRoot] = leaf;
|
126
|
}
|
127
|
}
|
128
|
|
129
|
leaf = obj;
|
130
|
}
|
131
|
|
132
|
return leaf;
|
133
|
};
|
134
|
|
135
|
var parseKeys = function parseQueryStringKeys(givenKey, val, options) {
|
136
|
if (!givenKey) {
|
137
|
return;
|
138
|
}
|
139
|
|
140
|
// Transform dot notation to bracket notation
|
141
|
var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
|
142
|
|
143
|
// The regex chunks
|
144
|
|
145
|
var brackets = /(\[[^[\]]*])/;
|
146
|
var child = /(\[[^[\]]*])/g;
|
147
|
|
148
|
// Get the parent
|
149
|
|
150
|
var segment = brackets.exec(key);
|
151
|
var parent = segment ? key.slice(0, segment.index) : key;
|
152
|
|
153
|
// Stash the parent if it exists
|
154
|
|
155
|
var keys = [];
|
156
|
if (parent) {
|
157
|
// If we aren't using plain objects, optionally prefix keys that would overwrite object prototype properties
|
158
|
if (!options.plainObjects && has.call(Object.prototype, parent)) {
|
159
|
if (!options.allowPrototypes) {
|
160
|
return;
|
161
|
}
|
162
|
}
|
163
|
|
164
|
keys.push(parent);
|
165
|
}
|
166
|
|
167
|
// Loop through children appending to the array until we hit depth
|
168
|
|
169
|
var i = 0;
|
170
|
while ((segment = child.exec(key)) !== null && i < options.depth) {
|
171
|
i += 1;
|
172
|
if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
|
173
|
if (!options.allowPrototypes) {
|
174
|
return;
|
175
|
}
|
176
|
}
|
177
|
keys.push(segment[1]);
|
178
|
}
|
179
|
|
180
|
// If there's a remainder, just add whatever is left
|
181
|
|
182
|
if (segment) {
|
183
|
keys.push('[' + key.slice(segment.index) + ']');
|
184
|
}
|
185
|
|
186
|
return parseObject(keys, val, options);
|
187
|
};
|
188
|
|
189
|
var normalizeParseOptions = function normalizeParseOptions(opts) {
|
190
|
if (!opts) {
|
191
|
return defaults;
|
192
|
}
|
193
|
|
194
|
if (opts.decoder !== null && opts.decoder !== undefined && typeof opts.decoder !== 'function') {
|
195
|
throw new TypeError('Decoder has to be a function.');
|
196
|
}
|
197
|
|
198
|
if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
|
199
|
throw new Error('The charset option must be either utf-8, iso-8859-1, or undefined');
|
200
|
}
|
201
|
var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset;
|
202
|
|
203
|
return {
|
204
|
allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots,
|
205
|
allowPrototypes: typeof opts.allowPrototypes === 'boolean' ? opts.allowPrototypes : defaults.allowPrototypes,
|
206
|
arrayLimit: typeof opts.arrayLimit === 'number' ? opts.arrayLimit : defaults.arrayLimit,
|
207
|
charset: charset,
|
208
|
charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
|
209
|
comma: typeof opts.comma === 'boolean' ? opts.comma : defaults.comma,
|
210
|
decoder: typeof opts.decoder === 'function' ? opts.decoder : defaults.decoder,
|
211
|
delimiter: typeof opts.delimiter === 'string' || utils.isRegExp(opts.delimiter) ? opts.delimiter : defaults.delimiter,
|
212
|
depth: typeof opts.depth === 'number' ? opts.depth : defaults.depth,
|
213
|
ignoreQueryPrefix: opts.ignoreQueryPrefix === true,
|
214
|
interpretNumericEntities: typeof opts.interpretNumericEntities === 'boolean' ? opts.interpretNumericEntities : defaults.interpretNumericEntities,
|
215
|
parameterLimit: typeof opts.parameterLimit === 'number' ? opts.parameterLimit : defaults.parameterLimit,
|
216
|
parseArrays: opts.parseArrays !== false,
|
217
|
plainObjects: typeof opts.plainObjects === 'boolean' ? opts.plainObjects : defaults.plainObjects,
|
218
|
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling
|
219
|
};
|
220
|
};
|
221
|
|
222
|
module.exports = function (str, opts) {
|
223
|
var options = normalizeParseOptions(opts);
|
224
|
|
225
|
if (str === '' || str === null || typeof str === 'undefined') {
|
226
|
return options.plainObjects ? Object.create(null) : {};
|
227
|
}
|
228
|
|
229
|
var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
|
230
|
var obj = options.plainObjects ? Object.create(null) : {};
|
231
|
|
232
|
// Iterate over the keys and setup the new object
|
233
|
|
234
|
var keys = Object.keys(tempObj);
|
235
|
for (var i = 0; i < keys.length; ++i) {
|
236
|
var key = keys[i];
|
237
|
var newObj = parseKeys(key, tempObj[key], options);
|
238
|
obj = utils.merge(obj, newObj, options);
|
239
|
}
|
240
|
|
241
|
return utils.compact(obj);
|
242
|
};
|