Skip to content

Commit

Permalink
fix: remove dead code for index nested start keys
Browse files Browse the repository at this point in the history
When using index nesting, the parse loop visits `[` and computes keys
_only if_ we didn't just pass a `]`.

This is to capture the start of such a key. For example, `foo[bar][baz]`
would result in the only occurrence of this being `foo[`.

Since a syntactically valid indexed key should only have one of these
(at the start), `lastKey` should always be `undefined` at that point. So
we can remove the check.
  • Loading branch information
43081j committed Jun 23, 2024
1 parent 4da194f commit 7c67a0b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,6 @@ export function parse(input: string, options?: ParseOptions): ParsedQuery {
);

currentKey = keyDeserializer(keyChunk);
if (lastKey !== undefined) {
currentObj = getDeepObject(currentObj, lastKey, currentKey);
}
lastKey = currentKey;

keyHasPlus = false;
Expand Down
17 changes: 17 additions & 0 deletions src/test/test-cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ export const testCases: TestCase[] = [
output: {foo: {bar: {baz: 'dwa'}}},
options: {nesting: true, nestingSyntax: 'index'}
},
{
input: 'foo[one][two][three]=dwa',
stringifyOutput: 'foo%5Bone%5D%5Btwo%5D%5Bthree%5D=dwa',
output: {foo: {one: {two: {three: 'dwa'}}}},
options: {nesting: true, nestingSyntax: 'index'}
},
{
input: 'foo[one]invalid[two]=dwa',
stringifyOutput: 'foo%5Binvalid%5D%5Btwo%5D=dwa',
output: {foo: {invalid: {two: 'dwa'}}},
options: {nesting: true, nestingSyntax: 'index'}
},
{
input: 'foo[bar=trzy',
stringifyOutput: 'foo%5Bbar%5D=trzy',
Expand All @@ -205,6 +217,11 @@ export const testCases: TestCase[] = [
output: {foo: {bar: 'x', baz: 'y'}},
options: {nesting: true, nestingSyntax: 'dot'}
},
{
input: 'foo.bar.x=x&foo.bar.y=y',
output: {foo: {bar: {x: 'x', y: 'y'}}},
options: {nesting: true, nestingSyntax: 'dot'}
},
{
input: 'foo.bar=x&foo.baz=y',
output: {'foo.bar': 'x', 'foo.baz': 'y'},
Expand Down

0 comments on commit 7c67a0b

Please sign in to comment.