Skip to content

Commit

Permalink
Merge pull request #3672 from openstax/fix/interval
Browse files Browse the repository at this point in the history
use whole numbers and read as sentence
  • Loading branch information
nathanstitt authored Jun 30, 2021
2 parents 5ec901f + c461743 commit 87a216f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions shared/specs/model/time.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ describe('time class', () => {
})

it('converts to interval with human display', () => {
const past = new Time('2021-01-15T10:00:00.000Z')
const future = new Time('2021-01-15T10:58:00.000Z')
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('58 minutes')
expect(interval.humanized).toEqual('1 day, 7 hours and 58 minutes')
})

})
5 changes: 3 additions & 2 deletions shared/src/model/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import moment from 'moment';
import { extendMoment } from 'moment-range';
import pluralize from 'pluralize';
import { now as getNow } from 'mobx-utils'
import { toSentence } from '../helpers/string';

const { range } = extendMoment(moment as any);

Expand Down Expand Up @@ -188,12 +189,12 @@ export class Interval {
}

get humanized() {
const { days, hours, minutes } = this.asLuxon.toDuration(['days', 'hours', 'minutes'])
const { days, hours, minutes } = this.asLuxon.toDuration(['days', 'hours', 'minutes', 'seconds'])
let str: string[] = []
if (days) str.push(pluralize('day', days, true))
if (hours) str.push(pluralize('hour', hours, true))
if (minutes) str.push(pluralize('minute', minutes, true))
return str.join(' ')
return toSentence(str)
}

get asMoment() {
Expand Down

0 comments on commit 87a216f

Please sign in to comment.