Skip to content

Commit

Permalink
Add arrayFormat option
Browse files Browse the repository at this point in the history
This commit adds the arrayFormat option to qs.stringify. Valid
values are "indices" (the default), "repeat", or "brackets".

Backwards compat is preserved with the indices option as well.
  • Loading branch information
mjackson committed Feb 18, 2015
1 parent 9250c4c commit 9c46a95
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
38 changes: 29 additions & 9 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ var Utils = require('./utils');

var internals = {
delimiter: '&',
indices: true
arrayPrefixGenerators: {
brackets: function (prefix, key) {
return prefix + '[]';
},
indices: function (prefix, key) {
return prefix + '[' + key + ']';
},
repeat: function (prefix, key) {
return prefix;
}
}
};


internals.stringify = function (obj, prefix, options) {
internals.stringify = function (obj, prefix, generateArrayPrefix) {

if (Utils.isBuffer(obj)) {
obj = obj.toString();
Expand Down Expand Up @@ -39,13 +49,11 @@ internals.stringify = function (obj, prefix, options) {
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));
if (Array.isArray(obj)) {
values = values.concat(internals.stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix));
}
else {
values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']', options));
values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']', generateArrayPrefix));
}
}

Expand All @@ -57,7 +65,6 @@ 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 = [];

Expand All @@ -67,10 +74,23 @@ module.exports = function (obj, options) {
return '';
}

var arrayFormat;
if (options.arrayFormat in internals.arrayPrefixGenerators) {
arrayFormat = options.arrayFormat;
}
else if ('indices' in options) {
arrayFormat = options.indices ? 'indices' : 'repeat';
}
else {
arrayFormat = 'indices';
}

var generateArrayPrefix = internals.arrayPrefixGenerators[arrayFormat];

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));
keys = keys.concat(internals.stringify(obj[key], key, generateArrayPrefix));
}

return keys.join(delimiter);
Expand Down
30 changes: 30 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,36 @@ describe('stringify()', function () {
done();
});

it('uses indices notation for arrays when indices=true', function (done) {

expect(Qs.stringify({ a: ['b', 'c'] }, { indices: true })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
done();
});

it('uses indices notation for arrays when no arrayFormat is specified', function (done) {

expect(Qs.stringify({ a: ['b', 'c'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
done();
});

it('uses indices notation for arrays when no arrayFormat=indices', function (done) {

expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })).to.equal('a%5B0%5D=b&a%5B1%5D=c');
done();
});

it('uses repeat notation for arrays when no arrayFormat=repeat', function (done) {

expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })).to.equal('a=b&a=c');
done();
});

it('uses brackets notation for arrays when no arrayFormat=brackets', function (done) {

expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })).to.equal('a%5B%5D=b&a%5B%5D=c');
done();
});

it('stringifies a complicated object', function (done) {

expect(Qs.stringify({ a: { b: 'c', d: 'e' } })).to.equal('a%5Bb%5D=c&a%5Bd%5D=e');
Expand Down

0 comments on commit 9c46a95

Please sign in to comment.