format static method

String format(
  1. dynamic data, {
  2. required FileType type,
})

Implementation

static String format(final data, {required final FileType type}) {
  try {
    if (type == FileType.json) {
      final dynamic jsonObject = jsonDecode(data);
      return const JsonEncoder.withIndent('  ').convert(jsonObject);
    } else if (type == FileType.yaml) {
      final yamlObject = loadYaml(data);
      final jsonCompatible = jsonDecode(jsonEncode(yamlObject));
      final formattedYaml =
          const JsonEncoder.withIndent('  ').convert(jsonCompatible);
      return formattedYaml;
    } else {
      throw AppException.fromFormatException(type);
    }
  } on FormatException {
    throw AppException.fromFormatException(type);
  }
}