Skip to content
This repository has been archived by the owner on May 1, 2021. It is now read-only.

Commit

Permalink
Include all configured keys in decodeQueryParams, resolves #30
Browse files Browse the repository at this point in the history
  • Loading branch information
pbeshai committed Aug 26, 2020
1 parent dd1d131 commit 7f7a91c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/__tests__/decodeQueryParams-test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { decodeQueryParams } from '../index';

import {
NumberParam,
ArrayParam,
StringParam,
DelimitedArrayParam,
} from '../params';
import withDefault from '../withDefault';

describe('decodeQueryParams', () => {
it('works', () => {
Expand All @@ -14,6 +16,7 @@ describe('decodeQueryParams', () => {
bar: NumberParam,
baz: ArrayParam,
box: DelimitedArrayParam,
not: withDefault(NumberParam, 94),
},
{
foo: '123',
Expand All @@ -27,6 +30,7 @@ describe('decodeQueryParams', () => {
bar: 555,
baz: ['a', 'b', 'c'],
box: ['a', 'b', 'c'],
not: 94,
});
});

Expand Down
11 changes: 10 additions & 1 deletion src/decodeQueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ export function decodeQueryParams<QPCMap extends QueryParamConfigMap>(
): Partial<DecodedValueMap<QPCMap>> {
const decodedQuery: Partial<DecodedValueMap<QPCMap>> = {};

const paramNames = Object.keys(encodedQuery);
// iterate over all keys in the config (#30)
const paramNames = Object.keys(paramConfigMap);

// ensure any non configured keys that are in the URL are also included
for (const encodedKey of Object.keys(encodedQuery)) {
if (paramConfigMap[encodedKey] == null) {
paramNames.push(encodedKey);
}
}

for (const paramName of paramNames) {
const encodedValue = encodedQuery[paramName];

Expand Down

0 comments on commit 7f7a91c

Please sign in to comment.