formatValue method

String formatValue(
  1. dynamic value,
  2. Map? spec
)

Implementation

String formatValue(dynamic value, Map? spec) {
  if (value == null) {
    return "";
  }

  value = value.toString();
  if (spec != null) {
    switch (spec["_type"]) {
      case "KFormatterTranslate":
        final attributes =
            Map<String, dynamic>.from(spec["attributes"] ?? {});
        if (spec["transform"] == "upper") {
          return Strings.getUpper(value, attributes: attributes);
        } else if (spec["transform"] == "lower") {
          return Strings.getLower(value, attributes: attributes);
        } else if (spec["transform"] == "capitalize") {
          return Strings.getCapitalized(value, attributes: attributes);
        } else if (spec["transform"] == "title") {
          return Strings.getTitle(value, attributes: attributes);
        }
        return Strings.get(value, attributes: attributes);
      case "KFormatterDateTime":
        return Lowder.properties.formatDateTime(
          parseDateTime(value),
          parseBool(spec["diffToNow"]),
          format: spec["format"],
        );
      case "KFormatterDate":
        return Lowder.properties.formatDate(
          parseDateTime(value),
          parseBool(spec["diffToNow"]),
          format: spec["format"],
        );
      case "KFormatterTime":
        return Lowder.properties.formatTime(
          parseDateTime(value),
          parseBool(spec["diffToNow"]),
          format: spec["format"],
        );
      case "KFormatterNumber":
        final format = spec["format"] ?? Strings.get("_number_format_");
        return NumberFormat(format).format(parseDouble(value));
      case "KFormatterCurrency":
        return NumberFormat.currency(
                symbol: spec["symbol"] ?? Strings.get("_currency_symbol_"),
                decimalDigits:
                    parseInt(spec["decimalDigits"], defaultValue: 2))
            .format(parseDouble(value));
    }
  }
  return value;
}