maplibre / flutter-maplibre-gl

Customizable, performant and vendor-free vector and raster maps, flutter wrapper for maplibre-native and maplibre-gl-js (fork of flutter-mapbox-gl/maps)
https://pub.dev/packages/maplibre_gl
Other
185 stars 106 forks source link

[FEATURE] Expression utility functions #454

Open XanderD99 opened 3 weeks ago

XanderD99 commented 3 weeks ago

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