Skip to content

Commit

Permalink
introduce stricter linting and better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubkrapiec committed Apr 1, 2021
1 parent c0fe1a6 commit 1816742
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,9 +1,9 @@
.dart_tool/
.packages
pubspec.lock
build/
doc/api/
.vscode/
idea/
test/.test_coverage.dart
coverage_badge.svg
coverage
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,9 @@
## 3.0.0+2

- Improved performance.
- Added support for package development in Android Studio in .gitignore.
- Introduced stricter internal linting.

## 3.0.0+1

- Improved README formatting.
Expand Down
10 changes: 10 additions & 0 deletions analysis_options.yaml
@@ -1 +1,11 @@
include: package:pedantic/analysis_options.yaml

analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false

linter:
rules:
prefer_const_constructors: true
lines_longer_than_80_chars: false
30 changes: 9 additions & 21 deletions lib/src/dateable_base.dart
Expand Up @@ -4,8 +4,7 @@ import 'package:meta/meta.dart';
@immutable
class Date implements Comparable<Date> {
/// Converts a [DateTime] to a [Date].
Date.fromDateTime(final DateTime dateTime)
: _date = _truncateTimeOfDay(dateTime);
Date.fromDateTime(final DateTime dateTime) : _date = _truncateTimeOfDay(dateTime);

/// Returns [DateTime] object with the date of [this] and time values at 00:00.
DateTime toDateTime() => _date;
Expand All @@ -26,18 +25,15 @@ class Date implements Comparable<Date> {
}

/// Get rid of the given [DateTime]'s time of day data, leaving it all zeroes.
static DateTime _truncateTimeOfDay(final DateTime dateTime) =>
DateTime(dateTime.year, dateTime.month, dateTime.day);
static DateTime _truncateTimeOfDay(final DateTime dateTime) => DateTime(dateTime.year, dateTime.month, dateTime.day);

/// Creates a [Date] object from day, month and year values. Performs validation.
Date(final int day, final int month, final int year)
: _date = DateTime(year, month, day);
Date(final int day, final int month, final int year) : _date = DateTime(year, month, day);

/// Parses given [String] to a new [Date] object. Besides ISO 8601 works with
/// every format of [String] that [DateTime.parse] would work with.
/// Performs validation. Throws [FormatException] when the input can't be parsed.
Date.parseIso8601(final String dateString)
: _date = _truncateTimeOfDay(DateTime.parse(dateString));
Date.parseIso8601(final String dateString) : _date = _truncateTimeOfDay(DateTime.parse(dateString));

/// Returns an ISO8601 [String] representing [this].
/// ISO8601 in this case means: yyyy-mm-ddT00:00:00.000000
Expand All @@ -49,15 +45,12 @@ class Date implements Comparable<Date> {
/// Parses [String] to [Date] object. [String] must be formatted as ddmmyyyy.
/// Performs validation. Throws [FormatException] when argument contains non-numbers.
Date.parse(final String dateString)
: _date = DateTime(
int.parse(dateString.substring(4, 8)),
int.parse(dateString.substring(2, 4)),
: _date = DateTime(int.parse(dateString.substring(4, 8)), int.parse(dateString.substring(2, 4)),
int.parse(dateString.substring(0, 2)));

/// Returns a new [Date] with given amount of days subtracted from [this].
/// [days] can be negative, in this case addition will happen.
Date subtractDays(final int days) =>
_date.subtract(Duration(days: days)).toDate();
Date subtractDays(final int days) => _date.subtract(Duration(days: days)).toDate();

/// Returns a new [Date] with given amount of days added to [this].
/// [days] can be negative, in this case subtraction will happen.
Expand All @@ -76,10 +69,7 @@ class Date implements Comparable<Date> {

@override
bool operator ==(final dynamic other) =>
other is Date &&
other.day == _date.day &&
other.month == _date.month &&
other.year == _date.year;
other is Date && other.day == _date.day && other.month == _date.month && other.year == _date.year;

/// Checks if [this] is after [other].
bool operator >(final Date other) => _date.isAfter(other.toDateTime());
Expand All @@ -106,12 +96,10 @@ class Date implements Comparable<Date> {
Date.today() : _date = _truncateTimeOfDay(DateTime.now());

/// Returns tomorrow's date.
Date.tomorrow()
: _date = _truncateTimeOfDay(DateTime.now().add(Duration(days: 1)));
Date.tomorrow() : _date = _truncateTimeOfDay(DateTime.now().add(const Duration(days: 1)));

/// Returns yesterday's date.
Date.yesterday()
: _date = _truncateTimeOfDay(DateTime.now().subtract(Duration(days: 1)));
Date.yesterday() : _date = _truncateTimeOfDay(DateTime.now().subtract(const Duration(days: 1)));

/// Returns day of the date represented by this object. Always in range [1; 31].
int get day => _date.day;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,6 +1,6 @@
name: dateable
description: A Dart package to help you with managing dates easily. Can be used to store, format, convert, construct, parse and serialise dates.
version: 3.0.0+1
version: 3.0.0+2
repository: https://github.com/SugaR256/dateable
homepage: https://github.com/SugaR256/dateable

Expand Down
32 changes: 11 additions & 21 deletions test/dateable_test.dart
Expand Up @@ -4,21 +4,19 @@ import 'package:test/test.dart';
void main() {
group('Constructor tests', () {
test('Date.fromDateTime(DateTime) constructor test', () {
expect(
Date.fromDateTime(DateTime(2002, 3, 11)), equals(Date(11, 3, 2002)));
expect(Date.fromDateTime(DateTime(2002, 3, 11)), equals(Date(11, 3, 2002)));
});
test('Date(int, int, int) date validation test', () {
expect(Date(50, 40, 2000), equals(Date(20, 5, 2003)));
});
test('Date.parseIso8601(String) constructor test', () {
expect(Date.parseIso8601('2002-03-11T14:33:21.125'),
equals(Date(11, 3, 2002)));
expect(Date.parseIso8601('2002-03-11T14:33:21.125'), equals(Date(11, 3, 2002)));
});
test('Date.parse(String) constructor test', () {
expect(Date.parse('11032002'), equals(Date(11, 3, 2002)));
});
test('Date.parse(String) constructor exception test', () {
var exception;
dynamic exception;
try {
Date.parse('1103207X');
} catch (e) {
Expand All @@ -30,12 +28,10 @@ void main() {
expect(Date.today(), equals(DateTime.now().toDate()));
});
test('tomorrow constructor test', () {
expect(Date.tomorrow(),
equals(DateTime.now().add(Duration(days: 1)).toDate()));
expect(Date.tomorrow(), equals(DateTime.now().add(const Duration(days: 1)).toDate()));
});
test('yesterday constructor test', () {
expect(Date.yesterday(),
equals(DateTime.now().subtract(Duration(days: 1)).toDate()));
expect(Date.yesterday(), equals(DateTime.now().subtract(const Duration(days: 1)).toDate()));
});
});
group('Comparision tests', () {
Expand Down Expand Up @@ -94,16 +90,13 @@ void main() {
expect(Date(11, 3, 2002).isAfter(Date(11, 3, 2002)), equals(false));
});
test('copyWith test', () {
expect(Date(11, 3, 2002).copyWith(day: 21, month: 9),
equals(Date(21, 9, 2002)));
expect(Date(11, 3, 2002).copyWith(day: 21, month: 9), equals(Date(21, 9, 2002)));
});
test('isTheSameDate(Date) DateTime extension positive test', () {
expect(DateTime(2002, 3, 11, 14, 32, 56).isTheSameDate(Date(11, 3, 2002)),
equals(true));
expect(DateTime(2002, 3, 11, 14, 32, 56).isTheSameDate(Date(11, 3, 2002)), equals(true));
});
test('isTheSameDate(Date) DateTime extension negative test', () {
expect(DateTime(2004, 9, 21, 18, 26).isTheSameDate(Date(11, 3, 2002)),
equals(false));
expect(DateTime(2004, 9, 21, 18, 26).isTheSameDate(Date(11, 3, 2002)), equals(false));
});
test('isTheSameDate(Date) positive test', () {
expect(Date(11, 3, 2002).isTheSameDate(Date(11, 3, 2002)), equals(true));
Expand All @@ -115,19 +108,16 @@ void main() {
expect(DateTime.now().toDate().isToday(), equals(true));
});
test('isToday() negative test', () {
expect(DateTime.now().add(Duration(days: 1)).toDate().isToday(),
equals(false));
expect(DateTime.now().add(const Duration(days: 1)).toDate().isToday(), equals(false));
});
test('isTomorrow() positive test', () {
expect(DateTime.now().add(Duration(days: 1)).toDate().isTomorrow(),
equals(true));
expect(DateTime.now().add(const Duration(days: 1)).toDate().isTomorrow(), equals(true));
});
test('isTomorrow() negative test', () {
expect(DateTime.now().toDate().isTomorrow(), equals(false));
});
test('isYesterday() positive test', () {
expect(DateTime.now().subtract(Duration(days: 1)).toDate().isYesterday(),
equals(true));
expect(DateTime.now().subtract(const Duration(days: 1)).toDate().isYesterday(), equals(true));
});
test('isYesterday() negative test', () {
expect(DateTime.now().toDate().isYesterday(), equals(false));
Expand Down

0 comments on commit 1816742

Please sign in to comment.