Skip to content

Commit 2f5ace1

Browse files
authored
Merge pull request #3673 from openstax/fix/interval
improve humanized, show only most significant duration
2 parents 87a216f + c943a33 commit 2f5ace1

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

shared/specs/model/time.spec.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Time, { setNow, setResolution } from '../../src/model/time'
1+
import Time, { Interval, setNow, setResolution } from '../../src/model/time'
22
import { autorun } from 'mobx'
33
import moment from 'moment'
44

@@ -57,13 +57,40 @@ describe('time class', () => {
5757
expect(future.isAfter(past, 'hour')).toBe(false)
5858
})
5959

60-
it('converts to interval with human display', () => {
60+
it('has a humanized representation', () => {
61+
expect(new Interval({
62+
start: '2020-01-14T03:00:00.000Z',
63+
end: '2021-01-15T10:58:03.330Z',
64+
}).humanized).toEqual('1 year')
65+
expect(new Interval({
66+
start: '2021-01-14T03:00:00.000Z',
67+
end: '2021-04-15T10:58:03.330Z',
68+
}).humanized).toEqual('3 months')
69+
expect(new Interval({
70+
start: '2021-01-14T03:00:00.000Z',
71+
end: '2021-01-18T10:58:03.330Z',
72+
}).humanized).toEqual('4 days')
73+
expect(new Interval({
74+
start: '2021-01-14T03:00:00.000Z',
75+
end: '2021-01-14T10:58:03.330Z',
76+
}).humanized).toEqual('7 hours')
77+
expect(new Interval({
78+
start: '2021-01-14T03:00:00.000Z',
79+
end: '2021-01-14T03:58:03.330Z',
80+
}).humanized).toEqual('58 minutes')
81+
expect(new Interval({
82+
start: '2021-01-14T03:00:00.000Z',
83+
end: '2021-01-14T03:0:03.330Z',
84+
}).humanized).toEqual('now')
85+
})
86+
87+
it('converts to sentence', () => {
6188
const past = new Time('2021-01-14T03:00:00.000Z')
6289
const future = new Time('2021-01-15T10:58:03.330Z')
6390
const interval = future.intervalTo(past)
6491
// it flipped start/end so start always comes first
6592
expect(interval.start.isSame(past, 'millisecond')).toBe(true)
66-
expect(interval.humanized).toEqual('1 day, 7 hours and 58 minutes')
93+
expect(interval.asSentence).toEqual('1 day, 7 hours and 58 minutes')
6794
})
6895

6996
})

shared/src/model/time.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
DateTime as LDT, DurationUnit, Interval as LDTInterval, DateObjectUnits, DurationObject, Zone, Settings,
2+
DateTime as LDT, DurationUnit, Interval as LDTInterval, DateObjectUnits, DurationObject, Zone, Settings, DurationObjectUnits,
33
} from 'luxon'
44
import { map, compact, flatten, max, min, isString, isNumber, isDate } from 'lodash';
55
import { readonly } from 'core-decorators'
@@ -189,6 +189,17 @@ export class Interval {
189189
}
190190

191191
get humanized() {
192+
const durations = ['years', 'months', 'days', 'hours', 'minutes', 'seconds'] as any as keyof DurationObjectUnits
193+
const values = this.asLuxon.toDuration(durations)
194+
for (let i = 0; i < durations.length - 1; i++) {
195+
if (values[durations[i]]) {
196+
return pluralize(durations[i], values[durations[i]], true)
197+
}
198+
}
199+
return 'now'
200+
}
201+
202+
get asSentence() {
192203
const { days, hours, minutes } = this.asLuxon.toDuration(['days', 'hours', 'minutes', 'seconds'])
193204
let str: string[] = []
194205
if (days) str.push(pluralize('day', days, true))

0 commit comments

Comments
 (0)