Skip to content

Commit

Permalink
Merge pull request #10 from gbassisp/fix/0.1-conflicts
Browse files Browse the repository at this point in the history
Fix/0.1 conflicts
  • Loading branch information
gbassisp authored Dec 15, 2024
2 parents 71ab144 + 2084b16 commit 1ec7c5d
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 137 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

## 0.1.16

- Fix conflicts with version 1.x

## 0.1.15

- Fix `dd-MMM-yy` format parsing
Expand Down
6 changes: 5 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ include: package:very_good_analysis/analysis_options.yaml

analyzer:
exclude:
- example/*
- example/*

linter:
rules:
use_late_for_private_fields_and_variables: false
22 changes: 11 additions & 11 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ extension DateTimeExtension on DateTime {

const _parser = AnyDate();

@internal
extension InternalIterableExtension<T extends Object> on Iterable<T?> {
Iterable<T> safeWhereNotNull() sync* {
for (final e in this) {
if (e != null) {
yield e;
}
}
}
}

/// a collection of extensions on [String]
@internal
extension StringParsers on String {
Expand Down Expand Up @@ -148,14 +159,3 @@ extension StringParsers on String {
return res;
}
}

@internal
extension InternalIterableExtension<T extends Object> on Iterable<T?> {
Iterable<T> safeWhereNotNull() sync* {
for (final e in this) {
if (e != null) {
yield e;
}
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: any_date
description: A package for parsing String into DateTime in any format. Easy way to parse a date in any format and in any language, while always respecting ISO and RFC formats.
version: 0.1.15
version: 0.1.16
repository: https://github.com/gbassisp/any_date
homepage: https://github.com/gbassisp/any_date

Expand Down
133 changes: 9 additions & 124 deletions test/locale_based_test.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import 'package:any_date/any_date.dart';
import 'package:any_date/src/extensions.dart';
import 'package:intl/date_symbol_data_file.dart'
show availableLocalesForDateFormatting;
import 'package:intl/date_symbol_data_local.dart';

import 'package:intl/intl.dart';
import 'package:intl/locale.dart';
import 'package:lean_extensions/dart_essentials.dart';
import 'package:test/test.dart';

import 'locale_test_implementation.dart';
import 'rfc_test.dart';

final _locales = availableLocalesForDateFormatting.map((e) => e).toList()
Expand All @@ -21,127 +19,14 @@ final _locales = availableLocalesForDateFormatting.map((e) => e).toList()
}
return false;
});
final _localeCodes = _locales.map(Locale.tryParse).safeWhereNotNull().toList();

extension _ListExtension<T> on Iterable<T> {
int? indexOf(T element) {
for (final i in range(length)) {
if (elementAt(i) == element) {
return i;
}
}

return null;
}
}

// TODO(gbassisp): promote this to lib/ once we enable support for locale
extension _LocaleExtensions on Locale {
AnyDate get anyDate => AnyDate(
info: DateParserInfo(
dayFirst: !usesMonthFirst,
yearFirst: usesYearFirst,
months: [...longMonths, ...shortMonths],
weekdays: [...longWeekdays, ...shortWeekdays],
),
);

static final _date = DateTime(1234, 5, 6, 7, 8, 9);

String get _yMd => DateFormat.yMd(toString()).format(_date);

// DateParserInfo get parserInfo => DateParserInfo(
// yearFirst: usesYearFirst,
// dayFirst: !usesMonthFirst,
// months: [...longMonths, ...shortMonths],
// weekdays: [...longWeekdays, ...shortWeekdays],
// );

bool get usesNumericSymbols {
try {
usesMonthFirst;
usesYearFirst;
return true;
} catch (_) {
return false;
}
}

bool get usesMonthFirst {
final formatted = _yMd;
final fields = formatted.split(RegExp(r'\D'))
..removeWhere((element) => element.trim().tryToInt() == null);
final numbers = fields.map((e) => e.toInt());

assert(
numbers.contains(5),
'could not find test date in $this: $formatted',
);

final monthIndex = numbers.indexOf(5);
final dayIndex = numbers.indexOf(6);

assert(
monthIndex != null && dayIndex != null,
'month and day must both be present in $this: $formatted',
);
return monthIndex! < dayIndex!;
}

bool get usesYearFirst {
final formatted = _yMd;
final fields = formatted.split(RegExp(r'\D'))
..removeWhere((element) => element.trim().tryToInt() == null);
final numbers = fields.map((e) => e.toInt());

assert(
numbers.contains(1234),
'could not find test date in $this: $formatted',
);

final yearIndex = numbers.indexOf(1234);
final monthIndex = numbers.indexOf(5);

assert(
yearIndex != null && monthIndex != null,
'month and year must both be present in $this: $formatted',
);
return yearIndex! < monthIndex!;
}

Iterable<Month> get longMonths sync* {
final format = DateFormat('MMMM', toString());
for (final i in range(12)) {
final m = i + 1;
final d = DateTime(1234, m, 10);
yield Month(number: m, name: format.format(d));
}
}

Iterable<Month> get shortMonths sync* {
final format = DateFormat('MMM', toString());
for (final i in range(12)) {
final m = i + 1;
final d = DateTime(1234, m, 10);
yield Month(number: m, name: format.format(d));
}
}

Iterable<Weekday> get longWeekdays sync* {
final format = DateFormat('EEEE', toString());
for (final i in range(7)) {
final w = i + 1;
final d = DateTime(2023, 10, 8 + w);
yield Weekday(number: w, name: format.format(d));
}
}

Iterable<Weekday> get shortWeekdays sync* {
final format = DateFormat('EEE', toString());
for (final i in range(7)) {
final w = i + 1;
final d = DateTime(2023, 10, 8 + w);
yield Weekday(number: w, name: format.format(d));
final _localeCodes = _locales.map(Locale.tryParse).whereIsNotNull().toList();

/// taken from collection package to avoid deprecation warning and conflict
/// with dart sdk
extension _IterableNullableExtension<T extends Object> on Iterable<T?> {
Iterable<T> whereIsNotNull() sync* {
for (final element in this) {
if (element != null) yield element;
}
}
}
Expand Down
128 changes: 128 additions & 0 deletions test/locale_test_implementation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// TODO(gbassisp): promote this to lib/ once we enable support for locale
import 'package:any_date/any_date.dart';
import 'package:any_date/src/extensions.dart';
import 'package:intl/intl.dart';
import 'package:intl/locale.dart';
import 'package:lean_extensions/dart_essentials.dart';

extension LocaleExtensions on Locale {
AnyDate get anyDate => AnyDate(
info: DateParserInfo(
dayFirst: !usesMonthFirst,
yearFirst: usesYearFirst,
months: [...longMonths, ...shortMonths],
weekdays: [...longWeekdays, ...shortWeekdays],
),
);

static final _date = DateTime(1234, 5, 6, 7, 8, 9);

String get _yMd => DateFormat.yMd(toString()).format(_date);

// DateParserInfo get parserInfo => DateParserInfo(
// yearFirst: usesYearFirst,
// dayFirst: !usesMonthFirst,
// months: [...longMonths, ...shortMonths],
// weekdays: [...longWeekdays, ...shortWeekdays],
// );

bool get usesNumericSymbols {
try {
usesMonthFirst;
usesYearFirst;
return true;
} catch (_) {
return false;
}
}

bool get usesMonthFirst {
final formatted = _yMd;
final fields = formatted.split(RegExp(r'\D'))
..removeWhere((element) => element.trim().tryToInt() == null);
final numbers = fields.map((e) => e.toInt());

assert(
numbers.contains(5),
'could not find test date in $this: $formatted',
);

final monthIndex = numbers.indexOf(5);
final dayIndex = numbers.indexOf(6);

assert(
monthIndex != null && dayIndex != null,
'month and day must both be present in $this: $formatted',
);
return monthIndex! < dayIndex!;
}

bool get usesYearFirst {
final formatted = _yMd;
final fields = formatted.split(RegExp(r'\D'))
..removeWhere((element) => element.trim().tryToInt() == null);
final numbers = fields.map((e) => e.toInt());

assert(
numbers.contains(1234),
'could not find test date in $this: $formatted',
);

final yearIndex = numbers.indexOf(1234);
final monthIndex = numbers.indexOf(5);

assert(
yearIndex != null && monthIndex != null,
'month and year must both be present in $this: $formatted',
);
return yearIndex! < monthIndex!;
}

Iterable<Month> get longMonths sync* {
final format = DateFormat('MMMM', toString());
for (final i in range(12)) {
final m = i + 1;
final d = DateTime(1234, m, 10);
yield Month(number: m, name: format.format(d));
}
}

Iterable<Month> get shortMonths sync* {
final format = DateFormat('MMM', toString());
for (final i in range(12)) {
final m = i + 1;
final d = DateTime(1234, m, 10);
yield Month(number: m, name: format.format(d));
}
}

Iterable<Weekday> get longWeekdays sync* {
final format = DateFormat('EEEE', toString());
for (final i in range(7)) {
final w = i + 1;
final d = DateTime(2023, 10, 8 + w);
yield Weekday(number: w, name: format.format(d));
}
}

Iterable<Weekday> get shortWeekdays sync* {
final format = DateFormat('EEE', toString());
for (final i in range(7)) {
final w = i + 1;
final d = DateTime(2023, 10, 8 + w);
yield Weekday(number: w, name: format.format(d));
}
}
}

extension _ListExtension<T> on Iterable<T> {
int? indexOf(T element) {
for (final i in range(length)) {
if (elementAt(i) == element) {
return i;
}
}

return null;
}
}

0 comments on commit 1ec7c5d

Please sign in to comment.