match<T> function

T match<T>(
  1. dynamic value,
  2. Map<dynamic, T> values(), {
  3. dynamic defaultValue,
})

Match a value from a Map of data. It will return null if a match is not found.

Implementation

T match<T>(dynamic value, Map<dynamic, T> Function() values,
    {dynamic defaultValue}) {
  if (value == null) {
    return defaultValue;
  }

  Map<dynamic, T> valuesMeta = values();

  for (var val in valuesMeta.entries) {
    if (val.key == value) {
      return val.value;
    }
  }

  if (!valuesMeta.containsKey(value)) {
    NyLogger.error('The value "$value" does not match any values provided');
    if (defaultValue != null) {
      return defaultValue;
    } else {
      throw Exception('The value "$value" does not match any values provided');
    }
  }
  return valuesMeta[value] as T;
}