Skip to content
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

Ability to Dynamically Build Match Cases #274

Open
rjray opened this issue Aug 24, 2024 · 0 comments
Open

Ability to Dynamically Build Match Cases #274

rjray opened this issue Aug 24, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@rjray
Copy link

rjray commented Aug 24, 2024

Is your feature request related to a problem? Please describe.

I have a case where I want to be able to match against an arbitrary set of value-cases, with the return value of each match being known. The value-cases and matching closures would be derived from input data. For example:

// type AuthorFetchOpts = { aliases: boolean; references: boolean; };

function getScopes(params: AuthorFetchOpts): string {
  return match([params.aliases, params.references])
    .returnType<string[]>()
    .with([false, false], () => ["defaultScope"])
    .with([false, true], () => ["defaultScope", "references"])
    .with([true, false], () => ["defaultScope", "aliases"])
    .with([true, true], () => ["defaultScope", "aliases", "references"])
    .run();
}

(This is a weak example, since in this case I could accomplish this by using filter over the keys of the input object.)

In this example I would prefer to pass in to getScopes a generic Record<string, boolean>, derive the 2^n cases and their return values, and then use match() to process it all. This is similar in nature to the request in #217, I think, but sufficiently different to file this as a new request.

Describe the solution you'd like

Something that creates an initial (or "empty") matcher, allows the programmatic addition of with cases, then can have run called on it.

Describe alternatives you've considered

I have looked at passing multiple patterns to with, but it isn't clear how I would carry over the matching pattern to the handler.

@rjray rjray added the enhancement New feature or request label Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant