Skip to content

Commit

Permalink
feat(file_utils): add support for CSV files
Browse files Browse the repository at this point in the history
  • Loading branch information
nikaera committed Jan 8, 2024
1 parent c0edcc3 commit e290e5b
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion slang/lib/builder/utils/file_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,55 @@ class FileUtils {
}
return json2yaml(content, yamlStyle: YamlStyle.generic);
case FileType.csv:
throw UnimplementedError('CSV is not supported yet');
String escapeRow(String value) {
final escaped = value.replaceAll('"', '""');
if (escaped.contains(RegExp(r'[,"]'))) {
return '"$escaped"';
}
return escaped;
}

Map<String, String> encodeRow({
String key = '',
required dynamic value,
}) {
if (value is Map) {
final keyPrefix = key.isEmpty ? '' : '$key.';
return value.map((k, v) {
final map = encodeRow(key: '$keyPrefix$k', value: v);
final mapEntry = map.entries.first;

return MapEntry(mapEntry.key, mapEntry.value);
});
} else if (value is String) {
return {key: escapeRow(value)};
}
return {};
}

final Map<String, Map<String, String>> columns = {};
if (content.containsKey(INFO_KEY)) {
final info = content.remove(INFO_KEY);
columns[INFO_KEY] = {INFO_KEY: escapeRow(info.join('\\n'))};
}
content.entries.forEach((e) {
columns[e.key] = encodeRow(value: e.value);
});

// get all translation keys
final translationKeys = columns.values
.map((e) => e.entries.map((e) => e.key))
.expand((e) => e)
.toSet();

final headers = ['key', ...columns.keys].join(',');
final rows = translationKeys.map((key) {
final values =
columns.values.map((e) => e.containsKey(key) ? e[key] : '');
return "$key,${values.join(',')}";
});

return "$headers\n${rows.join('\n')}";
case FileType.arb:
// this encoder does not append \n automatically
return JsonEncoder.withIndent(' ').convert(content) + '\n';
Expand Down

0 comments on commit e290e5b

Please sign in to comment.