Add pattern matching
See original GitHub issueHi!
I was just about to build something like this myself when I stumbled on this library haha. Great work! I love how you approached it.
A common pattern in Remix when having multiple forms on one page, is to add a hidden input named action
to your form. It would be great to be able to combine domain functions with pattern matching to handle this. For example:
type Input =
| {
action: 'update';
name: string;
}
| {
action: 'delete';
id: string;
};
// Or possibly:
// type Input =
// | UnpackInput<typeof updateProjectName>
// | UnpackInput<typeof deleteStuff>;
export const action: ActionFunction = async ({ request }) => {
const input = (await inputFromForm(request)) as Input;
return match(input.action)
.with({ action: 'update' }, (input) => updateProjectName(input))
.with({ action: 'delete' }, (input) => deleteStuff(input))
.otherwise(() => {
throw new Error('Unexpected action');
});
};
ts-pattern
immediately comes to mind. Maybe this could be integrated or serve as inspiration.
Also, quick question; is this library actually specific to Remix? By the looks of it I can use this in any other framework, as long as it gets a Request
as input?
Issue Analytics
- State:
- Created a year ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Pattern matching overview - C# guide | Microsoft Learn
C# pattern matching provides more concise syntax for testing expressions and taking action when an expression matches. The " is expression" ...
Read more >Pattern Matching | Tour of Scala - Scala Documentation
Pattern matching is a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent...
Read more >PEP 636 – Structural Pattern Matching: Tutorial
This PEP is a tutorial for the pattern matching introduced by PEP 634. ... For example, you might want to add single verbs...
Read more >Pattern Matching - Reason
Pattern matching provides a way to conditionally execute code when the shape of some data matches a particular pattern. It is similar to...
Read more >GitHub - tc39/proposal-pattern-matching
This proposal adds a pattern matching expression to the language, based in part on the existing Destructuring Binding Patterns. This proposal was approved ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for taking the time to elaborate! Makes a lot of sense 😃 As far as I’m concerned we can close this issue, unless you’re still thinking about implementing pattern matching.
Sure @waspeer!
We like to keep our Remix Actions and Loaders all in the same shape:
And we try to solve all our problems with composition of domain functions so we have one domain function per loader/action.
This allows us to create Response helpers such as:
Which leads us to tiny and similar actions/loaders throughout the app:
By solving all our data needs with domain-function composition we can also properly type our components when needed:
This is a little bit about how we’ve been using the
domain-functions
library at Seasoned. I also wrote a post about it although it is a bit dated as we’ve found more patterns and added more API surface to the library… it still worth a read to understand some of our reasoning though 😉I hope it helps clarifying where we are heading to! Cheers