1 | import type { ContextOptions, DateArg } from "./types.js";
|
2 | /**
|
3 | * The {@link closestTo} function options.
|
4 | */
|
5 | export interface ClosestToOptions<DateType extends Date = Date>
|
6 | extends ContextOptions<DateType> {}
|
7 | /**
|
8 | * The {@link closestTo} function result type. It resolves the proper data type.
|
9 | * It uses the first argument date object type, starting from the date argument,
|
10 | * then the start interval date, and finally the end interval date. If
|
11 | * a context function is passed, it uses the context function return type.
|
12 | */
|
13 | export type ClosestToResult<
|
14 | DateToCompare extends DateArg<Date>,
|
15 | DatesType extends DateArg<Date>[],
|
16 | Options extends ClosestToOptions | undefined,
|
17 | > =
|
18 | Options extends ClosestToOptions<infer DateType extends Date>
|
19 | ? DateType
|
20 | : DateToCompare extends Date
|
21 | ? DateToCompare
|
22 | : DatesType extends DateArg<infer DateType>[]
|
23 | ? DateType
|
24 | : Date;
|
25 | /**
|
26 | * @name closestTo
|
27 | * @category Common Helpers
|
28 | * @summary Return a date from the array closest to the given date.
|
29 | *
|
30 | * @description
|
31 | * Return a date from the array closest to the given date.
|
32 | *
|
33 | * @typeParam DateToCompare - Date to compare argument type.
|
34 | * @typeParam DatesType - Dates array argument type.
|
35 | * @typeParam Options - Options type.
|
36 | *
|
37 | * @param dateToCompare - The date to compare with
|
38 | * @param dates - The array to search
|
39 | *
|
40 | * @returns The date from the array closest to the given date or undefined if no valid value is given
|
41 | *
|
42 | * @example
|
43 | * // Which date is closer to 6 September 2015: 1 January 2000 or 1 January 2030?
|
44 | * const dateToCompare = new Date(2015, 8, 6)
|
45 | * const result = closestTo(dateToCompare, [
|
46 | * new Date(2000, 0, 1),
|
47 | * new Date(2030, 0, 1)
|
48 | * ])
|
49 | * //=> Tue Jan 01 2030 00:00:00
|
50 | */
|
51 | export declare function closestTo<
|
52 | DateToCompare extends DateArg<Date>,
|
53 | DatesType extends DateArg<Date>[],
|
54 | Options extends ClosestToOptions | undefined = undefined,
|
55 | >(
|
56 | dateToCompare: DateToCompare,
|
57 | dates: DatesType,
|
58 | options?: Options | undefined,
|
59 | ): ClosestToResult<DateToCompare, DatesType, Options> | undefined;
|