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

Commit d02382b

Browse files
authored
Merge pull request #31 from pbeshai/30-decoded-include-config
Include all configured keys in decodeQueryParams, resolves #30
2 parents 4074176 + 7f7a91c commit d02382b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/__tests__/decodeQueryParams-test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { decodeQueryParams } from '../index';
2+
23
import {
34
NumberParam,
45
ArrayParam,
56
StringParam,
67
DelimitedArrayParam,
78
} from '../params';
9+
import withDefault from '../withDefault';
810

911
describe('decodeQueryParams', () => {
1012
it('works', () => {
@@ -14,6 +16,7 @@ describe('decodeQueryParams', () => {
1416
bar: NumberParam,
1517
baz: ArrayParam,
1618
box: DelimitedArrayParam,
19+
not: withDefault(NumberParam, 94),
1720
},
1821
{
1922
foo: '123',
@@ -27,6 +30,7 @@ describe('decodeQueryParams', () => {
2730
bar: 555,
2831
baz: ['a', 'b', 'c'],
2932
box: ['a', 'b', 'c'],
33+
not: 94,
3034
});
3135
});
3236

src/decodeQueryParams.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ export function decodeQueryParams<QPCMap extends QueryParamConfigMap>(
1313
): Partial<DecodedValueMap<QPCMap>> {
1414
const decodedQuery: Partial<DecodedValueMap<QPCMap>> = {};
1515

16-
const paramNames = Object.keys(encodedQuery);
16+
// iterate over all keys in the config (#30)
17+
const paramNames = Object.keys(paramConfigMap);
18+
19+
// ensure any non configured keys that are in the URL are also included
20+
for (const encodedKey of Object.keys(encodedQuery)) {
21+
if (paramConfigMap[encodedKey] == null) {
22+
paramNames.push(encodedKey);
23+
}
24+
}
25+
1726
for (const paramName of paramNames) {
1827
const encodedValue = encodedQuery[paramName];
1928

0 commit comments

Comments
 (0)