-
-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add literal parsers #453
Changes from 1 commit
dc56674
afb16ec
0a440b7
7237733
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,8 +230,8 @@ export function parseAsStringEnum<Enum extends string>(validValues: Enum[]) { | |
} | ||
|
||
/** | ||
* String-based readonly lists provide better type-safety for known sets of values. | ||
* You will need to pass the parseAsStringConst function a list of your string values | ||
* String- or number-based readonly lists provide better type-safety for known sets of values. | ||
* You will need to pass the parseAsLiteral function a list of your string or number values | ||
* in order to validate the query string. Anything else will return `null`, | ||
* or your default value if specified. | ||
* | ||
|
@@ -241,25 +241,25 @@ export function parseAsStringEnum<Enum extends string>(validValues: Enum[]) { | |
* | ||
* const [color, setColor] = useQueryState( | ||
* 'color', | ||
* parseAsStringConst(colors) // pass a readonly list of allowed values | ||
* parseAsLiteral(colors) // pass a readonly list of allowed values | ||
* .withDefault("red") | ||
* ) | ||
* ``` | ||
* | ||
* @param validValues The values you want to accept | ||
*/ | ||
export function parseAsStringConst<Const extends string>( | ||
validValues: readonly Const[] | ||
export function parseAsLiteral<Literal extends string | number>( | ||
validValues: readonly Literal[] | ||
) { | ||
return createParser({ | ||
parse: (query: string) => { | ||
const asConst = query as unknown as Const | ||
const asConst = query as unknown as Literal | ||
if (validValues.includes(asConst)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what I meant by having to extend the existing numeric parsers: using number literals here won't work as the array [1, 2, 3] never contains the value "2", it has to be parsed first. We could hack our way out by detecting if the first element of the validValues is a number, then pass it through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be enough to provide 2 functions, that looks too much like a hack to me? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds like a good plan indeed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please take a look at this. By the way, I can't think of any other uses for literals in query than String and Number. So a generic attempt would not really achieve much. You can always create your own parser for special use-cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only other allowed literal type is boolean, and we already have a parser for that one, so this is perfect, thanks! |
||
return asConst | ||
} | ||
return null | ||
}, | ||
serialize: (value: Const) => value.toString() | ||
serialize: (value: Literal) => value.toString() | ||
}) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Update the doc to include a valid default value, like 4: https://xkcd.com/221/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I must have overlooked that.