@beyonk/date-utils
Advanced tools
Comparing version 4.4.1 to 4.5.0
@@ -5,3 +5,4 @@ import { | ||
minsToTime, | ||
ymdNoTimezone as toISODateString | ||
ymdNoTimezone as toISODateString, | ||
utcDate | ||
} from './date-utils.js' | ||
@@ -25,5 +26,17 @@ | ||
function toStartDate (iso) { | ||
const { date, timeslot } = fromISOString(iso) | ||
return utcDate(date).add(timeslot, 'minutes').toDate() | ||
} | ||
function toEndDate (iso) { | ||
const { duration } = fromISOString(iso) | ||
return utcDate(toStartDate(iso)).add(duration, 'minutes').toDate() | ||
} | ||
export { | ||
fromISOString, | ||
toISOString | ||
toISOString, | ||
toStartDate, | ||
toEndDate | ||
} |
import { expect } from '@hapi/code' | ||
import { toISOString } from './iso-dates.js' | ||
import { toISOString, fromISOString, toStartDate, toEndDate } from './iso-dates.js' | ||
describe('iso-dates', () => { | ||
const scenarios = [ | ||
{ | ||
ctx: 'from Date object', | ||
date: new Date('2021-10-31T00:00:00.000Z'), | ||
timeslot: 540, | ||
duration: 90 | ||
}, | ||
{ | ||
ctx: 'from Date string', | ||
date: '2021-10-31', | ||
timeslot: 540, | ||
duration: 90 | ||
context('#toISOString()', () => { | ||
const scenarios = [ | ||
{ | ||
ctx: 'from Date object', | ||
date: new Date('2021-10-31T00:00:00.000Z'), | ||
timeslot: 540, | ||
duration: 90 | ||
}, | ||
{ | ||
ctx: 'from Date string', | ||
date: '2021-10-31', | ||
timeslot: 540, | ||
duration: 90 | ||
} | ||
] | ||
for (const { ctx, date, timeslot, duration } of scenarios) { | ||
context(ctx, () => { | ||
it('returns expected string', () => { | ||
expect( | ||
toISOString(date, timeslot, duration) | ||
).to.equal( | ||
'2021-10-31T09:00--PT90M' | ||
) | ||
}) | ||
}) | ||
} | ||
] | ||
}) | ||
for (const { ctx, date, timeslot, duration } of scenarios) { | ||
context(ctx, () => { | ||
it('returns expected string', () => { | ||
describe('#fromISOString()', () => { | ||
const scenarios = [ | ||
{ test: 'relative time', value: '2021-10-31T01:15--PT1H30M' }, | ||
{ test: 'UTC time', value: '2021-10-31T01:15Z--PT1H30M' }, | ||
{ test: 'UTC offset', value: '2021-10-31T01:15-05:00--PT1H30M' } | ||
] | ||
for (const { test, value } of scenarios) { | ||
it(`extracts local date, timeslot and duration elements (${test})`, () => { | ||
expect( | ||
toISOString(date, timeslot, duration) | ||
).to.equal( | ||
'2021-10-31T09:00--PT90M' | ||
) | ||
fromISOString(value) | ||
).to.equal({ | ||
date: '2021-10-31', | ||
timeslot: 75, | ||
duration: 90 | ||
}) | ||
}) | ||
} | ||
}) | ||
describe('#toStartDate()', () => { | ||
it('returns date', () => { | ||
expect( | ||
toStartDate('2021-10-31T01:15Z--PT1H30M') | ||
).to.equal( | ||
new Date('2021-10-31T01:15Z') | ||
) | ||
}) | ||
} | ||
}) | ||
describe('#toEndDate()', () => { | ||
it('returns date', () => { | ||
expect( | ||
toEndDate('2021-10-31T01:15Z--PT1H30M') | ||
).to.equal( | ||
new Date('2021-10-31T02:45Z') | ||
) | ||
}) | ||
}) | ||
}) |
{ | ||
"name": "@beyonk/date-utils", | ||
"version": "4.4.1", | ||
"version": "4.5.0", | ||
"description": "Beyonk Date Utils", | ||
@@ -5,0 +5,0 @@ "author": "Antony Jones <aj@desirableobjects.co.uk>", |
18478
570