Skip to content

Commit

Permalink
fix: addings seconds ago() utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander Hoogendoorn committed Sep 15, 2024
1 parent 5640da7 commit 8d4a1d6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/easy/src/utils/Seconds.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ifTrue } from './If';
import { toList } from '../types/List';
import { DateTime } from '../domain/DateTime';

export const seconds = {
toDuration: (s: number) => {
Expand All @@ -10,10 +11,12 @@ export const seconds = {
return { days, hours, minutes, seconds };
},

toText: (s: number) => {
toText: (s: number): string => {
const { days, hours, minutes, seconds: secs } = seconds.toDuration(s);
return toList(ifTrue(days, `${days}d`), ifTrue(hours, `${hours}h`), ifTrue(minutes, `${minutes}m`), ifTrue(days + hours + minutes === 0, `${secs}s`))
.mapDefined(s => s)
.join(' ');
},

ago: (start: DateTime, end: DateTime = DateTime.now) => seconds.toText(end.diff(start, 'second')),
};
16 changes: 15 additions & 1 deletion packages/easy/test/utils/Seconds.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { seconds } from '../../src';
import { dt, seconds } from '../../src';
import { DateTime } from 'luxon';
import { mock } from '@thisisagile/easy-test';

describe('Seconds', () => {
test('split', () => {
Expand All @@ -22,4 +24,16 @@ 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 8d4a1d6

Please sign in to comment.