Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-163] Option to omit prefix and suffix from relative datetime functions #218

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions doc/README.md
Expand Up @@ -396,6 +396,12 @@ Jiffy.parseFromDateTime(DateTime(2050, 10, 29)).fromNow(); // in 30 years
Jiffy.now().startOf(Unit.hour).fromNow(); // 9 minutes ago
```

You can also omit returning the prefix and suffix like `in` and `ago`.

```dart
Jiffy.parse('2007-01-29').fromNow(withPrefixAndSuffix: false); // 14 years
```

### Time from X
This method is used to get the relative time from a specific date time.
```dart
Expand All @@ -405,6 +411,15 @@ var jiffy2 = Jiffy.parse('2017-01-29');
jiffy1.from(jiffy2); // a day ago
```

You can also omit returning the prefix and suffix like `in` and `ago`.

```dart
var jiffy1 = Jiffy.parse('2007-01-28');
var jiffy2 = Jiffy.parse('2017-01-29');

jiffy1.from(jiffy2, withPrefixAndSuffix: false); // a day
```

### Time to Now
This works opposite to the `fromNow()` method.
This method is used to get the relative time to now.
Expand All @@ -416,6 +431,12 @@ Jiffy.parseFromDateTime(DateTime(2050, 10, 29)).toNow(); // 30 years ago
Jiffy.now().startOf(Unit.hour).toNow(); // in 9 minutes
```

You can also omit returning the prefix and suffix like `in` and `ago`.

```dart
Jiffy.parse('2007-01-29').toNow(withPrefixAndSuffix: false); // 14 years
```

### Time to X
This works opposite to the `from()` method.
This method is used to get the relative time to a specific date time.
Expand All @@ -426,6 +447,14 @@ var jiffy2 = Jiffy.parse('2017-01-29');
jiffy1.to(jiffy2); // in a day
```

You can also omit returning the prefix and suffix like `in` and `ago`.

```dart
var jiffy1 = Jiffy.parse('2007-01-28');
var jiffy2 = Jiffy.parse('2017-01-29');

jiffy1.to(jiffy2, withPrefixAndSuffix: false); // a day
```
### Difference
Used to get the difference between two Jiffy date times.
```dart
Expand Down
21 changes: 13 additions & 8 deletions lib/src/display.dart
Expand Up @@ -34,8 +34,8 @@ class Display {
}
}

