UNPKG

date-vir

Version:

Easy and explicit dates and times.

49 lines (48 loc) 1.65 kB
import { checkValidShape } from 'object-shape-tester'; import { datePartShape, timePartShape, } from '../full-date/full-date-shape.js'; /** * Combine two parts of a {@link FullDate} objects into a filled-in {@link FullDate}. Note that the * timezones must match for both inputs. If two complete {@link FullDate} objects are passed in, only * the respective parts of each is used. * * @category Util */ export function combineDateParts(maybeDateParts) { const timePart = checkValidShape(maybeDateParts.time, timePartShape, { allowExtraKeys: true, }) ? { hour: maybeDateParts.time.hour, minute: maybeDateParts.time.minute, second: maybeDateParts.time.second, millisecond: maybeDateParts.time.millisecond, timezone: maybeDateParts.time.timezone, } : undefined; const datePart = checkValidShape(maybeDateParts.date, datePartShape, { allowExtraKeys: true, }) ? { year: maybeDateParts.date.year, month: maybeDateParts.date.month, day: maybeDateParts.date.day, timezone: maybeDateParts.date.timezone, } : undefined; if (timePart && datePart) { if (timePart.timezone !== datePart.timezone) { throw new Error(`Time and date parts have mismatching timezones. Got '${timePart.timezone}' and '${datePart.timezone}'.`); } return { ...timePart, ...datePart, }; } if (timePart) { return timePart; } if (datePart) { return datePart; } return undefined; }