Skip to content

Commit 0311603

Browse files
committed
Add arrayFormat option
This commit removes the indices option in favor of the arrayFormat option. arrayFormat may be either "indices", "repeat", or "brackets". It defaults to indices.
1 parent 9250c4c commit 0311603

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

lib/stringify.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ var Utils = require('./utils');
77

88
var internals = {
99
delimiter: '&',
10-
indices: true
10+
arrayPrefixGenerators: {
11+
brackets: function (prefix, key) {
12+
return prefix + '[]';
13+
},
14+
indices: function (prefix, key) {
15+
return prefix + '[' + key + ']';
16+
},
17+
repeat: function (prefix, key) {
18+
return prefix;
19+
}
20+
}
1121
};
1222

1323

14-
internals.stringify = function (obj, prefix, options) {
24+
internals.stringify = function (obj, prefix, generateArrayPrefix) {
1525

1626
if (Utils.isBuffer(obj)) {
1727
obj = obj.toString();
@@ -39,13 +49,11 @@ internals.stringify = function (obj, prefix, options) {
3949
var objKeys = Object.keys(obj);
4050
for (var i = 0, il = objKeys.length; i < il; ++i) {
4151
var key = objKeys[i];
42-
if (!options.indices &&
43-
Array.isArray(obj)) {
44-
45-
values = values.concat(internals.stringify(obj[key], prefix, options));
52+
if (Array.isArray(obj)) {
53+
values = values.concat(internals.stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix));
4654
}
4755
else {
48-
values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']', options));
56+
values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']', generateArrayPrefix));
4957
}
5058
}
5159

@@ -57,7 +65,6 @@ module.exports = function (obj, options) {
5765

5866
options = options || {};
5967
var delimiter = typeof options.delimiter === 'undefined' ? internals.delimiter : options.delimiter;
60-
options.indices = typeof options.indices === 'boolean' ? options.indices : internals.indices;
6168

6269
var keys = [];
6370

@@ -67,10 +74,18 @@ module.exports = function (obj, options) {
6774
return '';
6875
}
6976

77+
var generateArrayPrefix;
78+
if (options.arrayFormat in internals.arrayPrefixGenerators) {
79+
generateArrayPrefix = internals.arrayPrefixGenerators[options.arrayFormat];
80+
}
81+
else {
82+
generateArrayPrefix = internals.arrayPrefixGenerators.indices;
83+
}
84+
7085
var objKeys = Object.keys(obj);
7186
for (var i = 0, il = objKeys.length; i < il; ++i) {
7287
var key = objKeys[i];
73-
keys = keys.concat(internals.stringify(obj[key], key, options));
88+
keys = keys.concat(internals.stringify(obj[key], key, generateArrayPrefix));
7489
}
7590

7691
return keys.join(delimiter);

test/stringify.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ describe('stringify()', function () {
4242
done();
4343
});
4444

45-
it('omits array indices when asked', function (done) {
45+
it('omits array indices when arrayFormat=repeat', function (done) {
4646

47-
expect(Qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false })).to.equal('a=b&a=c&a=d');
47+
expect(Qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'repeat' })).to.equal('a=b&a=c&a=d');
4848
done();
4949
});
5050

@@ -54,16 +54,22 @@ describe('stringify()', function () {
5454
done();
5555
});
5656

57+
it('uses brackets when arrayFormat=brackets', function (done) {
58+
59+
expect(Qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'brackets' })).to.equal('a%5Bb%5D%5B%5D=c&a%5Bb%5D%5B%5D=d');
60+
done();
61+
});
62+
5763
it('stringifies an object inside an array', function (done) {
5864

5965
expect(Qs.stringify({ a: [{ b: 'c' }] })).to.equal('a%5B0%5D%5Bb%5D=c');
6066
expect(Qs.stringify({ a: [{ b: { c: [1] } }] })).to.equal('a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1');
6167
done();
6268
});
6369

64-
it('does not omit object keys when indices = false', function (done) {
70+
it('does not omit object keys when arrayFormat=repeat', function (done) {
6571

66-
expect(Qs.stringify({ a: [{ b: 'c' }] }, { indices: false })).to.equal('a%5Bb%5D=c');
72+
expect(Qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'repeat' })).to.equal('a%5Bb%5D=c');
6773
done();
6874
});
6975

0 commit comments

Comments
 (0)