Skip to content

Commit

Permalink
Merge pull request #3673 from openstax/fix/interval
Browse files Browse the repository at this point in the history
improve humanized, show only most significant duration
  • Loading branch information
nathanstitt authored Jul 6, 2021
2 parents 87a216f + c943a33 commit 2f5ace1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
33 changes: 30 additions & 3 deletions shared/specs/model/time.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Time, { setNow, setResolution } from '../../src/model/time'
import Time, { Interval, setNow, setResolution } from '../../src/model/time'
import { autorun } from 'mobx'
import moment from 'moment'

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

it('converts to interval with human display', () => {
it('has a humanized representation', () => {
expect(new Interval({
start: '2020-01-14T03:00:00.000Z',
end: '2021-01-15T10:58:03.330Z',
}).humanized).toEqual('1 year')
expect(new Interval({
start: '2021-01-14T03:00:00.000Z',
end: '2021-04-15T10:58:03.330Z',
}).humanized).toEqual('3 months')
expect(new Interval({
start: '2021-01-14T03:00:00.000Z',
end: '2021-01-18T10:58:03.330Z',
}).humanized).toEqual('4 days')
expect(new Interval({
start: '2021-01-14T03:00:00.000Z',
end: '2021-01-14T10:58:03.330Z',
}).humanized).toEqual('7 hours')
expect(new Interval({
start: '2021-01-14T03:00:00.000Z',
end: '2021-01-14T03:58:03.330Z',
}).humanized).toEqual('58 minutes')
expect(new Interval({
start: '2021-01-14T03:00:00.000Z',
end: '2021-01-14T03:0:03.330Z',
}).humanized).toEqual('now')
})

it('converts to sentence', () => {
const past = new Time('2021-01-14T03:00:00.000Z')
const future = new Time('2021-01-15T10:58:03.330Z')
const interval = future.intervalTo(past)
// it flipped start/end so start always comes first
expect(interval.start.isSame(past, 'millisecond')).toBe(true)
expect(interval.humanized).toEqual('1 day, 7 hours and 58 minutes')
expect(interval.asSentence).toEqual('1 day, 7 hours and 58 minutes')
})

})
13 changes: 12 additions & 1 deletion shared/src/model/time.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
DateTime as LDT, DurationUnit, Interval as LDTInterval, DateObjectUnits, DurationObject, Zone, Settings,
DateTime as LDT, DurationUnit, Interval as LDTInterval, DateObjectUnits, DurationObject, Zone, Settings, DurationObjectUnits,
} from 'luxon'
import { map, compact, flatten, max, min, isString, isNumber, isDate } from 'lodash';
import { readonly } from 'core-decorators'
Expand Down Expand Up @@ -189,6 +189,17 @@ export class Interval {
}

get humanized() {
const durations = ['years', 'months', 'days', 'hours', 'minutes', 'seconds'] as any as keyof DurationObjectUnits
const values = this.asLuxon.toDuration(durations)
for (let i = 0; i < durations.length - 1; i++) {
if (values[durations[i]]) {
return pluralize(durations[i], values[durations[i]], true)
}
}
return 'now'
}

get asSentence() {
const { days, hours, minutes } = this.asLuxon.toDuration(['days', 'hours', 'minutes', 'seconds'])
let str: string[] = []
if (days) str.push(pluralize('day', days, true))
Expand Down

0 comments on commit 2f5ace1

Please sign in to comment.