You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.1 KiB
58 lines
1.1 KiB
// Load modules
|
|
|
|
var Utils = require('./utils');
|
|
|
|
|
|
// Declare internals
|
|
|
|
var internals = {
|
|
delimiter: '&'
|
|
};
|
|
|
|
|
|
internals.stringify = function (obj, prefix) {
|
|
|
|
if (Utils.isBuffer(obj)) {
|
|
obj = obj.toString();
|
|
}
|
|
else if (obj instanceof Date) {
|
|
obj = obj.toISOString();
|
|
}
|
|
else if (obj === null) {
|
|
obj = '';
|
|
}
|
|
|
|
if (typeof obj === 'string' ||
|
|
typeof obj === 'number' ||
|
|
typeof obj === 'boolean') {
|
|
|
|
return [encodeURIComponent(prefix) + '=' + encodeURIComponent(obj)];
|
|
}
|
|
|
|
var values = [];
|
|
|
|
for (var key in obj) {
|
|
if (obj.hasOwnProperty(key)) {
|
|
values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']'));
|
|
}
|
|
}
|
|
|
|
return values;
|
|
};
|
|
|
|
|
|
module.exports = function (obj, options) {
|
|
|
|
options = options || {};
|
|
var delimiter = typeof options.delimiter === 'undefined' ? internals.delimiter : options.delimiter;
|
|
|
|
var keys = [];
|
|
|
|
for (var key in obj) {
|
|
if (obj.hasOwnProperty(key)) {
|
|
keys = keys.concat(internals.stringify(obj[key], key));
|
|
}
|
|
}
|
|
|
|
return keys.join(delimiter);
|
|
};
|