@formkit/tempo 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,352 @@
1
+ /**
2
+ * The date format used as an input value. Either a date or an ISO8601 string.
3
+ */
4
+ type DateInput = Date | string;
5
+ /**
6
+ * Format parts with text names use these descriptors:
7
+ */
8
+ type NamedFormatOption = "long" | "short" | "narrow";
9
+ /**
10
+ * A registry of named format parts. Each type of part has every option.
11
+ */
12
+ interface NamedFormats {
13
+ weekday: Record<string, NamedFormatOption>;
14
+ month: Record<string, NamedFormatOption>;
15
+ dayPeriod: Record<string, NamedFormatOption>;
16
+ }
17
+ /**
18
+ * Internal format for "pieces" of a date form. Each part represents a single
19
+ * logical grouping, like "month", or "seconds".
20
+ */
21
+ interface Part {
22
+ /**
23
+ * An object of partName to partValue For example:
24
+ * ```js
25
+ * { hour: '2-digit' }
26
+ * ```
27
+ */
28
+ option: FormatPattern[1];
29
+ /**
30
+ * The name of the part, these must be valid parts of a date format as
31
+ * specified in Intl.DateTimeFormatPartTypes. Valid values are:
32
+ * day, dayPeriod, era, hour, literal, minute, month, second, timeZoneName,
33
+ * weekday, year
34
+ */
35
+ partName: Intl.DateTimeFormatPartTypes;
36
+ /**
37
+ * The value of a given part. For example "2-digit", or "narrow".
38
+ */
39
+ partValue: string;
40
+ /**
41
+ * The string token that represents the regex. For example "YYYY".
42
+ */
43
+ token: string;
44
+ /**
45
+ * A regular expression if the above token.
46
+ */
47
+ pattern: RegExp;
48
+ /**
49
+ * Does this part require a the hour12 clock.
50
+ */
51
+ hour12: boolean;
52
+ }
53
+ /**
54
+ * A date part with an actual value applied.
55
+ */
56
+ type FilledPart = Part & {
57
+ value: string;
58
+ };
59
+ /**
60
+ * A tuple describing a given formatting token.
61
+ */
62
+ type FormatPattern = [
63
+ pattern: FormatToken | string,
64
+ option: Partial<Record<Intl.DateTimeFormatPartTypes, string>>,
65
+ exp?: RegExp
66
+ ];
67
+ /**
68
+ * Possible options for a format style.
69
+ */
70
+ type FormatStyle = "full" | "long" | "medium" | "short";
71
+ /**
72
+ * Possible objects for the dateStyle and timeStyle.
73
+ */
74
+ type FormatStyleObj = {
75
+ date: FormatStyle;
76
+ time: FormatStyle;
77
+ } | {
78
+ date: FormatStyle;
79
+ } | {
80
+ time: FormatStyle;
81
+ };
82
+ type Format = FormatStyle | FormatStyleObj | string;
83
+ /**
84
+ * A union of all available formatting tokens.
85
+ */
86
+ type FormatToken = "YYYY" | "YY" | "MMMM" | "MMM" | "MM" | "M" | "DD" | "D" | "dddd" | "ddd" | "d" | "mm" | "m" | "ss" | "s" | "HH" | "H" | "hh" | "h" | "a" | "A" | "ZZ" | "Z";
87
+ interface ParseOptions {
88
+ /**
89
+ * A string representing a date.
90
+ */
91
+ date: string;
92
+ /**
93
+ * The format that should be used to parse the date. This is a string composed
94
+ * of tokens.
95
+ */
96
+ format: Format;
97
+ /**
98
+ * The locale used to parse the date.
99
+ */
100
+ locale: string;
101
+ /**
102
+ * A function that can be used to filter out parts of the format. This is
103
+ * useful when using the native Intl formats like
104
+ * `{ date: 'full', time: 'full' }` and not wanting to keep all the parts of
105
+ * the given format.
106
+ */
107
+ partFilter?: (part: Part) => boolean;
108
+ /**
109
+ * The behavior to use when a date overflows a given month. For example, if
110
+ * the date to parse is February 29, 2023 — there is no 29th day of February.
111
+ * In this case overflow "forward" would result in March 1, 2023, "backward"
112
+ * would result in February 28, 2023, and "throw" would throw an error.
113
+ */
114
+ dateOverflow?: "forward" | "backward" | "throw";
115
+ }
116
+
117
+ /**
118
+ * Returns a new date object 1/n days after the original one.
119
+ * @param inputDate - A date to increment by 1 day.
120
+ */
121
+ declare function addDay(inputDate: DateInput, count?: number): Date;
122
+
123
+ /**
124
+ * Returns a new date object 1/n months after the original one. Keep in mind if you
125
+ * start with a date late in a given month you could get a date after the next
126
+ * month.
127
+ * @param inputDate - A date to increment by 1 or more months.
128
+ * @param count - The quantity to add.
129
+ * @param dateOverflow - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month.
130
+ */
131
+ declare function addMonth(inputDate: DateInput, count?: number, dateOverflow?: boolean): Date;
132
+
133
+ /**
134
+ * Returns a new date object 1/n years after the original one. Keep in mind if
135
+ * you start with a date late in a given month you could get a date after the
136
+ * next month.
137
+ * @param inputDate - A date to increment by 1 day.
138
+ * @param count - The quantity of years add.
139
+ * @param dateOverflow - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month.
140
+ */
141
+ declare function addYear(inputDate: DateInput, count?: number, dateOverflow?: boolean): Date;
142
+
143
+ /**
144
+ * Returns a new date object 1/n hours after the original one.
145
+ * @param inputDate - A date to increment by 1 day.
146
+ */
147
+ declare function addHour(inputDate: DateInput, count?: number): Date;
148
+
149
+ /**
150
+ * Returns a new date object 1/n seconds after the original one.
151
+ * @param inputDate - A date to increment by 1 day.
152
+ */
153
+ declare function addMinute(inputDate: DateInput, count?: number): Date;
154
+
155
+ /**
156
+ * Returns a new date object 1/n seconds after the original one.
157
+ * @param inputDate - A date to increment by 1 day.
158
+ */
159
+ declare function addSecond(inputDate: DateInput, count?: number): Date;
160
+
161
+ /**
162
+ * Determines the correct value for am/pm by locale and memoizes it.
163
+ * @param ampm - am or pm
164
+ * @param locale - The locale to fetch.
165
+ */
166
+ declare function ap(ampm: "am" | "pm", locale: string): string;
167
+
168
+ /**
169
+ * Apply a given offset to a date, returning a new date with the offset
170
+ * applied by adding or subtracting the given number of minutes.
171
+ * @param dateInput - The date to apply the offset to.
172
+ * @param offset - The offset to apply in the +-HHmm format.
173
+ */
174
+ declare function applyOffset(dateInput: DateInput, offset?: string): Date;
175
+
176
+ /**
177
+ * A date to parse.
178
+ * @param date - A Date object or an ISO 8601 date.
179
+ */
180
+ declare function date(date?: DateInput): Date;
181
+
182
+ /**
183
+ * Gets the what day of the year a given date is. For example, August 1st is
184
+ * the 213th day of the year on non-leapyears and 214th on leapyears.
185
+ * @param inputDate - The input date.
186
+ */
187
+ declare function dayOfYear(inputDate: DateInput): number;
188
+
189
+ /**
190
+ * Returns a Date object for end of the given day.
191
+ * @param inputDate - A string or Date object
192
+ */
193
+ declare function dayEnd(inputDate: DateInput): Date;
194
+
195
+ /**
196
+ * Returns a Date object for start of the given day.
197
+ * @param inputDate - A string or Date object
198
+ */
199
+ declare function dayStart(inputDate: DateInput): Date;
200
+
201
+ /**
202
+ * Produce a formatted string. Available strings:
203
+ *
204
+ * YY - 2 digit year
205
+ * YYYY - 4 digit year
206
+ * M - The month 1-12
207
+ * MM - The month 01-12
208
+ * MMM - Short name Jan-Dec
209
+ * MMMM - Full name January - December
210
+ * D - The day of the month 1-31
211
+ * DD - The day of the month 01-31
212
+ * d - Single digit day "T"
213
+ * ddd - Short day name Thu
214
+ * dddd - Full day name Wednesday
215
+ * H - Minimum hour digits, 24 hour, 0-23
216
+ * HH - 2 hour digits, 24 hour, 00-23
217
+ * h - Minimum hour digits, 12 hour clock, 1-12
218
+ * hh - 2 hour digits, 12 hour clock, 01-12
219
+ * m - The minute 0-59
220
+ * mm - The minute 00-59
221
+ * s - The second 0-59
222
+ * ss - The second 00-59
223
+ * a - am/pm
224
+ * A - AM/PM
225
+ * Z - +0800, +0530, -1345
226
+ *
227
+ * @param inputDate - A date object or ISO 8601 string
228
+ * @param format - A format
229
+ */
230
+ declare function format(inputDate: DateInput, format?: Format, locale?: string, genitive?: boolean, partFilter?: (part: Part) => boolean): string;
231
+
232
+ /**
233
+ * Return the string format for a given format. For example:
234
+ * ```js
235
+ * formatStr({ date: 'long' }, 'en') // dddd, MMMM D, YYYY
236
+ * ```
237
+ * @param format - A format string or object.
238
+ * @param locale - A locale or en by default.
239
+ */
240
+ declare function formatStr(format: Format, locale?: string, escapeLiterals?: boolean, filterParts?: (part: Part) => boolean): string;
241
+
242
+ /**
243
+ * Converts a 2 digit year into a 4 digit year. This function assumes years 20
244
+ * years into the future belong to the current century, and the past 80 are in
245
+ * the past.
246
+ *
247
+ * @param value - 2 digits in string format
248
+ */
249
+ declare function fourDigitYear(value: string): number;
250
+
251
+ /**
252
+ * True when the date string is valid ISO 8601.
253
+ * @param date - A date string.
254
+ */
255
+ declare function iso8601(date: string): boolean;
256
+
257
+ /**
258
+ * Returns the total number of days from a given month.
259
+ * @param inputDate - A string or Date object
260
+ */
261
+ declare function monthDays(inputDate: DateInput): number;
262
+
263
+ /**
264
+ * Returns a Date object for the with the input date set to the last day of
265
+ * the current month. Does not change the time.
266
+ * @param inputDate - A string or Date object
267
+ */
268
+ declare function monthEnd(inputDate: DateInput): Date;
269
+
270
+ /**
271
+ * Returns a Date object for the first day of a month.
272
+ * @param inputDate - A string or Date object
273
+ */
274
+ declare function monthStart(inputDate: DateInput): Date;
275
+
276
+ /**
277
+ * Performs a bidirectional search for the nearest date that passes a function.
278
+ * @param target - Performs a search for the nearest passing date.
279
+ * @param search - The search function to use, given a date returns a boolean.
280
+ * @param constraint - The number of iterations to perform before giving up, or logical constraint like "month", or "week".
281
+ *
282
+ */
283
+ declare function nearestDay(inputDate: DateInput, search: (date: Date) => boolean, constraint?: number | "month" | "week" | "year"): Date | null;
284
+
285
+ /**
286
+ * Returns the offset between two timezones on a given date. The results are
287
+ * ISO8601 compatible offsets like -0800 or +0530.
288
+ *
289
+ * @param dateInput - The date on which to determine the offset.
290
+ * @param tzA - The second timezone to compare determine the offset between.
291
+ * @param tzB - The first timezone to compare determine the offset between.
292
+ */
293
+ declare function offset(utcTime: DateInput, tzA?: string, tzB?: string): string;
294
+
295
+ declare function parse(options: ParseOptions): Date | never;
296
+ declare function parse(dateStr: string, format?: Format, locale?: string): Date | never;
297
+
298
+ /**
299
+ * Given a format string, produce an array of matching "parts", each part
300
+ * contains a regular expression and the corresponding
301
+ * Intl.DateTimeFormatPartTypesRegistry key/value.
302
+ * @param format - A format string like MM/DD/YYYY
303
+ * @param locale - The locale to parse for.
304
+ */
305
+ declare function parts(format: Format, locale: string): Part[];
306
+
307
+ /**
308
+ * Returns an array of options for a given token in a given locale.
309
+ * @param token - Get the full range of options for a given token
310
+ * @param locale - The locale to fetch the options for.
311
+ */
312
+ declare function range(token: FormatToken, locale?: string, genitive?: boolean): string[];
313
+
314
+ /**
315
+ * Inverts the offset and applies it to the given date, returning a new date.
316
+ * @param dateInput - The date to remove the offset from.
317
+ * @param offset - The offset to remove in the +-HHmm format.
318
+ */
319
+ declare function removeOffset(dateInput: DateInput, offset?: string): Date;
320
+
321
+ /**
322
+ * Checks if two date objects refer to the same date. Ignores time.
323
+ * @param inputDateA - First date to compare
324
+ * @param inputDateB - Second date to compare
325
+ */
326
+ declare function sameDay(inputDateA: DateInput, inputDateB: DateInput): boolean;
327
+
328
+ /**
329
+ * Returns a Date object for the last day at the last second of the given week.
330
+ * Defaults to Sunday as the first day of the week:
331
+ * 0 = Sunday ... 6 = Saturday
332
+ * @param inputDate - Gets the last day of the week
333
+ * @param startOfWeekDay - The first day of the week
334
+ */
335
+ declare function weekEnd(inputDate: DateInput, startOfWeekDay?: number): Date;
336
+
337
+ /**
338
+ * Returns a Date object for start of the given week. Defaults to Sunday as the
339
+ * first day of the week:
340
+ * 0 = Sunday ... 6 = Saturday
341
+ * @param inputDate - A string or Date object
342
+ * @param startOfWeekDay - Determines which day of the week is the first
343
+ */
344
+ declare function weekStart(inputDate: DateInput, startOfWeekDay?: number): Date;
345
+
346
+ /**
347
+ * Get the number of days in the given date’s year.
348
+ * @param inputDate - A string or Date object
349
+ */
350
+ declare function yearDays(inputDate: DateInput): number;
351
+
352
+ export { type DateInput, type FilledPart, type Format, type FormatPattern, type FormatStyle, type FormatStyleObj, type FormatToken, type NamedFormatOption, type NamedFormats, type ParseOptions, type Part, addDay, addHour, addMinute, addMonth, addSecond, addYear, ap, applyOffset, date, dayEnd, dayOfYear, dayStart, format, formatStr, fourDigitYear, iso8601, monthDays, monthEnd, monthStart, nearestDay, offset, parse, parts, range, removeOffset, sameDay, weekEnd, weekStart, yearDays };