Skip to content

Commit c9b3310

Browse files
allowDotsForNumbers option to convert from brackets of indices to dots
1 parent e6c0724 commit c9b3310

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

lib/parse.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var isArray = Array.isArray;
77

88
var defaults = {
99
allowDots: false,
10-
allowDotsForIndices: false,
10+
allowDotsForNumbers: false,
1111
allowEmptyArrays: false,
1212
allowPrototypes: false,
1313
allowSparse: false,
@@ -216,8 +216,8 @@ var parseObject = function (chain, val, options, valuesParsed) {
216216
};
217217

218218
var splitKeyIntoSegments = function splitKeyIntoSegments(givenKey, options) {
219-
var key = options.allowDots
220-
? options.allowDotsForIndices ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey.replace(/\.(?!\d)([^.[\]]+)/g, '[$1]') : givenKey;
219+
var key = options.allowDots ? options.allowDotsForNumbers
220+
? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey.replace(/\.(?!\d+(?:\.|\[|\]|$))([^.[]+)/g, '[$1]') : givenKey;
221221

222222
if (options.depth <= 0) {
223223
if (!options.plainObjects && has.call(Object.prototype, key)) {
@@ -319,17 +319,17 @@ var normalizeParseOptions = function normalizeParseOptions(opts) {
319319
throw new TypeError('The duplicates option must be either combine, first, or last');
320320
}
321321

322-
if ('allowDots' in opts && opts.allowDots === false && opts.allowDotsForIndices) {
323-
throw new TypeError('allowDots cannot be false and allowDotsForIndices true at the same time');
322+
if ('allowDots' in opts && opts.allowDots === false && opts.allowDotsForNumbers) {
323+
throw new TypeError('allowDots cannot be false and allowDotsForNumbers true at the same time');
324324
}
325325

326-
var allowDots = typeof opts.allowDots === 'undefined' ? opts.decodeDotInKeys === true || !!opts.allowDotsForIndices
326+
var allowDots = typeof opts.allowDots === 'undefined' ? opts.decodeDotInKeys === true || !!opts.allowDotsForNumbers
327327
? true : defaults.allowDots : !!opts.allowDots;
328-
var allowDotsForIndices = typeof opts.allowDotsForIndices === 'undefined' ? allowDots : !!opts.allowDotsForIndices;
328+
var allowDotsForNumbers = typeof opts.allowDotsForNumbers === 'undefined' ? allowDots : !!opts.allowDotsForNumbers;
329329

330330
return {
331331
allowDots: allowDots,
332-
allowDotsForIndices: allowDotsForIndices,
332+
allowDotsForNumbers: allowDotsForNumbers,
333333
allowEmptyArrays: typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays,
334334
allowPrototypes: typeof opts.allowPrototypes === 'boolean' ? opts.allowPrototypes : defaults.allowPrototypes,
335335
allowSparse: typeof opts.allowSparse === 'boolean' ? opts.allowSparse : defaults.allowSparse,

lib/stringify.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var defaultFormat = formats['default'];
3333
var defaults = {
3434
addQueryPrefix: false,
3535
allowDots: false,
36-
allowDotsForIndices: false,
36+
allowDotsForNumbers: false,
3737
allowEmptyArrays: false,
3838
arrayFormat: 'indices',
3939
charset: 'utf-8',
@@ -79,7 +79,7 @@ var stringify = function stringify(
7979
filter,
8080
sort,
8181
allowDots,
82-
allowDotsForIndices,
82+
allowDotsForNumbers,
8383
serializeDate,
8484
format,
8585
formatter,
@@ -196,7 +196,7 @@ var stringify = function stringify(
196196
filter,
197197
sort,
198198
allowDots,
199-
allowDotsForIndices,
199+
allowDotsForNumbers,
200200
serializeDate,
201201
format,
202202
formatter,
@@ -226,8 +226,8 @@ var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
226226
throw new TypeError('Encoder has to be a function.');
227227
}
228228

229-
if ('allowDots' in opts && opts.allowDots === false && opts.allowDotsForIndices) {
230-
throw new TypeError('allowDots cannot be false and allowDotsForIndices true at the same time');
229+
if ('allowDots' in opts && opts.allowDots === false && opts.allowDotsForNumbers) {
230+
throw new TypeError('allowDots cannot be false and allowDotsForNumbers true at the same time');
231231
}
232232

233233
var charset = opts.charset || defaults.charset;
@@ -254,7 +254,7 @@ var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
254254
arrayFormat = opts.arrayFormat;
255255
} else if ('indices' in opts) {
256256
arrayFormat = opts.indices ? 'indices' : 'repeat';
257-
} else if (opts.allowDotsForIndices) {
257+
} else if (opts.allowDotsForNumbers) {
258258
arrayFormat = 'dots';
259259
} else {
260260
arrayFormat = defaults.arrayFormat;
@@ -265,12 +265,12 @@ var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
265265
}
266266

267267
var allowDots = typeof opts.allowDots === 'undefined' ? opts.encodeDotInKeys === true ? true : defaults.allowDots : !!opts.allowDots;
268-
var allowDotsForIndices = !!opts.allowDotsForIndices;
268+
var allowDotsForNumbers = !!opts.allowDotsForNumbers;
269269

270270
return {
271271
addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix,
272272
allowDots: allowDots,
273-
allowDotsForIndices: allowDotsForIndices,
273+
allowDotsForNumbers: allowDotsForNumbers,
274274
allowEmptyArrays: typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays,
275275
arrayFormat: arrayFormat,
276276
charset: charset,
@@ -344,7 +344,7 @@ module.exports = function (object, opts) {
344344
options.filter,
345345
options.sort,
346346
options.allowDots,
347-
options.allowDotsForIndices,
347+
options.allowDotsForNumbers,
348348
options.serializeDate,
349349
options.format,
350350
options.formatter,

test/parse.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ test('parse()', function (t) {
6363
st.end();
6464
});
6565

66-
t.test('allows enabling dots for indices', function (st) {
67-
st.deepEqual(qs.parse('a.0.b=d&a.1.c=e', { allowDotsForIndices: false, allowDots: true }), { 'a.0': { b: 'd' }, 'a.1': { c: 'e' } }, 'with allowDotsForIndices false and allowDots true');
68-
st.deepEqual(qs.parse('a.b.0=c&a.c.0=d', { allowDotsForIndices: true }), { a: { b: ['c'], c: ['d'] } }, 'with only allowDotsForIndices true');
66+
t.test('allows enabling dots for numbers', function (st) {
67+
st.deepEqual(qs.parse('a.0.b=d&a.1.c=e', { allowDotsForNumbers: false, allowDots: true }), { 'a.0': { b: 'd' }, 'a.1': { c: 'e' } }, 'with allowDotsForNumbers false and allowDots true');
68+
st.deepEqual(qs.parse('a.b.0=c&a.c.0=d', { allowDotsForNumbers: true }), { a: { b: ['c'], c: ['d'] } }, 'with only allowDotsForIndices true');
6969
st.end();
7070
});
7171

test/stringify.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ test('stringify()', function (t) {
227227
st.end();
228228
});
229229

230-
t.test('`allowDotsForIndices` option: stringifies a nested object with converting indices to dot notation', function (st) {
231-
st.equal(qs.stringify({ a: [1, 2] }, { allowDotsForIndices: true }), 'a.0=1&a.1=2');
232-
st.equal(qs.stringify({ a: [{ b: 'c', d: 'e' }] }, { allowDotsForIndices: true, allowDots: true }), 'a.0.b=c&a.0.d=e');
230+
t.test('`allowDotsForNumbers` option: stringifies a nested object with converting numbers to dot notation', function (st) {
231+
st.equal(qs.stringify({ a: [1, 2] }, { allowDotsForNumbers: true }), 'a.0=1&a.1=2');
232+
st.equal(qs.stringify({ a: [{ b: 'c', d: 'e' }] }, { allowDotsForNumbers: true, allowDots: true }), 'a.0.b=c&a.0.d=e');
233233
st.end();
234234
});
235235

0 commit comments

Comments
 (0)