Skip to content

Commit

Permalink
fix: moving ago() utility to DateTime class
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander Hoogendoorn committed Sep 15, 2024
1 parent 8d4a1d6 commit 461efc8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
5 changes: 5 additions & 0 deletions packages/easy/src/domain/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isDate } from '../types/IsDate';
import { isA } from '../types/IsA';
import { ifDefined } from '../utils/If';
import { JsonValue } from '../types/Json';
import { seconds } from '../utils/Seconds';

Settings.defaultZone = 'utc';

Expand Down Expand Up @@ -139,6 +140,10 @@ export class DateTime extends Value<Optional<string>> {
toDate(): Optional<Date> {
return this.isValid ? this.utc.toJSDate() : undefined;
}

ago(end: DateTime = DateTime.now): string {
return seconds.toText(end.diff(this, 'second'));
}
}

export const isDateTime = (dt?: unknown): dt is DateTime => isDefined(dt) && dt instanceof DateTime;
Expand Down
3 changes: 0 additions & 3 deletions packages/easy/src/utils/Seconds.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ifTrue } from './If';
import { toList } from '../types/List';
import { DateTime } from '../domain/DateTime';

export const seconds = {
toDuration: (s: number) => {
Expand All @@ -17,6 +16,4 @@ export const seconds = {
.mapDefined(s => s)
.join(' ');
},

ago: (start: DateTime, end: DateTime = DateTime.now) => seconds.toText(end.diff(start, 'second')),
};
12 changes: 12 additions & 0 deletions packages/easy/test/domain/DateTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,16 @@ describe('DateTime', () => {
expect(dt(iso).toJSON()).toMatchText(iso);
expect(dt(new_york).toJSON()).toMatchText(iso);
});

test('ago', () => {
const start = dt('2021-01-01T00:00:00Z');
const end = dt('2021-01-01T00:00:01Z');
expect(start.ago(end)).toBe('1s');
});

test('ago with default end', () => {
const start = dt('2021-01-06T00:00:00Z');
mock.property(DateTime, 'now', dt('2021-01-10T03:03:01Z'));
expect(start.ago()).toBe('4d 3h 3m');
});
});
16 changes: 1 addition & 15 deletions packages/easy/test/utils/Seconds.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { dt, seconds } from '../../src';
import { DateTime } from 'luxon';
import { mock } from '@thisisagile/easy-test';
import { seconds } from '../../src';

describe('Seconds', () => {
test('split', () => {
Expand All @@ -24,16 +22,4 @@ describe('Seconds', () => {
expect(seconds.toText(86461)).toBe('1d 1m');
expect(seconds.toText(90061)).toBe('1d 1h 1m');
});

test('ago', () => {
const start = dt('2021-01-01T00:00:00Z');
const end = dt('2021-01-01T00:00:01Z');
expect(seconds.ago(start, end)).toBe('1s');
});

test('ago with default end', () => {
DateTime.now = mock.return(dt('2021-01-03T03:03:01Z'));
const start = dt('2021-01-06T00:00:00Z');
expect(seconds.ago(start)).toBe('1348d 13h 13m');
});
});

0 comments on commit 461efc8

Please sign in to comment.