Skip to content

TagSpaces is an offline, open source, document manager with tagging support

chore: improving the date parsing functionalities, adding tests

Ilian Sapundshieva18480bc0d77
diff --git a/src/renderer/utils/dates.ts b/src/renderer/utils/dates.ts
index 175af59ce..65855b976 100644
--- a/src/renderer/utils/dates.ts
+++ b/src/renderer/utils/dates.ts
@@ -51,74 +51,142 @@ export function isYearPeriod(tagDate: string): boolean {
);
}
-/** Returns true if string is this format: 201512 */
+/** Validates that an 8-digit string (YYYYMMDD) represents a real calendar date */
+function isValidDatePart(yyyymmdd: string): boolean {
+ const year = parseInt(yyyymmdd.slice(0, 4), 10);
+ const month = parseInt(yyyymmdd.slice(4, 6), 10);
+ const day = parseInt(yyyymmdd.slice(6, 8), 10);
+ if (month < 1 || month > 12 || day < 1) return false;
+ return day <= getDaysInMonth(year, month);
+}
+
+/** Returns true if string is this format: 201512 (valid month 01-12) */
export function isYearMonth(tagDate: string): boolean {
- return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789])(\s|$)/.test(
- tagDate,
- );
+ if (
+ !/(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789])(\s|$)/.test(
+ tagDate,
+ )
+ ) {
+ return false;
+ }
+ const match = tagDate.trim();
+ const month = parseInt(match.slice(4, 6), 10);
+ return month >= 1 && month <= 12;
}
-/** Returns true if string is this format: 201512-201604 */
+/** Returns true if string is this format: 201512-201604 (valid months 01-12) */
export function isYearMonthPeriod(tagDate: string): boolean {
- return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789]-[0123][0123456789][0123456789][0123456789][01][0123456789])(\s|$)/.test(
- tagDate,
- );
+ if (
+ !/(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789]-[0123][0123456789][0123456789][0123456789][01][0123456789])(\s|$)/.test(
+ tagDate,
+ )
+ ) {
+ return false;
+ }
+ const t = tagDate.trim();
+ const m1 = parseInt(t.slice(4, 6), 10);
+ const m2 = parseInt(t.slice(11, 13), 10);
+ return m1 >= 1 && m1 <= 12 && m2 >= 1 && m2 <= 12;
}
-/** Returns true if string is this format: 20151223 */
+/** Returns true if string is this format: 20151223 (valid month 01-12 and day for that month) */
export function isYearMonthDay(tagDate: string): boolean {
- return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789])(\s|$)/.test(
- tagDate,
- );
+ if (
+ !/(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789])(\s|$)/.test(
+ tagDate,
+ )
+ ) {
+ return false;
+ }
+ return isValidDatePart(tagDate.trim().slice(0, 8));
}
-/** Returns true if string is this format: 20151223-20160223 */
+/** Returns true if string is this format: 20151223-20160223 (valid dates) */
export function isYearMonthDayPeriod(tagDate: string): boolean {
- return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789]-[0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789])(\s|$)/.test(
- tagDate,
- );
+ if (
+ !/(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789]-[0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789])(\s|$)/.test(
+ tagDate,
+ )
+ ) {
+ return false;
+ }
+ const t = tagDate.trim();
+ return isValidDatePart(t.slice(0, 8)) && isValidDatePart(t.slice(9, 17));
}
-/** Returns true if string is this format: 20151223~01 */
+/** Returns true if string is this format: 20151223~01 (valid date) */
export function isYearMonthDayHour(tagDate: string): boolean {
- return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789])(\s|$)/.test(
- tagDate,
- );
+ if (
+ !/(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789])(\s|$)/.test(
+ tagDate,
+ )
+ ) {
+ return false;
+ }
+ return isValidDatePart(tagDate.trim().slice(0, 8));
}
-/** Returns true if string is this format: 20190712~17-20190712~17 */
+/** Returns true if string is this format: 20190712~17-20190712~17 (valid dates) */
export function isYearMonthDayHourPeriod(tagDate: string): boolean {
- return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789]-[0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789])(\s|$)/.test(
- tagDate,
- );
+ if (
+ !/(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789]-[0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789])(\s|$)/.test(
+ tagDate,
+ )
+ ) {
+ return false;
+ }
+ const t = tagDate.trim();
+ return isValidDatePart(t.slice(0, 8)) && isValidDatePart(t.slice(12, 20));
}
-/** Returns true if string is this format: 20151223~0112 or 20151223T0112 */
+/** Returns true if string is this format: 20151223~0112 or 20151223T0112 (valid date) */
export function isYearMonthDayHourMin(tagDate: string): boolean {
- return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789][0123456][0123456789])(\s|$)/.test(
- tagDate,
- );
+ if (
+ !/(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789][0123456][0123456789])(\s|$)/.test(
+ tagDate,
+ )
+ ) {
+ return false;
+ }
+ return isValidDatePart(tagDate.trim().slice(0, 8));
}
-/** Returns true if string is this format: 20190712~1740-20190712~1740 */
+/** Returns true if string is this format: 20190712~1740-20190712~1740 (valid dates) */
export function isYearMonthDayHourMinPeriod(tagDate: string): boolean {
- return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789][0123456][0123456789]-[0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789][0123456][0123456789])(\s|$)/.test(
- tagDate,
- );
+ if (
+ !/(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789][0123456][0123456789]-[0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789][0123456][0123456789])(\s|$)/.test(
+ tagDate,
+ )
+ ) {
+ return false;
+ }
+ const t = tagDate.trim();
+ return isValidDatePart(t.slice(0, 8)) && isValidDatePart(t.slice(14, 22));
}
-/** Returns true if string is this format: 20151223~011358 */
+/** Returns true if string is this format: 20151223~011358 (valid date) */
export function isYearMonthDayHourMinSec(tagDate: string): boolean {
- return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789][0123456][0123456789][0123456][0123456789])(\s|$)/.test(
- tagDate,
- );
+ if (
+ !/(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789][0123456][0123456789][0123456][0123456789])(\s|$)/.test(
+ tagDate,
+ )
+ ) {
+ return false;
+ }
+ return isValidDatePart(tagDate.trim().slice(0, 8));
}
-/** Returns true if string is this format: 20190712~174031-20190712~174031 */
+/** Returns true if string is this format: 20190712~174031-20190712~174031 (valid dates) */
export function isYearMonthDayHourMinSecPeriod(tagDate: string): boolean {
- return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789][0123456][0123456789][0123456][0123456789]-[0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789][0123456][0123456789][0123456][0123456789])(\s|$)/.test(
- tagDate,
- );
+ if (
+ !/(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789][0123456][0123456789][0123456][0123456789]-[0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789][~T][0123456][0123456789][0123456][0123456789][0123456][0123456789])(\s|$)/.test(
+ tagDate,
+ )
+ ) {
+ return false;
+ }
+ const t = tagDate.trim();
+ return isValidDatePart(t.slice(0, 8)) && isValidDatePart(t.slice(16, 24));
}
/** Returns the number of day in month, January = 1 -> 31 .. December = 12 */
diff --git a/tests/unit/dates.test.js b/tests/unit/dates.test.js
new file mode 100644
index 000000000..73c7a8aad
--- /dev/null
+++ b/tests/unit/dates.test.js
@@ -0,0 +1,331 @@
+import {
+ isDateTimeTag,
+ isYear,
+ isYearPeriod,
+ isYearMonth,
+ isYearMonthPeriod,
+ isYearMonthDay,
+ isYearMonthDayPeriod,
+ isYearMonthDayHour,
+ isYearMonthDayHourPeriod,
+ isYearMonthDayHourMin,
+ isYearMonthDayHourMinPeriod,
+ isYearMonthDayHourMinSec,
+ isYearMonthDayHourMinSecPeriod,
+ getDaysInMonth,
+} from '-/utils/dates';
+import { describe, expect, test } from '@playwright/test';
+
+describe('Date Utilities', () => {
+ describe('getDaysInMonth', () => {
+ test('January has 31 days', () => {
+ expect(getDaysInMonth(2026, 1)).toBe(31);
+ });
+
+ test('February has 28 days in non-leap year', () => {
+ expect(getDaysInMonth(2025, 2)).toBe(28);
+ });
+
+ test('February has 29 days in leap year', () => {
+ expect(getDaysInMonth(2024, 2)).toBe(29);
+ });
+
+ test('April has 30 days', () => {
+ expect(getDaysInMonth(2026, 4)).toBe(30);
+ });
+ });
+
+ describe('isYear', () => {
+ test('valid year 2025', () => {
+ expect(isYear('2025')).toBe(true);
+ });
+
+ test('valid year 2000', () => {
+ expect(isYear('2000')).toBe(true);
+ });
+
+ test('rejects non-numeric', () => {
+ expect(isYear('abcd')).toBe(false);
+ });
+
+ test('rejects 5-digit number', () => {
+ expect(isYear('20251')).toBe(false);
+ });
+ });
+
+ describe('isYearPeriod', () => {
+ test('valid period 2015-2017', () => {
+ expect(isYearPeriod('2015-2017')).toBe(true);
+ });
+
+ test('rejects single year', () => {
+ expect(isYearPeriod('2015')).toBe(false);
+ });
+ });
+
+ describe('isYearMonth', () => {
+ test('valid 202512', () => {
+ expect(isYearMonth('202512')).toBe(true);
+ });
+
+ test('valid 202501 (January)', () => {
+ expect(isYearMonth('202501')).toBe(true);
+ });
+
+ test('rejects month 00', () => {
+ expect(isYearMonth('202500')).toBe(false);
+ });
+
+ test('rejects month 13', () => {
+ expect(isYearMonth('202513')).toBe(false);
+ });
+
+ test('rejects month 19', () => {
+ expect(isYearMonth('202519')).toBe(false);
+ });
+ });
+
+ describe('isYearMonthPeriod', () => {
+ test('valid 201512-201604', () => {
+ expect(isYearMonthPeriod('201512-201604')).toBe(true);
+ });
+
+ test('rejects invalid from-month 201500-201604', () => {
+ expect(isYearMonthPeriod('201500-201604')).toBe(false);
+ });
+
+ test('rejects invalid to-month 201512-201613', () => {
+ expect(isYearMonthPeriod('201512-201613')).toBe(false);
+ });
+ });
+
+ describe('isYearMonthDay', () => {
+ test('valid 20251223', () => {
+ expect(isYearMonthDay('20251223')).toBe(true);
+ });
+
+ test('valid 20260101 (Jan 1)', () => {
+ expect(isYearMonthDay('20260101')).toBe(true);
+ });
+
+ test('valid 20260228 (Feb 28 non-leap)', () => {
+ expect(isYearMonthDay('20260228')).toBe(true);
+ });
+
+ test('valid 20240229 (Feb 29 leap year)', () => {
+ expect(isYearMonthDay('20240229')).toBe(true);
+ });
+
+ test('rejects 20250229 (Feb 29 non-leap year)', () => {
+ expect(isYearMonthDay('20250229')).toBe(false);
+ });
+
+ test('rejects day 00', () => {
+ expect(isYearMonthDay('20260300')).toBe(false);
+ });
+
+ test('rejects day 32 in January', () => {
+ expect(isYearMonthDay('20260132')).toBe(false);
+ });
+
+ test('rejects day 34 (20260334)', () => {
+ expect(isYearMonthDay('20260334')).toBe(false);
+ });
+
+ test('rejects month 13', () => {
+ expect(isYearMonthDay('20261301')).toBe(false);
+ });
+
+ test('rejects day 31 in April (30-day month)', () => {
+ expect(isYearMonthDay('20260431')).toBe(false);
+ });
+
+ test('rejects month 00', () => {
+ expect(isYearMonthDay('20260001')).toBe(false);
+ });
+ });
+
+ describe('isYearMonthDayPeriod', () => {
+ test('valid 20151223-20160223', () => {
+ expect(isYearMonthDayPeriod('20151223-20160223')).toBe(true);
+ });
+
+ test('rejects invalid from-date 20260334-20260401', () => {
+ expect(isYearMonthDayPeriod('20260334-20260401')).toBe(false);
+ });
+
+ test('rejects invalid to-date 20260301-20260334', () => {
+ expect(isYearMonthDayPeriod('20260301-20260334')).toBe(false);
+ });
+ });
+
+ describe('isYearMonthDayHour', () => {
+ test('valid 20251223~01', () => {
+ expect(isYearMonthDayHour('20251223~01')).toBe(true);
+ });
+
+ test('valid with T separator 20251223T14', () => {
+ expect(isYearMonthDayHour('20251223T14')).toBe(true);
+ });
+
+ test('rejects invalid date 20260334~12', () => {
+ expect(isYearMonthDayHour('20260334~12')).toBe(false);
+ });
+ });
+
+ describe('isYearMonthDayHourPeriod', () => {
+ test('valid 20190712~17-20190712~17', () => {
+ expect(isYearMonthDayHourPeriod('20190712~17-20190712~17')).toBe(true);
+ });
+
+ test('rejects invalid from-date 20260334~17-20260401~17', () => {
+ expect(isYearMonthDayHourPeriod('20260334~17-20260401~17')).toBe(false);
+ });
+
+ test('rejects invalid to-date 20260301~17-20260334~17', () => {
+ expect(isYearMonthDayHourPeriod('20260301~17-20260334~17')).toBe(false);
+ });
+ });
+
+ describe('isYearMonthDayHourMin', () => {
+ test('valid 20251223~0112', () => {
+ expect(isYearMonthDayHourMin('20251223~0112')).toBe(true);
+ });
+
+ test('valid with T separator 20251223T0112', () => {
+ expect(isYearMonthDayHourMin('20251223T0112')).toBe(true);
+ });
+
+ test('rejects invalid date 20260334~0112', () => {
+ expect(isYearMonthDayHourMin('20260334~0112')).toBe(false);
+ });
+ });
+
+ describe('isYearMonthDayHourMinPeriod', () => {
+ test('valid 20190712~1740-20190712~1740', () => {
+ expect(isYearMonthDayHourMinPeriod('20190712~1740-20190712~1740')).toBe(
+ true,
+ );
+ });
+
+ test('rejects invalid from-date', () => {
+ expect(
+ isYearMonthDayHourMinPeriod('20260334~1740-20260401~1740'),
+ ).toBe(false);
+ });
+
+ test('rejects invalid to-date', () => {
+ expect(
+ isYearMonthDayHourMinPeriod('20260301~1740-20260334~1740'),
+ ).toBe(false);
+ });
+ });
+
+ describe('isYearMonthDayHourMinSec', () => {
+ test('valid 20251223~011358', () => {
+ expect(isYearMonthDayHourMinSec('20251223~011358')).toBe(true);
+ });
+
+ test('valid with T separator 20251223T011358', () => {
+ expect(isYearMonthDayHourMinSec('20251223T011358')).toBe(true);
+ });
+
+ test('rejects invalid date 20260334~011358', () => {
+ expect(isYearMonthDayHourMinSec('20260334~011358')).toBe(false);
+ });
+ });
+
+ describe('isYearMonthDayHourMinSecPeriod', () => {
+ test('valid 20190712~174031-20190712~174031', () => {
+ expect(
+ isYearMonthDayHourMinSecPeriod('20190712~174031-20190712~174031'),
+ ).toBe(true);
+ });
+
+ test('rejects invalid from-date', () => {
+ expect(
+ isYearMonthDayHourMinSecPeriod('20260334~174031-20260401~174031'),
+ ).toBe(false);
+ });
+
+ test('rejects invalid to-date', () => {
+ expect(
+ isYearMonthDayHourMinSecPeriod('20260301~174031-20260334~174031'),
+ ).toBe(false);
+ });
+ });
+
+ describe('isDateTimeTag', () => {
+ test('recognizes year', () => {
+ expect(isDateTimeTag('2025')).toBe(true);
+ });
+
+ test('recognizes year-month', () => {
+ expect(isDateTimeTag('202503')).toBe(true);
+ });
+
+ test('recognizes year-month-day', () => {
+ expect(isDateTimeTag('20250315')).toBe(true);
+ });
+
+ test('recognizes datetime with tilde', () => {
+ expect(isDateTimeTag('20250315~1430')).toBe(true);
+ });
+
+ test('recognizes datetime with T', () => {
+ expect(isDateTimeTag('20250315T143059')).toBe(true);
+ });
+
+ test('recognizes year period', () => {
+ expect(isDateTimeTag('2020-2025')).toBe(true);
+ });
+
+ test('rejects invalid day 34', () => {
+ expect(isDateTimeTag('20260334')).toBe(false);
+ });
+
+ test('rejects Feb 29 in non-leap year', () => {
+ expect(isDateTimeTag('20250229')).toBe(false);
+ });
+
+ test('accepts Feb 29 in leap year', () => {
+ expect(isDateTimeTag('20240229')).toBe(true);
+ });
+
+ test('rejects month 13', () => {
+ expect(isDateTimeTag('20261301')).toBe(false);
+ });
+
+ test('rejects month 00', () => {
+ expect(isDateTimeTag('202600')).toBe(false);
+ });
+
+ test('rejects invalid date in datetime format', () => {
+ expect(isDateTimeTag('20260334~1430')).toBe(false);
+ });
+
+ test('rejects non-date string', () => {
+ expect(isDateTimeTag('viewer')).toBe(false);
+ });
+
+ test('rejects alphanumeric', () => {
+ expect(isDateTimeTag('abc123')).toBe(false);
+ });
+
+ test('rejects April 31', () => {
+ expect(isDateTimeTag('20260431')).toBe(false);
+ });
+
+ test('accepts April 30', () => {
+ expect(isDateTimeTag('20260430')).toBe(true);
+ });
+
+ test('rejects Jun 31', () => {
+ expect(isDateTimeTag('20260631')).toBe(false);
+ });
+
+ test('accepts Dec 31', () => {
+ expect(isDateTimeTag('20261231')).toBe(true);
+ });
+ });
+});