Skip to content

Commit 0891175

Browse files
committed
feat(query): add date query convenience methods 🍓
- Add isFuture and isPast methods for temporal comparisons - Add isSameDay, isSameMonth, isSameQuarter, isSameWeek, and isSameYear methods - Add isThisMonth, isThisQuarter, isThisWeek, and isThisYear methods - Add isToday, isTomorrow, and isYesterday methods - Add test coverage for all query methods - Update IDaytime interface with new query methods - Format test code with improved readability
1 parent 57a09f0 commit 0891175

4 files changed

Lines changed: 505 additions & 6 deletions

File tree

src/Daytime.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,15 @@ export class Daytime implements Types.IDaytime {
209209
return Helpers.isDST(this.date)
210210
}
211211

212+
/**
213+
* Checks if this date is in the future (after today).
214+
* @returns True if this date is in the future, false otherwise
215+
*/
216+
isFuture(): boolean {
217+
const now = new Date()
218+
return Utils.isAfter(this.date, now)
219+
}
220+
212221
/**
213222
* Checks if the year of the date is a leap year.
214223
* @returns True if the year is a leap year, false otherwise
@@ -217,6 +226,15 @@ export class Daytime implements Types.IDaytime {
217226
return Helpers.isLeapYear(this.date)
218227
}
219228

229+
/**
230+
* Checks if this date is in the past (before today).
231+
* @returns True if this date is in the past, false otherwise
232+
*/
233+
isPast(): boolean {
234+
const now = new Date()
235+
return Utils.isBefore(this.date, now)
236+
}
237+
220238
/**
221239
* Checks if this date is the same as another date.
222240
* @param other - The other date to compare with
@@ -228,6 +246,24 @@ export class Daytime implements Types.IDaytime {
228246
return Utils.isSame(this.date, otherDate, unit)
229247
}
230248

249+
/**
250+
* Checks if this date is the same day as another date.
251+
* @param other - The other date to compare with
252+
* @returns True if dates are on the same day, false otherwise
253+
*/
254+
isSameDay(other: Types.DateInput): boolean {
255+
return this.isSame(other, 'day')
256+
}
257+
258+
/**
259+
* Checks if this date is the same month as another date.
260+
* @param other - The other date to compare with
261+
* @returns True if dates are in the same month, false otherwise
262+
*/
263+
isSameMonth(other: Types.DateInput): boolean {
264+
return this.isSame(other, 'month')
265+
}
266+
231267
/**
232268
* Checks if this date is the same as or after another date.
233269
* @param other - The other date to compare with
@@ -250,6 +286,88 @@ export class Daytime implements Types.IDaytime {
250286
return Utils.isSameOrBefore(this.date, otherDate, unit)
251287
}
252288

289+
/**
290+
* Checks if this date is the same quarter as another date.
291+
* @param other - The other date to compare with
292+
* @returns True if dates are in the same quarter, false otherwise
293+
*/
294+
isSameQuarter(other: Types.DateInput): boolean {
295+
return this.isSame(other, 'quarter')
296+
}
297+
298+
/**
299+
* Checks if this date is the same week as another date.
300+
* @param other - The other date to compare with
301+
* @returns True if dates are in the same week, false otherwise
302+
*/
303+
isSameWeek(other: Types.DateInput): boolean {
304+
return this.isSame(other, 'week')
305+
}
306+
307+
/**
308+
* Checks if this date is the same year as another date.
309+
* @param other - The other date to compare with
310+
* @returns True if dates are in the same year, false otherwise
311+
*/
312+
isSameYear(other: Types.DateInput): boolean {
313+
return this.isSame(other, 'year')
314+
}
315+
316+
/**
317+
* Checks if this date is in the current month.
318+
* @returns True if this date is in the current month, false otherwise
319+
*/
320+
isThisMonth(): boolean {
321+
const now = new Date()
322+
return this.isSame(now, 'month')
323+
}
324+
325+
/**
326+
* Checks if this date is in the current quarter.
327+
* @returns True if this date is in the current quarter, false otherwise
328+
*/
329+
isThisQuarter(): boolean {
330+
const now = new Date()
331+
return this.isSame(now, 'quarter')
332+
}
333+
334+
/**
335+
* Checks if this date is in the current week.
336+
* @returns True if this date is in the current week, false otherwise
337+
*/
338+
isThisWeek(): boolean {
339+
const now = new Date()
340+
return this.isSame(now, 'week')
341+
}
342+
343+
/**
344+
* Checks if this date is in the current year.
345+
* @returns True if this date is in the current year, false otherwise
346+
*/
347+
isThisYear(): boolean {
348+
const now = new Date()
349+
return this.isSame(now, 'year')
350+
}
351+
352+
/**
353+
* Checks if this date is today.
354+
* @returns True if this date is today, false otherwise
355+
*/
356+
isToday(): boolean {
357+
const now = new Date()
358+
return this.isSame(now, 'day')
359+
}
360+
361+
/**
362+
* Checks if this date is tomorrow.
363+
* @returns True if this date is tomorrow, false otherwise
364+
*/
365+
isTomorrow(): boolean {
366+
const tomorrow = new Date()
367+
tomorrow.setDate(tomorrow.getDate() + 1)
368+
return this.isSame(tomorrow, 'day')
369+
}
370+
253371
/**
254372
* Checks if the date is valid.
255373
* @returns True if the date is valid, false otherwise
@@ -274,6 +392,16 @@ export class Daytime implements Types.IDaytime {
274392
return Helpers.isWeekend(this.date)
275393
}
276394

395+
/**
396+
* Checks if this date is yesterday.
397+
* @returns True if this date is yesterday, false otherwise
398+
*/
399+
isYesterday(): boolean {
400+
const yesterday = new Date()
401+
yesterday.setDate(yesterday.getDate() - 1)
402+
return this.isSame(yesterday, 'day')
403+
}
404+
277405
/**
278406
* Gets the ISO week number (1-53).
279407
* @returns The ISO week number (1-53)

src/Types.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,48 @@ export interface IDaytime {
7676
isBusinessDay(): boolean
7777
/** Checks if the date is in daylight saving time */
7878
isDST(): boolean
79+
/** Checks if the date is in the future */
80+
isFuture(): boolean
7981
/** Checks if the date is a leap year */
8082
isLeapYear(): boolean
83+
/** Checks if the date is in the past */
84+
isPast(): boolean
8185
/** Checks if the date is the same as another date */
8286
isSame(other: DateInput, unit?: TimeUnit): boolean
87+
/** Checks if the date is the same day as another date */
88+
isSameDay(other: DateInput): boolean
89+
/** Checks if the date is the same month as another date */
90+
isSameMonth(other: DateInput): boolean
8391
/** Checks if the date is the same as or after another date */
8492
isSameOrAfter(other: DateInput, unit?: TimeUnit): boolean
8593
/** Checks if the date is the same as or before another date */
8694
isSameOrBefore(other: DateInput, unit?: TimeUnit): boolean
95+
/** Checks if the date is the same quarter as another date */
96+
isSameQuarter(other: DateInput): boolean
97+
/** Checks if the date is the same week as another date */
98+
isSameWeek(other: DateInput): boolean
99+
/** Checks if the date is the same year as another date */
100+
isSameYear(other: DateInput): boolean
101+
/** Checks if the date is in the current month */
102+
isThisMonth(): boolean
103+
/** Checks if the date is in the current quarter */
104+
isThisQuarter(): boolean
105+
/** Checks if the date is in the current week */
106+
isThisWeek(): boolean
107+
/** Checks if the date is in the current year */
108+
isThisYear(): boolean
109+
/** Checks if the date is today */
110+
isToday(): boolean
111+
/** Checks if the date is tomorrow */
112+
isTomorrow(): boolean
87113
/** Checks if the date is valid */
88114
isValid(): boolean
89115
/** Checks if the date is in UTC */
90116
isUTC(): boolean
91117
/** Checks if the date is a weekend */
92118
isWeekend(): boolean
119+
/** Checks if the date is yesterday */
120+
isYesterday(): boolean
93121
/** Gets the ISO week number */
94122
isoWeek(): number
95123
/** Gets the ISO weekday number */

0 commit comments

Comments
 (0)