enum_ext 1.2.0 copy "enum_ext: ^1.2.0" to clipboard
enum_ext: ^1.2.0 copied to clipboard

outdated

Lets make enums super useful by adding some useful extension methods. Say good bye to switch-case and if-else blocks.

Lets make enums 🚀 by adding some useful extension methods.
Say good bye to switch-case and if-else blocks.


Inspired by freezed

Features #

  • Generate Some Useful Methods #

    • when
    • mayBeWhen
    • onlyWhen
    • map
    • mapSimply
    • mayBeMap
  • Generate Value Checking Getters #

  • Generate Custom Extension Value #

  • Change Case Of Enum Names #

Getting started #

Add following dependencies -

dependencies:
  yaml: ^3.1.0
  enum_ext: ^1.0.0

dev_dependencies:
  build_runner:
  enum_ext_gen: ^1.0.0

Usage #

Add the part directive in the file

part 'file_name.g.dart';

Extension methods #

In order to generate extension methods, simply annotate the enum with @EnumExt()

@EnumExt()
enum HttpResponse { ok, notFound, internalServerError }

⚙️ Now generate the code using build_runner.
Dart only -

dart pub run build_runner build
// OR
dart pub run build_runner watch

Flutter -

flutter pub run build_runner build
// OR
flutter pub run build_runner watch

This will generate all these extension methods for you

  final response = HttpResponse.internalServerError;

  response.when(
    ok: (e) {
      // Do some actions only if the response is HttpResponse.ok
    },
    notFound: (e) {
      // Do some actions only if the response is HttpResponse.notFound
    },
    internalServerError: (e) {
      // Do some actions only if the response is HttpResponse.internalServerError
    },
  );

  response.mayBeWhen(
    ok: (e) {
      // Do some actions only if the response is HttpResponse.ok
    },
    orElse: (e) {
      // Do some actions only if the response is something other than HttpResponse.ok
    },
  );

  response.onlyWhen(
    ok: (e) {
      // Do some actions only if the response is HttpResponse.ok
    },
  );

  final value = response.map(
    ok: (e) => "Some value based on HttpResponse.ok",
    notFound: (e) => "Some value based on HttpResponse.notFound",
    internalServerError: (e) => "Some value based on HttpResponse.internalServerError",
  );

  final skinType = response.mapSimply(
    ok: "Some value based on HttpResponse.ok",
    notFound: "Some value based on HttpResponse.notFound",
    internalServerError: "Some value based on HttpResponse.internalServerError",
  );

Note :- when(), onlyWhen(), mayBeWhen() methods used when do just want to perform some task based on specific enum properties, whereas map(), mayBeMap(), mapSimply() methods are used to return a value based on the enum.

Value Checking Getters #

It will also generate value checking getter like

response.isOk // response == Response.ok
response.isNotOk // response != Response.ok

If you do not want to generate value checking getters, you can set conditionalGetters: false like this

EnumExt(conditionalGetters: false)

Extension value #

In order to generate extension value, annotate enum properties with @EnumExtValue(...)
⚠️Note:- Any enum propery that is not annotated with @EnumExtValue(...) will simply return null
⚠️Note:- Supported values for @EnumExtValue(...) are String, int, double, List, Map, Set and Symbol. Any other vaue will be replaced by null

Lets add some extension values to the HttpResponse enum

@EnumExt()
enum HttpResponse {
  @EnumExtValue(200)
  ok,
  @EnumExtValue(404)
  notFound,
  @EnumExtValue(500)
  internalServerError,
}

This will generate a getter extension named value on the enum.

  final response = HttpResponse.internalServerError;

  print(response); // HttpResponse.internalServerError
  print(response.index); // 2
  print(response.name); // internalServerError
  print(response.value); // 500 <-- 🔥 Extension

Change case of enum names #

Import the following

import 'package:enum_ext/enum_ext.dart';
enum HttpResponse { ok, notFound, internalServerError }

void main() {
  final response = HttpResponse.internalServerError;

  print(response.camelCase); // internalServerError
  print(response.constantCase); // INTERNAL_SERVER_ERROR
  print(response.dotCase); // internal.server.error
  print(response.headerCase); // Internal-Server-Error
  print(response.paramCase); // internal-server-error
  print(response.pascalCase); // InternalServerError
  print(response.pathCase); // internal/server/error
  print(response.sentenceCase); // Internal server error
  print(response.snakeCase); // internal_server_error
  print(response.titleCase); // Internal Server Error
}

Please hit a like👍 if this package made your life 🚀 #

13
likes
0
pub points
24%
popularity

Publisher

unverified uploader

Lets make enums super useful by adding some useful extension methods. Say good bye to switch-case and if-else blocks.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

recase

More

Packages that depend on enum_ext