|
|
// Load modules
var Utils = require('./utils');
// Declare internals
var internals = { delimiter: '&', indices: true };
internals.stringify = function (obj, prefix, options) {
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 = [];
if (typeof obj === 'undefined') { return values; }
var objKeys = Object.keys(obj); for (var i = 0, il = objKeys.length; i < il; ++i) { var key = objKeys[i]; if (!options.indices && Array.isArray(obj)) {
values = values.concat(internals.stringify(obj[key], prefix, options)); } else { values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']', options)); } }
return values; };
module.exports = function (obj, options) {
options = options || {}; var delimiter = typeof options.delimiter === 'undefined' ? internals.delimiter : options.delimiter; options.indices = typeof options.indices === 'boolean' ? options.indices : internals.indices;
var keys = [];
if (typeof obj !== 'object' || obj === null) {
return ''; }
var objKeys = Object.keys(obj); for (var i = 0, il = objKeys.length; i < il; ++i) { var key = objKeys[i]; keys = keys.concat(internals.stringify(obj[key], key, options)); }
return keys.join(delimiter); };
|