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

[FEATURE] Expression utility functions #454

Open
XanderD99 opened this issue Jun 7, 2024 · 0 comments
Open

[FEATURE] Expression utility functions #454

XanderD99 opened this issue Jun 7, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@XanderD99
Copy link
Contributor

Feature Description

Since starting to use expressions more in my layers some utility functions to create them could be nice to have. This could improve the readability of them since the map expressions can become hard to read very fast.

This could be a time-consuming task to get done. but nice to have in my opinion.

I already have some examples since I use them in a personal project.

List ifelseCase({required final Map cases, required final defValue}) {
  final flattened = cases.entries.map((final element) {
    return [element.key, element.value];
  }).flatten;

  return ['case', ...flattened, defValue];
}

List boolean(final String property, {final bool value = true}) {
  return ['boolean', has(property), value];
}

List match({
  required final value,
  required final Map matches,
  required final defValue,
}) {
  final flattened = matches.entries.map((final element) {
    return [element.key, element.value];
  }).flatten;

  return ['match', value, ...flattened, defValue];
}

List get(final String name, [final expression]) {
  return ['get', name, if (expression != null) expression];
}

final sort = ifelseCase(
  cases: {
    boolean('category'): match(
      value: get('category'),
      matches: {
        'first': 1,
        'second': 2,
      },
      defValue: 0,
    ),
  },
  defValue: 0,
);

Describe alternatives you've considered

No response

Additional context

No response

@XanderD99 XanderD99 added the enhancement New feature or request label Jun 7, 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