Skip to content

Commit

Permalink
enforce dartfmt rules
Browse files Browse the repository at this point in the history
  • Loading branch information
rinukkusu committed Nov 21, 2018
1 parent d2fe88a commit 618bb23
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 48 deletions.
13 changes: 0 additions & 13 deletions .analysis_options

This file was deleted.

2 changes: 1 addition & 1 deletion lib/simple_moment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export 'src/localedata.dart';
export 'src/locales/de.dart';
export 'src/locales/en.dart';
export 'src/locales/es.dart';
export 'src/identifier_position.dart';
export 'src/identifier_position.dart';
6 changes: 1 addition & 5 deletions lib/src/identifier_position.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
enum IdentifierPosition {
prepend,
append,
dontDisplay
}
enum IdentifierPosition { prepend, append, dontDisplay }
2 changes: 1 addition & 1 deletion lib/src/localedata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ abstract class ILocaleData {

IdentifierPosition get futurePosition;
IdentifierPosition get pastPosition;
}
}
2 changes: 1 addition & 1 deletion lib/src/locales/de.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ class LocaleDe implements ILocaleData {

IdentifierPosition get futurePosition => IdentifierPosition.prepend;
IdentifierPosition get pastPosition => IdentifierPosition.prepend;
}
}
2 changes: 1 addition & 1 deletion lib/src/locales/en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ class LocaleEn implements ILocaleData {

IdentifierPosition get futurePosition => IdentifierPosition.prepend;
IdentifierPosition get pastPosition => IdentifierPosition.append;
}
}
2 changes: 1 addition & 1 deletion lib/src/locales/es.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ class LocaleEs implements ILocaleData {

IdentifierPosition get futurePosition => IdentifierPosition.prepend;
IdentifierPosition get pastPosition => IdentifierPosition.prepend;
}
}
48 changes: 33 additions & 15 deletions lib/src/simple_moment_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,53 @@ class Moment {

var locale = _getLocale();

if (diff.inSeconds.abs() < 45) timeString = locale.seconds.replaceFirst('%i', '${diff.inSeconds.abs()}');
else if (diff.inMinutes.abs() < 2) timeString = locale.aMinute.replaceFirst('%i', '${diff.inMinutes.abs()}');
else if (diff.inMinutes.abs() < 45) timeString = locale.minutes.replaceFirst('%i','${diff.inMinutes.abs()}');
else if (diff.inHours.abs() < 2) timeString = locale.anHour.replaceFirst('%i', '${diff.inHours.abs()}');
else if (diff.inHours.abs() < 22) timeString = locale.hours.replaceFirst('%i', '${diff.inHours.abs()}');
else if (diff.inDays.abs() < 2) timeString = locale.aDay.replaceFirst('%i', '${diff.inDays.abs()}');
else if (diff.inDays.abs() < 26) timeString = locale.days.replaceFirst('%i', '${diff.inDays.abs()}');
else if (diff.inDays.abs() < 60) timeString = locale.aMonth.replaceFirst('%i', '${diff.inDays.abs() ~/ 30}');
else if (diff.inDays.abs() < 320) timeString = locale.months.replaceFirst('%i', '${diff.inDays.abs() ~/ 30}');
else if (diff.inDays.abs() < 547) timeString = locale.aYear.replaceFirst('%i', '${diff.inDays.abs() ~/ 356}');
else timeString = locale.years.replaceFirst('%i', '${diff.inDays.abs() ~/ 356}');
if (diff.inSeconds.abs() < 45)
timeString = locale.seconds.replaceFirst('%i', '${diff.inSeconds.abs()}');
else if (diff.inMinutes.abs() < 2)
timeString = locale.aMinute.replaceFirst('%i', '${diff.inMinutes.abs()}');
else if (diff.inMinutes.abs() < 45)
timeString = locale.minutes.replaceFirst('%i', '${diff.inMinutes.abs()}');
else if (diff.inHours.abs() < 2)
timeString = locale.anHour.replaceFirst('%i', '${diff.inHours.abs()}');
else if (diff.inHours.abs() < 22)
timeString = locale.hours.replaceFirst('%i', '${diff.inHours.abs()}');
else if (diff.inDays.abs() < 2)
timeString = locale.aDay.replaceFirst('%i', '${diff.inDays.abs()}');
else if (diff.inDays.abs() < 26)
timeString = locale.days.replaceFirst('%i', '${diff.inDays.abs()}');
else if (diff.inDays.abs() < 60)
timeString =
locale.aMonth.replaceFirst('%i', '${diff.inDays.abs() ~/ 30}');
else if (diff.inDays.abs() < 320)
timeString =
locale.months.replaceFirst('%i', '${diff.inDays.abs() ~/ 30}');
else if (diff.inDays.abs() < 547)
timeString =
locale.aYear.replaceFirst('%i', '${diff.inDays.abs() ~/ 356}');
else
timeString =
locale.years.replaceFirst('%i', '${diff.inDays.abs() ~/ 356}');

if (!withoutPrefixOrSuffix) {
if (diff.isNegative)
timeString = _addIdentifier(timeString, locale.pastPosition, locale.pastIdentifier);
timeString = _addIdentifier(
timeString, locale.pastPosition, locale.pastIdentifier);
else
timeString = _addIdentifier(timeString, locale.futurePosition, locale.futureIdentifier);
timeString = _addIdentifier(
timeString, locale.futurePosition, locale.futureIdentifier);
}

return timeString;
}

