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

Conversation

@nekitk
Copy link
Contributor

@nekitk nekitk commented Dec 14, 2020

I've been using use-query-params in my TS project and there're times when StringParam is not enough. E.g. when storing filter values in query, I want to check that these values are compatible with TS enum. For that purpose I added simple function to ease the creation of custom enum params.

Example:

import { createEnumParam } from 'serialize-query-params'; // values other than 'asc' or 'desc' will be decoded as undefined const SortOrderEnumParam = createEnumParam(['asc', 'desc'])

Decided to submit PR with these changes in case it would be helpful for others too

Copy link
Owner

@pbeshai pbeshai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, it's a convenient parameter that I've also created similar versions of in the past. One minor request, but otherwise looks great.

src/serialize.ts Outdated
Comment on lines 271 to 272
const isEnumMember = (str: string): str is T => enumValues.includes(str as any)
return isEnumMember(str) ? str : undefined
Copy link
Owner

@pbeshai pbeshai Dec 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid creating this intermediate function by writing the code this way?

return enumValues.includes(str as any) ? str as T : undefined
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Done

@pbeshai pbeshai merged commit 3251f28 into pbeshai:master Dec 15, 2020
@pbeshai
Copy link
Owner

pbeshai commented Dec 15, 2020

Thank you! Published in v1.3.0

@nekitk nekitk deleted the enum-param branch December 15, 2020 16:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

2 participants