Skip to content

Commit

Permalink
fix: lookup expected months in sorted order
Browse files Browse the repository at this point in the history
  • Loading branch information
gbassisp committed Dec 17, 2024
1 parent 4ade197 commit d2816ea
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/src/param_cleanup_rules.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,19 @@ String _restoreMillisecons(String formattedString, String separator) {
);
}

// .reversed because it works for disambiguation
// (e.g., in vi locale try 'thang 12' before 'thang 1')
Iterable<Month> _sortMonths(Iterable<Month> months) {
final sorted = months.toList()
..sort((a, b) => a.name.length.compareTo(b.name.length))
..sort((a, b) => a.number.compareTo(b.number));

return sorted.reversed;
}

Month? _expectMonth(DateParsingParameters parameters) {
final timestamp = parameters.formattedString.toLowerCase();
final month = parameters.parserInfo.months.where(
final month = _sortMonths(parameters.parserInfo.months).where(
(element) =>
element.name.tryToInt() == null &&
timestamp.contains(element.name.toLowerCase()),
Expand All @@ -136,12 +146,22 @@ Month? _expectMonth(DateParsingParameters parameters) {
return month.firstOrNullExtension ?? english.firstOrNullExtension;
}

// .reversed because it works for disambiguation
// (e.g., in vi locale try 'thang 12' before 'thang 1')
Iterable<Weekday> _sortWeekdays(Iterable<Weekday> weekdays) {
final sorted = weekdays.toList()
..sort((a, b) => a.name.length.compareTo(b.name.length))
..sort((a, b) => a.number.compareTo(b.number));

return sorted.reversed;
}

Weekday? _expectWeekday(DateParsingParameters parameters) {
// TODO(gbassisp): allow weekday in any part of the string
// currently unsupported because some locales can have a conflict between
// month and weekday (e.g., "Mar" in French for Mardi and Mars)
final timestamp = parameters.formattedString.toLowerCase();
var weekday = parameters.parserInfo.weekdays
var weekday = _sortWeekdays(parameters.parserInfo.weekdays)
.where(
(element) => timestamp.startsWith(element.name.toLowerCase()),
// (element) => timestamp
Expand Down

0 comments on commit d2816ea

Please sign in to comment.