UNPKG

1.57 kBPlain TextView Raw
1import type {
2 DateAfter,
3 DateBefore,
4 DateInterval,
5 DateLib,
6 DateRange,
7 DayOfWeek
8} from "../types/index.js";
9
10/**
11 * Returns true if `matcher` is of type {@link DateInterval}.
12 *
13 * @group Utilities
14 */
15export function isDateInterval(matcher: unknown): matcher is DateInterval {
16 return Boolean(
17 matcher &&
18 typeof matcher === "object" &&
19 "before" in matcher &&
20 "after" in matcher
21 );
22}
23
24/**
25 * Returns true if `value` is a {@link DateRange} type.
26 *
27 * @group Utilities
28 */
29export function isDateRange(value: unknown): value is DateRange {
30 return Boolean(value && typeof value === "object" && "from" in value);
31}
32
33/**
34 * Returns true if `value` is of type {@link DateAfter}.
35 *
36 * @group Utilities
37 */
38export function isDateAfterType(value: unknown): value is DateAfter {
39 return Boolean(value && typeof value === "object" && "after" in value);
40}
41
42/**
43 * Returns true if `value` is of type {@link DateBefore}.
44 *
45 * @group Utilities
46 */
47export function isDateBeforeType(value: unknown): value is DateBefore {
48 return Boolean(value && typeof value === "object" && "before" in value);
49}
50
51/**
52 * Returns true if `value` is a {@link DayOfWeek} type.
53 *
54 * @group Utilities
55 */
56export function isDayOfWeekType(value: unknown): value is DayOfWeek {
57 return Boolean(value && typeof value === "object" && "dayOfWeek" in value);
58}
59
60/** Returns true if `value` is an array of valid dates. */
61export function isDatesArray(
62 value: unknown,
63 dateLib: DateLib
64): value is Date[] {
65 return Array.isArray(value) && value.every(dateLib.isDate);
66}