Skip to content

A local-first personal finance app

[AI] Support Date inputs in current period checks (#9263)

Minh Vu15a2e4dbaa9d
diff --git a/packages/loot-core/src/shared/months.test.ts b/packages/loot-core/src/shared/months.test.ts
index c21bc9ffe9..295aea58e1 100644
--- a/packages/loot-core/src/shared/months.test.ts
+++ b/packages/loot-core/src/shared/months.test.ts
@@ -61,3 +61,26 @@ test('isValidYearMonthDay validates the day against the month and year', () => {
expect(monthUtils.isValidYearMonthDay('2024-02-00')).toBe(false);
expect(monthUtils.isValidYearMonthDay('2024-02')).toBe(false);
});
+
+test('isCurrentMonth matches valid local dates in the current month', () => {
+ expect(monthUtils.isCurrentMonth('2017-01')).toBe(true);
+ expect(monthUtils.isCurrentMonth('2017-01-15')).toBe(true);
+ expect(monthUtils.isCurrentMonth(new Date(2017, 0, 15, 12))).toBe(true);
+ expect(monthUtils.isCurrentMonth('2016-12')).toBe(false);
+ expect(monthUtils.isCurrentMonth(new Date(2016, 11, 31, 12))).toBe(false);
+ expect(monthUtils.isCurrentMonth('2016-13')).toBe(false);
+ expect(monthUtils.isCurrentMonth('2016-12-32')).toBe(false);
+ expect(monthUtils.isCurrentMonth('invalid')).toBe(false);
+ expect(monthUtils.isCurrentMonth(new Date(Number.NaN))).toBe(false);
+});
+
+test('isCurrentDay matches valid local dates on the current day', () => {
+ expect(monthUtils.isCurrentDay('2017-01-01')).toBe(true);
+ expect(monthUtils.isCurrentDay(new Date(2017, 0, 1, 12))).toBe(true);
+ expect(monthUtils.isCurrentDay('2016-12-31')).toBe(false);
+ expect(monthUtils.isCurrentDay(new Date(2016, 11, 31, 12))).toBe(false);
+ expect(monthUtils.isCurrentDay('2017-01')).toBe(false);
+ expect(monthUtils.isCurrentDay('2016-12-32')).toBe(false);
+ expect(monthUtils.isCurrentDay('invalid')).toBe(false);
+ expect(monthUtils.isCurrentDay(new Date(Number.NaN))).toBe(false);
+});
diff --git a/packages/loot-core/src/shared/months.ts b/packages/loot-core/src/shared/months.ts
index 19f621cb83..970465c445 100644
--- a/packages/loot-core/src/shared/months.ts
+++ b/packages/loot-core/src/shared/months.ts
@@ -249,11 +249,17 @@ export function isAfter(month1: DateLike, month2: DateLike): boolean {
}
export function isCurrentMonth(month: DateLike): boolean {
- return month === currentMonth();
+ const isValid =
+ typeof month === 'string'
+ ? isValidYearMonth(month) || isValidYearMonthDay(month)
+ : d.isValid(month);
+ return isValid && monthFromDate(month) === currentMonth();
}
export function isCurrentDay(day: DateLike): boolean {
- return day === currentDay();
+ const isValid =
+ typeof day === 'string' ? isValidYearMonthDay(day) : d.isValid(day);
+ return isValid && dayFromDate(day) === currentDay();
}
// TODO: This doesn't really fit in this module anymore, should
diff --git a/upcoming-release-notes/current-period-date-inputs.md b/upcoming-release-notes/current-period-date-inputs.md
new file mode 100644
index 0000000000..3e619e769a
--- /dev/null
+++ b/upcoming-release-notes/current-period-date-inputs.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [fallintoplace]
+---
+
+Handle Date values when checking the current month or day.