1
0
mirror of https://github.com/go-vikunja/app synced 2024-05-31 09:46:51 +00:00
app-mirror-github/lib/utils/checkboxes_in_text.dart
Denys Vitali 056b2d72c9
chore: format code with dart format (#71)
This PR formats all code with dart format and adds a step to the CI so that it will be checked on every push and PR.
2024-04-05 22:36:56 +02:00

49 lines
1.0 KiB
Dart

class CheckboxStatistics {
final int total;
final int checked;
const CheckboxStatistics({
required this.total,
required this.checked,
});
}
class MatchedCheckboxes {
final Iterable<Match> checked;
final Iterable<Match> unchecked;
const MatchedCheckboxes({
required this.checked,
required this.unchecked,
});
}
MatchedCheckboxes getCheckboxesInText(String text) {
const checkedString = '[x]';
final checked = <Match>[];
final unchecked = <Match>[];
final matches = RegExp(r'[*-] \[[ x]]').allMatches(text);
for (final match in matches) {
if (match[0]?.endsWith(checkedString) ?? false)
checked.add(match);
else
unchecked.add(match);
}
return MatchedCheckboxes(
checked: checked,
unchecked: unchecked,
);
}
CheckboxStatistics getCheckboxStatistics(String text) {
final checkboxes = getCheckboxesInText(text);
return CheckboxStatistics(
total: checkboxes.checked.length + checkboxes.unchecked.length,
checked: checkboxes.checked.length,
);
}