@beyonk/date-utils
Advanced tools
Comparing version 4.7.4 to 4.7.5
@@ -0,9 +1,23 @@ | ||
import { pluralize } from 'just-pluralize' | ||
import { formatMediumDateTime, formatShortTime } from '../date/format.js' | ||
function formatFinish ({ start, finish }, opts = {}) { | ||
const { locale } = opts | ||
const { format, ...otherOpts } = opts | ||
return finish.isSame(start, 'day') | ||
? `until ${formatShortTime(finish, { locale })}` | ||
: `until ${formatMediumDateTime(finish, { locale })}` | ||
switch (format) { | ||
case 'short': { | ||
const diffInDays = finish.diff(start, 'day') | ||
return [ | ||
formatShortTime(finish, otherOpts), | ||
diffInDays > 0 ? `(+${diffInDays} ${pluralize('day', diffInDays)})` : null | ||
].filter(Boolean).join(' ') | ||
} | ||
default: { | ||
return finish.isSame(start, 'day') | ||
? `until ${formatShortTime(finish, otherOpts)}` | ||
: `until ${formatMediumDateTime(finish, otherOpts)}` | ||
} | ||
} | ||
} | ||
@@ -10,0 +24,0 @@ |
import { formatFinish } from './format-finish.js' | ||
import { formatMediumDateTime } from '../date/format.js' | ||
import { formatMediumDateTime, formatShortTime } from '../date/format.js' | ||
function toLocaleString (interval, opts = {}) { | ||
let separator | ||
let startDate | ||
switch (opts.format) { | ||
case 'short': | ||
separator = ' – ' | ||
startDate = formatShortTime(interval.start, { ...opts }) | ||
break | ||
default: | ||
separator = ' ' | ||
startDate = formatMediumDateTime(interval.start, { ...opts, year: 'numeric' }) | ||
break | ||
} | ||
return [ | ||
formatMediumDateTime(interval.start, { ...opts, year: 'numeric' }), | ||
startDate, | ||
formatFinish(interval, opts) | ||
@@ -17,0 +22,0 @@ ].join(separator) |
@@ -18,2 +18,11 @@ import { expect } from '@hapi/code' | ||
{ | ||
test: 'Formatted output of the same day - short', | ||
interval: { | ||
start: dayjs('2021-10-30T12:15'), | ||
finish: dayjs('2021-10-30T14:15') | ||
}, | ||
opts: { locale: 'en-GB', format: 'short' }, | ||
expectation: '12:15 – 14:15' | ||
}, | ||
{ | ||
test: 'Formatted output of different days', | ||
@@ -26,2 +35,20 @@ interval: { | ||
expectation: 'Sat, 30 Oct 2021, 12:15 until Sun, 31 Oct, 14:15' | ||
}, | ||
{ | ||
test: 'Formatted output of different days - short', | ||
interval: { | ||
start: dayjs('2021-10-30T12:15'), | ||
finish: dayjs('2021-10-31T14:15') | ||
}, | ||
opts: { locale: 'en-GB', format: 'short' }, | ||
expectation: '12:15 – 14:15 (+1 day)' | ||
}, | ||
{ | ||
test: 'Formatted output of different days - short and en-US', | ||
interval: { | ||
start: dayjs('2021-10-30T12:15'), | ||
finish: dayjs('2021-11-02T14:15') | ||
}, | ||
opts: { locale: 'en-US', format: 'short' }, | ||
expectation: '12:15 PM – 2:15 PM (+3 days)' | ||
} | ||
@@ -28,0 +55,0 @@ ] |
{ | ||
"name": "@beyonk/date-utils", | ||
"version": "4.7.4", | ||
"version": "4.7.5", | ||
"description": "Beyonk Date Utils", | ||
@@ -5,0 +5,0 @@ "author": "Antony Jones <aj@desirableobjects.co.uk>", |
Sorry, the diff of this file is not supported yet
44612
1463