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

Commit

Permalink
Merge pull request #31 from pbeshai/30-decoded-include-config
Browse files Browse the repository at this point in the history
Include all configured keys in decodeQueryParams, resolves #30
  • Loading branch information
pbeshai authored Aug 26, 2020
2 parents 4074176 + 7f7a91c commit d02382b
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 d02382b

Please sign in to comment.