String fromAsRelativeDateTime(
DateTime firstDateTime, DateTime secondDateTime, Locale locale) {
String fromAsRelativeDateTime(DateTime firstDateTime, DateTime secondDateTime,
Locale locale, bool withPrefixAndSuffix) {
final isFirstDateTimeSameOrAfterSecondDateTime = _query.isSameOrAfter(
firstDateTime, secondDateTime, Unit.microsecond, locale.startOfWeek());

Expand Down Expand Up @@ -85,14 +85,19 @@ class Display {
result = relativeDateTime.years(years.round());
}

return [prefix, result, suffix]
.where((str) => str.isNotEmpty)
.join(relativeDateTime.wordSeparator());
if (withPrefixAndSuffix) {
return [prefix, result, suffix]
.where((str) => str.isNotEmpty)
.join(relativeDateTime.wordSeparator());
}

return result;
}

String toAsRelativeDateTime(
DateTime firstDateTime, DateTime secondDateTime, Locale locale) {
return fromAsRelativeDateTime(secondDateTime, firstDateTime, locale);
String toAsRelativeDateTime(DateTime firstDateTime, DateTime secondDateTime,
Locale locale, bool withPrefixAndSuffix) {
return fromAsRelativeDateTime(
secondDateTime, firstDateTime, locale, withPrefixAndSuffix);
}

num diff(DateTime firstDateTime, DateTime secondDateTime, Unit unit,
Expand Down
78 changes: 68 additions & 10 deletions lib/src/jiffy.dart
Expand Up @@ -671,7 +671,8 @@ class Jiffy {
String get jms => _defaultDisplay.jms(dateTime);

/// Returns a string representation of current [Jiffy]'s instance relative
/// from [jiffy]'s date and time.
/// from [jiffy]'s date and time, with optional [withPrefixAndSuffix] flag
/// to include prefix and suffix like "in" or "ago".
///
/// Example:
///
Expand All @@ -681,12 +682,27 @@ class Jiffy {
/// print(jiffy1.from(jiffy2));
/// // output: a month ago
/// ```
String from(Jiffy jiffy) {
return _display.fromAsRelativeDateTime(dateTime, jiffy.dateTime, _locale);
///
/// Example without returning prefix and suffix:
///
/// ```dart
/// final jiffy1 = Jiffy.parseFromDateTime(DateTime(2023, 1, 1));
/// final jiffy2 = Jiffy.parseFromDateTime(DateTime(2023, 2, 1));
/// print(jiffy1.from(jiffy2, withPrefixAndSuffix: false));
/// // output: a month
/// ```
///
/// If [withPrefixAndSuffix] is `true` (default), the output string will
/// include prefix and suffix words like "in" or "ago". Otherwise, only
/// the relative time difference string will be returned.
String from(Jiffy jiffy, {bool withPrefixAndSuffix = true}) {
return _display.fromAsRelativeDateTime(
dateTime, jiffy.dateTime, _locale, withPrefixAndSuffix);
}

/// Returns a string representation of current [Jiffy]'s instance relative
/// from current date and time.
/// from current date and time, with optional [withPrefixAndSuffix] flag
/// to include prefix and suffix like "in" or "ago".
///
/// Example:
///
Expand All @@ -695,10 +711,24 @@ class Jiffy {
/// print(jiffy.fromNow());
/// // output: a month ago
/// ```
String fromNow() => from(Jiffy.now());
///
/// Example without returning prefix and suffix:
///
/// ```dart
/// final jiffy = Jiffy.parseFromDateTime(DateTime(2023, 1, 1));
/// print(jiffy.fromNow(withPrefixAndSuffix: false));
/// // output: a month
/// ```
///
/// If [withPrefixAndSuffix] is `true` (default), the output string will
/// include prefix and suffix words like "in" or "ago". Otherwise, only
/// the relative time difference string will be returned.
String fromNow({bool withPrefixAndSuffix = true}) =>
from(Jiffy.now(), withPrefixAndSuffix: withPrefixAndSuffix);

/// Returns a string representation of current [Jiffy]'s instance relative
/// to [jiffy]'s date and time.
/// to [jiffy]'s date and time, with optional [withPrefixAndSuffix] flag
/// to include prefix and suffix like "in" or "ago".
///
/// Example:
///
Expand All @@ -708,12 +738,27 @@ class Jiffy {
/// print(jiffy1.to(jiffy2));
/// // output: in a month
/// ```
String to(Jiffy jiffy) {
return _display.toAsRelativeDateTime(dateTime, jiffy.dateTime, _locale);
///
/// Example without returning prefix and suffix:
///
/// ```dart
/// final jiffy1 = Jiffy.parseFromDateTime(DateTime(2023, 1, 1));
/// final jiffy2 = Jiffy.parseFromDateTime(DateTime(2023, 2, 1));
/// print(jiffy1.to(jiffy2, withPrefixAndSuffix: false));
/// // output: a month
/// ```
///
/// If [withPrefixAndSuffix] is `true` (default), the output string will
/// include prefix and suffix words like "in" or "ago". Otherwise, only
/// the relative time difference string will be returned.
String to(Jiffy jiffy, {bool withPrefixAndSuffix = true}) {
return _display.toAsRelativeDateTime(
dateTime, jiffy.dateTime, _locale, withPrefixAndSuffix);
}

/// Returns a string representation of current [Jiffy]'s instance relative
/// to current date and time.
/// to current date and time, with optional [withPrefixAndSuffix] flag
/// to include prefix and suffix like "in" or "ago".
///
/// Example:
///
Expand All @@ -722,7 +767,20 @@ class Jiffy {
/// print(jiffy.toNow());
/// // output: in a month
/// ```
String toNow() => to(Jiffy.now());
///
/// Example without returning prefix and suffix:
///
/// ```dart
/// final jiffy = Jiffy.parseFromDateTime(DateTime(2023, 1, 1));
/// print(jiffy.toNow(withPrefixAndSuffix: false));
/// // output: a month
/// ```
///
/// If [withPrefixAndSuffix] is `true` (default), the output string will
/// include prefix and suffix words like "in" or "ago". Otherwise, only
/// the relative time difference string will be returned.
String toNow({bool withPrefixAndSuffix = true}) =>
to(Jiffy.now(), withPrefixAndSuffix: withPrefixAndSuffix);

/// Returns the difference between this [Jiffy] instance and the given
/// [jiffy] instance as a numeric value.
Expand Down
42 changes: 40 additions & 2 deletions test/src/display_test.dart
Expand Up @@ -110,24 +110,62 @@ void main() {
final locale = EnLocale();

final actualFromAsRelativeDateTime = underTest.fromAsRelativeDateTime(
testData['firstDateTime'], testData['secondDateTime'], locale);
testData['firstDateTime'],
testData['secondDateTime'],
locale,
true);

expect(actualFromAsRelativeDateTime,
testData['expectedFromAsRelativeDateTime']);
});
}

test(
'Should successfully get from as relative datetime without prefix and suffix',
() {
final locale = EnLocale();
final firstDateTime = DateTime(1997, 10, 23, 12, 11, 22, 123, 456);
final secondDateTime = DateTime(1998, 10, 23, 12, 11, 22, 123, 457);
final withPrefixAndSuffix = false;

final expectedFromAsRelativeDateTime = 'a year';

final actualFromAsRelativeDateTime = underTest.fromAsRelativeDateTime(
firstDateTime, secondDateTime, locale, withPrefixAndSuffix);

expect(actualFromAsRelativeDateTime, expectedFromAsRelativeDateTime);
});

for (var testData in toAsRelativeDateTimeTestData()) {
test('Should successfully get to as relative datetime', () {
final locale = EnLocale();

final actualToAsRelativeDateTime = underTest.toAsRelativeDateTime(
testData['firstDateTime'], testData['secondDateTime'], locale);
testData['firstDateTime'],
testData['secondDateTime'],
locale,
true);

expect(actualToAsRelativeDateTime,
testData['expectedToAsRelativeDateTime']);
});
}

test(
'Should successfully get to as relative datetime without prefix and suffix',
() {
final locale = EnLocale();
final firstDateTime = DateTime(1997, 10, 23, 12, 11, 22, 123, 456);
final secondDateTime = DateTime(1998, 10, 23, 12, 11, 22, 123, 457);
final withPrefixAndSuffix = false;

final expectedFromAsRelativeDateTime = 'a year';

final actualFromAsRelativeDateTime = underTest.toAsRelativeDateTime(
firstDateTime, secondDateTime, locale, withPrefixAndSuffix);

expect(actualFromAsRelativeDateTime, expectedFromAsRelativeDateTime);
});
});

group('Test diff between two datetime', () {
Expand Down
66 changes: 66 additions & 0 deletions test/src/jiffy_test.dart
Expand Up @@ -763,6 +763,23 @@ void main() {
expect(actualRelativeDateTime, expectedRelativeDateTime);
});

test(
'Should successfully get relative date time from without prefix and suffix',
() {
// Setup
final underTest = Jiffy.parseFromList([1997]);
final jiffyFrom = Jiffy.parseFromList([2022]);

final expectedRelativeDateTime = '25 years';

// Execute
final actualRelativeDateTime =
underTest.from(jiffyFrom, withPrefixAndSuffix: false);

// Verify
expect(actualRelativeDateTime, expectedRelativeDateTime);
});

test('Should successfully get relative date time fromNow', () {
// Setup
final underTest = Jiffy.now().subtract(years: 25);
Expand All @@ -776,6 +793,22 @@ void main() {
expect(actualRelativeDateTime, expectedRelativeDateTime);
});

test(
'Should successfully get relative date time fromNow without prefix and suffix',
() {
// Setup
final underTest = Jiffy.now().subtract(years: 25);

final expectedRelativeDateTime = '25 years';

// Execute
final actualRelativeDateTime =
underTest.fromNow(withPrefixAndSuffix: false);

// Verify
expect(actualRelativeDateTime, expectedRelativeDateTime);
});

test('Should successfully get relative date time to', () {
// Setup
final underTest = Jiffy.parseFromList([1997]);
Expand All @@ -790,6 +823,23 @@ void main() {
expect(actualRelativeDateTime, expectedRelativeDateTime);
});

test(
'Should successfully get relative date time to without prefix and suffix',
() {
// Setup
final underTest = Jiffy.parseFromList([1997]);
final jiffyTo = Jiffy.parseFromList([2022]);

final expectedRelativeDateTime = '25 years';

// Execute
final actualRelativeDateTime =
underTest.to(jiffyTo, withPrefixAndSuffix: false);

// Verify
expect(actualRelativeDateTime, expectedRelativeDateTime);
});

test('Should successfully get relative date time toNow', () {
// Setup
final underTest = Jiffy.now().subtract(years: 25);
Expand All @@ -802,6 +852,22 @@ void main() {
// Verify
expect(actualRelativeDateTime, expectedRelativeDateTime);
});

test(
'Should successfully get relative date time toNow without prefix and suffix',
() {
// Setup
final underTest = Jiffy.now().subtract(years: 25);

final expectedRelativeDateTime = '25 years';

// Execute
final actualRelativeDateTime =
underTest.toNow(withPrefixAndSuffix: false);

// Verify
expect(actualRelativeDateTime, expectedRelativeDateTime);
});
});

group('Test displaying diff', () {
Expand Down
3 changes: 2 additions & 1 deletion test/src/locale/locales/locales_test.dart
Expand Up @@ -61,7 +61,8 @@ void main() {
final actualRelativeDateTime = display.toAsRelativeDateTime(
testData['firstDateTime'],
testData['secondDateTime'],
testData['locale']);
testData['locale'],
true);

// Verify
expect(actualRelativeDateTime, testData['expectedRelativeDateTime']);
Expand Down