Skip to content

Commit

Permalink
feat: enable "unitRounding" by default
Browse files Browse the repository at this point in the history
  • Loading branch information
brawaru committed Oct 13, 2023
1 parent 803439b commit c56f6fb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
11 changes: 3 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,7 @@ export interface FormatOptions extends FormatRelativeTimeOptions {
* hour"`. Otherwise, the result of 60 minutes will be returned as is — `"60
* minutes"`.
*
* By default, this option is disabled (`false`) when the `roundingMode` is
* set to `null`, but enabled (`true`) otherwise. This will change in the next
* major update, in which this option will always be enabled (`true`) by
* default, regardless of the `roundingMode`.
*
* @default false / true // Depends on the roundingMode
* @default true
*/
unitRounding?: boolean
}
Expand Down Expand Up @@ -373,8 +368,8 @@ function getRoundingMethod({ roundingMode }: FormatOptions) {
return roundingModesImpls[roundingMode]
}

function shouldUseUnitRounding({ unitRounding, roundingMode }: FormatOptions) {
unitRounding ??= (roundingMode ?? null) !== null
function shouldUseUnitRounding({ unitRounding }: FormatOptions) {
unitRounding ??= true
if (typeof unitRounding !== 'boolean') {
throwRangeError('unitRounding', unitRounding)
}
Expand Down
16 changes: 7 additions & 9 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,37 +156,35 @@ test('formatTimeDifference (halfCeil rounding method)', () => {
).toMatchInlineSnapshot('"через 3 роки"')
})

test('formatTimeDifference (with unitRounding)', () => {
test('formatTimeDifference (without unitRounding)', () => {
const fiftyNineSthnMinutesAgo = now - 59.6 * minute
const twentyThreeSthnHoursInFuture = now + 23.5 * hour

expect(
ago(fiftyNineSthnMinutesAgo, {
roundingMode: 'halfExpand',
unitRounding: false,
}),
).toMatchInlineSnapshot('"60 хвилин тому"')
).toMatchInlineSnapshot('"1 годину тому"')

expect(
ago(twentyThreeSthnHoursInFuture, {
roundingMode: 'halfExpand',
unitRounding: false,
}),
).toMatchInlineSnapshot('"через 24 години"')
).toMatchInlineSnapshot('"завтра"')

expect(
ago(fiftyNineSthnMinutesAgo, {
roundingMode: 'halfExpand',
unitRounding: true,
unitRounding: false,
}),
).toMatchInlineSnapshot('"1 годину тому"')
).toMatchInlineSnapshot('"60 хвилин тому"')

expect(
ago(twentyThreeSthnHoursInFuture, {
roundingMode: 'halfExpand',
unitRounding: true,
unitRounding: false,
}),
).toMatchInlineSnapshot('"завтра"')
).toMatchInlineSnapshot('"через 24 години"')
})

// TODO: more tests + coverage

0 comments on commit c56f6fb

Please sign in to comment.