maybeMap<T extends Object?> method

T maybeMap<T extends Object?>({
  1. required T orElse,
  2. T? camel,
  3. T? capital,
  4. T? constant,
  5. T? dot,
  6. T? header,
  7. T? kebab,
  8. T? no,
  9. T? none,
  10. T? pascal,
  11. T? path,
  12. T? sentence,
  13. T? snake,
  14. T? swap,
})

Optionally map all values of the enum

default value is provided when value has not been mapped

Implementation

T maybeMap<T extends Object?>({
  required T orElse,
  T? camel,
  T? capital,
  T? constant,
  T? dot,
  T? header,
  T? kebab,
  T? no,
  T? none,
  T? pascal,
  T? path,
  T? sentence,
  T? snake,
  T? swap,
}) {
  var isNullable = true;
  try {
    final value = null as T;
  } catch (_) {
    isNullable = false;
  }

  switch (this) {
    case SerializedFormat.camel:
      if (camel == null && !isNullable) return orElse;
      return camel as T;
    case SerializedFormat.capital:
      if (capital == null && !isNullable) return orElse;
      return capital as T;
    case SerializedFormat.constant:
      if (constant == null && !isNullable) return orElse;
      return constant as T;
    case SerializedFormat.dot:
      if (dot == null && !isNullable) return orElse;
      return dot as T;
    case SerializedFormat.header:
      if (header == null && !isNullable) return orElse;
      return header as T;
    case SerializedFormat.kebab:
      if (kebab == null && !isNullable) return orElse;
      return kebab as T;
    case SerializedFormat.no:
      if (no == null && !isNullable) return orElse;
      return no as T;
    case SerializedFormat.none:
      if (none == null && !isNullable) return orElse;
      return none as T;
    case SerializedFormat.pascal:
      if (pascal == null && !isNullable) return orElse;
      return pascal as T;
    case SerializedFormat.path:
      if (path == null && !isNullable) return orElse;
      return path as T;
    case SerializedFormat.sentence:
      if (sentence == null && !isNullable) return orElse;
      return sentence as T;
    case SerializedFormat.snake:
      if (snake == null && !isNullable) return orElse;
      return snake as T;
    case SerializedFormat.swap:
      if (swap == null && !isNullable) return orElse;
      return swap as T;
  }
}