date-vir
Version:
Easy and explicit dates and times.
39 lines (38 loc) • 1.11 kB
JavaScript
import { assertValidShape } from 'object-shape-tester';
import { assertValidTimezone } from '../timezone/timezone-checks.js';
import { fullDateShape } from './full-date-shape.js';
import { toLuxonDateTime } from './luxon-date-time-conversion.js';
/**
* Asserts that the input is a valid {@link FullDate}.
*
* @category Assertion
* @throws An Error if the input is not a valid {@link FullDate}.
*/
export function assertValidFullDate(input) {
assertValidShape(input, fullDateShape);
assertValidTimezone(input.timezone);
/** Check that the input can be converted to a luxon date correctly. */
toLuxonDateTime(input);
}
/**
* Checks and type guards that the input is a valid {@link FullDate}.
*
* @category Assertion
*/
export function isValidFullDate(input) {
try {
assertValidFullDate(input);
return true;
}
catch {
return false;
}
}
/**
* Checks and type guards that the given {@link FullDate} instance has the given timezone.
*
* @category Assertion
*/
export function hasTimezone(input, timezone) {
return input.timezone === timezone;
}