String _addIdentifier(String timeString, IdentifierPosition position, String identifier) {
String _addIdentifier(
String timeString, IdentifierPosition position, String identifier) {
switch (position) {
case IdentifierPosition.prepend:
return '${identifier} ${timeString}';
case IdentifierPosition.append:
return '${timeString} ${identifier}';

case IdentifierPosition.dontDisplay:
default:
return timeString;
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: simple_moment
description: A simple implementation of the Moment.js features for relative time.
version: 1.0.0
version: 1.0.1
author: rinukkusu <rinukkusu@auen.land>
homepage: https://github.com/rinukkusu
homepage: https://github.com/rinukkusu/simple_moment

environment:
sdk: '>=2.0.0 <3.0.0'
Expand Down
23 changes: 15 additions & 8 deletions test/simple_moment_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,38 @@ void main() {

DateTime currentDate = DateTime.parse("2016-12-31 12:00:00");

setUp(() {

});
setUp(() {});

test('static Moment.parse', () {
expect(Moment.parse(dateString).toString() == dateString, isTrue);
});

test('new Moment.fromDate', () {
expect(new Moment.fromDate(date).toString() == dateString, isTrue);
expect(new Moment.fromDate(date).toString() == dateString, isTrue);
});

test('Moment.from(future)', () {
DateTime compareDate = DateTime.parse("2016-12-31 12:00:30");
expect(new Moment.fromDate(currentDate).from(compareDate) == "in a few seconds", isTrue);
DateTime compareDate = DateTime.parse("2016-12-31 12:00:30");
expect(
new Moment.fromDate(currentDate).from(compareDate) ==
"in a few seconds",
isTrue);
});

test('Moment.from(past)', () {
DateTime compareDate = DateTime.parse("2016-12-31 11:59:30");
expect(new Moment.fromDate(currentDate).from(compareDate) == "a few seconds ago", isTrue);
expect(
new Moment.fromDate(currentDate).from(compareDate) ==
"a few seconds ago",
isTrue);
});

test('Moment.from(less than a second ago)', () {
DateTime compareDate = DateTime.parse("2016-12-31 11:59:59.001");
expect(new Moment.fromDate(currentDate).from(compareDate) == "a few seconds ago", isTrue);
expect(
new Moment.fromDate(currentDate).from(compareDate) ==
"a few seconds ago",
isTrue);
});
});
}

0 comments on commit 618bb23

Please sign in to comment.