UNPKG

@date-vir/duration

Version:

Durations units an utils for date-vir.

156 lines (155 loc) 4.69 kB
import { AssertionError } from '@augment-vir/assert'; import { hourBounds, millisecondsBounds, minuteBounds, secondBounds, } from './date-unit.js'; import { dayOfWeekIndexBounds } from './day-of-week.js'; import { dayOfMonthBounds, monthNumberBounds } from './month.js'; /** * Checks if input is a valid month number. * * @category Assertion */ export function isValidMonthNumber(input) { return (Number.isInteger(input) && monthNumberBounds.min <= input && input <= monthNumberBounds.max); } /** * Checks if input is a valid day of the week index. * * @category Assertion */ export function isValidDayOfWeekIndex(input) { return (Number.isInteger(input) && dayOfWeekIndexBounds.min <= input && input <= dayOfWeekIndexBounds.max); } /** * Checks if input is a valid day of month number. * * @category Assertion */ export function isValidDayOfMonth(input) { return (Number.isInteger(input) && dayOfMonthBounds.min <= input && input <= dayOfMonthBounds.max); } /** * Checks if input is a valid hour number. * * @category Assertion */ export function isValidHour(input) { return Number.isInteger(input) && hourBounds.min <= input && input <= hourBounds.max; } /** * Checks if input is a valid minute number. * * @category Assertion */ export function isValidMinute(input) { return Number.isInteger(input) && minuteBounds.min <= input && input <= minuteBounds.max; } /** * Checks if input is a valid second number. * * @category Assertion */ export function isValidSecond(input) { return Number.isInteger(input) && secondBounds.min <= input && input <= secondBounds.max; } /** * Checks if input is a valid millisecond number. * * @category Assertion */ export function isValidMillisecond(input) { return (Number.isInteger(input) && millisecondsBounds.min <= input && input <= millisecondsBounds.max); } /** * Asserts that the input is a valid month number. * * @category Assertion * @returns The input if the assertion succeeds. * @throws `AssertionError` If the assertion fails. */ export function assertWrapMonthNumber(input, failureMessage) { if (!isValidMonthNumber(input)) { throw new AssertionError(`${input} is not a valid month number.`, failureMessage); } return input; } /** * Asserts that the input is a valid day of the week index. * * @category Assertion * @returns The input if the assertion succeeds. * @throws `AssertionError` If the assertion fails. */ export function assertWrapDayOfWeekIndex(input, failureMessage) { if (!isValidDayOfWeekIndex(input)) { throw new AssertionError(`${input} is not a valid day of week index.`, failureMessage); } return input; } /** * Asserts that the input is a valid day of month number. * * @category Assertion * @returns The input if the assertion succeeds. * @throws `AssertionError` If the assertion fails. */ export function assertWrapDayOfMonth(input, failureMessage) { if (!isValidDayOfMonth(input)) { throw new AssertionError(`${input} is not a valid day of month.`, failureMessage); } return input; } /** * Asserts that the input is a valid hour number. * * @category Assertion * @returns The input if the assertion succeeds. * @throws `AssertionError` If the assertion fails. */ export function assertWrapHour(input, failureMessage) { if (!isValidHour(input)) { throw new AssertionError(`${input} is not a valid hour.`, failureMessage); } return input; } /** * Asserts that the input is a valid minute number. * * @category Assertion * @returns The input if the assertion succeeds. * @throws `AssertionError` If the assertion fails. */ export function assertWrapMinute(input, failureMessage) { if (!isValidMinute(input)) { throw new AssertionError(`${input} is not a valid minute.`, failureMessage); } return input; } /** * Asserts that the input is a valid second number. * * @category Assertion * @returns The input if the assertion succeeds. * @throws `AssertionError` If the assertion fails. */ export function assertWrapSecond(input, failureMessage) { if (!isValidSecond(input)) { throw new AssertionError(`${input} is not a valid second.`, failureMessage); } return input; } /** * Asserts that the input is a valid millisecond number. * * @category Assertion * @returns The input if the assertion succeeds. * @throws `AssertionError` If the assertion fails. */ export function assertWrapMillisecond(input, failureMessage) { if (!isValidMillisecond(input)) { throw new AssertionError(`${input} is not a valid millisecond.`, failureMessage); } return input; }