Skip to content

Commit

Permalink
[New] add depth=false to preserve the original key; [Fix] depth=0
Browse files Browse the repository at this point in the history
… should preserve the original key
  • Loading branch information
ljharb committed Aug 16, 2019
1 parent a30e4b1 commit 649f05f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
7 changes: 4 additions & 3 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ var parseKeys = function parseQueryStringKeys(givenKey, val, options) {

// Get the parent

var segment = brackets.exec(key);
var segment = options.depth > 0 && brackets.exec(key);
var parent = segment ? key.slice(0, segment.index) : key;

// Stash the parent if it exists
Expand All @@ -167,7 +167,7 @@ var parseKeys = function parseQueryStringKeys(givenKey, val, options) {
// Loop through children appending to the array until we hit depth

var i = 0;
while ((segment = child.exec(key)) !== null && i < options.depth) {
while (options.depth > 0 && (segment = child.exec(key)) !== null && i < options.depth) {
i += 1;
if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
if (!options.allowPrototypes) {
Expand Down Expand Up @@ -209,7 +209,8 @@ var normalizeParseOptions = function normalizeParseOptions(opts) {
comma: typeof opts.comma === 'boolean' ? opts.comma : defaults.comma,
decoder: typeof opts.decoder === 'function' ? opts.decoder : defaults.decoder,
delimiter: typeof opts.delimiter === 'string' || utils.isRegExp(opts.delimiter) ? opts.delimiter : defaults.delimiter,
depth: typeof opts.depth === 'number' ? opts.depth : defaults.depth,
// eslint-disable-next-line no-implicit-coercion, no-extra-parens
depth: (typeof opts.depth === 'number' || opts.depth === false) ? +opts.depth : defaults.depth,
ignoreQueryPrefix: opts.ignoreQueryPrefix === true,
interpretNumericEntities: typeof opts.interpretNumericEntities === 'boolean' ? opts.interpretNumericEntities : defaults.interpretNumericEntities,
parameterLimit: typeof opts.parameterLimit === 'number' ? opts.parameterLimit : defaults.parameterLimit,
Expand Down
16 changes: 2 additions & 14 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,13 @@ test('parse()', function (t) {
st.end();
});

t.test('current behavior with depth = 0', function (st) {
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: 0 }), { a: { '[0]': 'b', '[1]': 'c' } });
st.deepEqual(qs.parse('a[0][0]=b&a[0][1]=c&a[1]=d&e=2', { depth: 0 }), { a: { '[0][0]': 'b', '[0][1]': 'c', '[1]': 'd' }, e: '2' });
st.end();
});

t.test('uses original key when depth = 0', { skip: true, todo: true }, function (st) {
t.test('uses original key when depth = 0', function (st) {
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: 0 }), { 'a[0]': 'b', 'a[1]': 'c' });
st.deepEqual(qs.parse('a[0][0]=b&a[0][1]=c&a[1]=d&e=2', { depth: 0 }), { 'a[0][0]': 'b', 'a[0][1]': 'c', 'a[1]': 'd', e: '2' });
st.end();
});

t.test('current behavior with depth = false', function (st) {
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: false }), { a: ['b', 'c'] });
st.deepEqual(qs.parse('a[0][0]=b&a[0][1]=c&a[1]=d&e=2', { depth: false }), { a: [['b', 'c'], 'd'], e: '2' });
st.end();
});

t.test('uses original key when depth = false', { skip: true, todo: true }, function (st) {
t.test('uses original key when depth = false', function (st) {
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: false }), { 'a[0]': 'b', 'a[1]': 'c' });
st.deepEqual(qs.parse('a[0][0]=b&a[0][1]=c&a[1]=d&e=2', { depth: false }), { 'a[0][0]': 'b', 'a[0][1]': 'c', 'a[1]': 'd', e: '2' });
st.end();
Expand Down

0 comments on commit 649f05f

Please sign in to comment.