Skip to content

Commit

Permalink
coerce known filter values to string
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Nov 22, 2020
1 parent 35e3fbd commit 829c8f0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,16 @@ describe('parseSearchParams', () => {
},
});
});

it('converts boolean and number values to string for known filters', () => {
const searchParams = parseSearchParams('tag:42 tags:true type:69 types:false hello');
expect(searchParams).toEqual({
term: 'hello',
filters: {
tags: ['42', 'true'],
types: ['69', 'false'],
unknowns: {},
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ export const parseSearchParams = (term: string): ParsedSearchParams => {
};
}, {} as Record<string, FilterValues>);

const tags = filterValues.get('tag');
const types = filterValues.get('type');

return {
term: searchTerm,
filters: {
tags: filterValues.get('tag') as FilterValues<string>,
types: filterValues.get('type') as FilterValues<string>,
tags: tags ? valuesToString(tags) : undefined,
types: types ? valuesToString(types) : undefined,
unknowns: unknownFilters,
},
};
};

const valuesToString = (raw: FilterValues): FilterValues<string> =>
raw.map((value) => String(value));
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ describe('getFieldValueMap', () => {
expect(result.get('tag')).toEqual(['foo', 'bar']);
});

it('parses boolean field terms', () => {
const result = fieldValueMap('tag:true tag:false');

expect(result.size).toBe(1);
expect(result.get('tag')).toEqual([true, false]);
});

it('parses numeric field terms', () => {
const result = fieldValueMap('tag:42 tag:9000');

expect(result.size).toBe(1);
expect(result.get('tag')).toEqual([42, 9000]);
});

it('parses multiple mixed single/multi value field terms', () => {
const result = fieldValueMap('tag:foo tag:(bar OR hello) tag:dolly');

Expand Down

0 comments on commit 829c8f0

Please sign in to comment.