Skip to content

Commit 999fb0d

Browse files
feat: TT-38 refactoring pipe with a new way of displaying the difference (#678)
1 parent f5f8b90 commit 999fb0d

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/app/modules/shared/pipes/substract-date/substract-date.pipe.spec.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ describe('SubstractDatePipe', () => {
77
});
88

99
it('returns the date diff using hh:mm', () => {
10-
const fromDate = new Date('2011-04-11T10:20:40Z');
11-
const substractDate = new Date('2011-04-11T08:00:30Z');
12-
13-
const diff = new SubstractDatePipe().transform(fromDate, substractDate);
14-
15-
expect(diff).toBe('02:20');
10+
[
11+
{ endDate: '2021-04-11T10:20:00Z', startDate: '2021-04-11T08:00:00Z', expectedDiff: '02:20' },
12+
{ endDate: '2021-04-11T17:40:00Z', startDate: '2021-04-11T17:10:00Z', expectedDiff: '00:30' },
13+
{ endDate: '2021-04-11T18:18:00Z', startDate: '2021-04-11T18:00:00Z', expectedDiff: '00:18' },
14+
{ endDate: '2021-04-12T12:18:00Z', startDate: '2021-04-11T10:00:00Z', expectedDiff: '26:18' },
15+
{ endDate: '2021-04-12T10:01:00Z', startDate: '2021-04-12T10:00:00Z', expectedDiff: '00:01' },
16+
{ endDate: '2021-04-11T11:27:00Z', startDate: '2021-04-11T10:03:45Z', expectedDiff: '01:24' },
17+
].forEach(({ startDate, endDate, expectedDiff }) => {
18+
const fromDate = new Date(endDate);
19+
const substractDate = new Date(startDate);
20+
21+
const diff = new SubstractDatePipe().transform(fromDate, substractDate);
22+
23+
expect(diff).toBe(expectedDiff);
24+
});
1625
});
1726

1827
it('returns the date diff using hh:mm:ss for a diff < 1 min when displaySeconds is true', () => {
@@ -43,8 +52,8 @@ describe('SubstractDatePipe', () => {
4352
});
4453

4554
it('returns --:-- if substractDate is null', () => {
46-
const substractDate = null;
4755
const fromDate = new Date('2011-04-11T08:00:30Z');
56+
const substractDate = null;
4857

4958
const diff = new SubstractDatePipe().transform(fromDate, substractDate);
5059

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
11
import { NumberFormatter } from './../../formatters/number.formatter';
22
import { Pipe, PipeTransform } from '@angular/core';
33
import * as moment from 'moment';
4-
import { DATE_FORMAT_YEAR } from 'src/environments/environment';
54
@Pipe({
65
name: 'substractDate'
76
})
87
export class SubstractDatePipe implements PipeTransform {
98

109
transform(fromDate: Date, substractDate: Date, displaySeconds: boolean = false): string {
1110

12-
if (fromDate === null || substractDate === null ) {
11+
if (fromDate === null || substractDate === null) {
1312
return '--:--';
1413
}
1514

16-
const startDate = moment(substractDate, `${DATE_FORMAT_YEAR} HH:mm:ss`);
17-
let endDate = moment(fromDate, `${DATE_FORMAT_YEAR} HH:mm:ss`);
18-
let duration: moment.Duration = moment.duration(endDate.diff(startDate));
15+
const startDate = moment(substractDate);
16+
const endDate = moment(fromDate);
17+
let duration = this.getTimeDifference(startDate, endDate);
18+
const lessThanMinute = duration.asSeconds() < 60 || displaySeconds;
1919

20-
if (duration.asSeconds() > 60 && !displaySeconds) {
21-
endDate = endDate.add(1, 'minute').startOf('minute');
22-
duration = moment.duration(endDate.diff(startDate));
23-
return `${ this.formatTime(duration.hours())}:${this.formatTime(duration.minutes()) }`;
24-
} else {
25-
return `${ this.formatTime(duration.hours())}:${this.formatTime(duration.minutes())}:${this.formatTime(duration.seconds())}`;
20+
if (!lessThanMinute) {
21+
const newStartDate = startDate.clone().seconds(0).milliseconds(0);
22+
const newEndDate = endDate.clone().seconds(0).milliseconds(0);
23+
duration = this.getTimeDifference(newStartDate, newEndDate);
2624
}
25+
26+
return this.displayDifference(duration, lessThanMinute);
27+
}
28+
29+
getTimeDifference(substractDate: moment.Moment, fromDate: moment.Moment): moment.Duration {
30+
return moment.duration(fromDate.diff(substractDate));
31+
}
32+
33+
displayDifference(duration: moment.Duration, lessThanMinute) {
34+
const daysInHours = duration.days() >= 1 ? duration.days() * 24 : 0;
35+
const hours = this.formatTime(duration.hours() + daysInHours);
36+
const minutes = this.formatTime(duration.minutes());
37+
const seconds = lessThanMinute ? `:${this.formatTime(duration.seconds())}` : '';
38+
return `${hours}:${minutes}${seconds}`;
2739
}
2840

2941
formatTime(time: number): string {
3042
return new NumberFormatter(time).getAsAtLeastTwoDigitString();
3143
}
32-
3344
}

0 commit comments

Comments
 (0)