parseTextToJson function
Takes raw string and formats it to json. If you decode the result of the string returned by this then, the resulting object is represented as:
// if only dart had union types or tuples
typedef Json = List<Map<String, Object>>;
Json json = {
'description': 'flag: Nepal',
'emoji': ['🇳🇵']
}
Implementation
String parseTextToJson(String text) {
final lines = LineSplitter().convert(text);
List<Map<String, Object>> emojis = [];
for (final line in lines) {
if (!line.startsWith(RegExp(r'[0-9]'))) {
continue;
}
final reg = RegExp(r'\(.*?\)');
final item = line.split(reg);
if (item.length != 2) {
continue;
}
final emoji = line
.splitMapJoin((reg), onMatch: (m) => '${m[0]}', onNonMatch: (n) => '')
.replaceAll(RegExp(r'\(|\)'), '')
.split('..');
String description;
final head = item.first;
final tail = item.last;
if (tail.isEmpty) {
description = head.split(';')[2].split('#')[0].trim();
} else {
description = tail.trim();
}
emojis.add({'description': description, 'emoji': emoji});
}
final encoder = JsonEncoder.withIndent(' ');
return encoder.convert(emojis);
}