-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
33 lines (29 loc) · 833 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* eslint-disable no-var */
'use strict'
var has = Object.prototype.hasOwnProperty
/**
* Stringify an object for use in a query string.
*
* @param {Object} obj - The object.
* @param {string} prefix - When nesting, the parent key.
* keys in `obj` will be stringified as `prefix[key]`.
* @returns {string}
*/
module.exports = function queryStringify (obj, prefix) {
var pairs = []
for (var key in obj) {
if (!has.call(obj, key)) {
continue
}
var value = obj[key]
var enkey = encodeURIComponent(key)
var pair
if (typeof value === 'object') {
pair = queryStringify(value, prefix ? prefix + '[' + enkey + ']' : enkey)
} else {
pair = (prefix ? prefix + '[' + enkey + ']' : enkey) + '=' + encodeURIComponent(value)
}
pairs.push(pair)
}
return pairs.join('&')
}