diff --git a/src/stores/calendar.ts b/src/stores/calendar.ts index c785e50..dcd4bd1 100644 --- a/src/stores/calendar.ts +++ b/src/stores/calendar.ts @@ -2,6 +2,7 @@ import { computed, type Ref, type ComputedRef, ref } from 'vue' import { defineStore } from 'pinia' import { useUrlSearchParams } from '@vueuse/core' import type { LeimDate, LeimPeriod, LeimPeriodShort } from '@/models/Date' +import { isDigit, isInt, isSignedInt } from '@/utils/Regex' type CalendarViewType = 'month' | 'year' | 'decade' | 'century' @@ -73,7 +74,7 @@ export const useCalendar = defineStore('calendar', () => { // Get date from URL params const params = useUrlSearchParams('history', { - removeNullishValues: true + write: true }) // Default date settings (current day) @@ -90,13 +91,13 @@ export const useCalendar = defineStore('calendar', () => { }) // Assign default values if no params exist in URL - if (!params.day) { + if (!params.day || typeof params.day === 'object' || !isInt(params.day)) { params.day = defaultDay.toString() } - if (!params.month) { + if (!params.month || typeof params.month === 'object' || !isDigit(params.month)) { params.month = defaultMonth.toString() } - if (!params.year) { + if (!params.year || typeof params.year === 'object' || !isSignedInt(params.year)) { params.year = defaultYear.toString() } diff --git a/src/utils/Regex.ts b/src/utils/Regex.ts new file mode 100644 index 0000000..c43f0d1 --- /dev/null +++ b/src/utils/Regex.ts @@ -0,0 +1,29 @@ +/** + * Check if the string parses as single digit + * + * @param str String to test + * @returns Result of the test + */ +export function isDigit(str: string) { + return RegExp(/^\d{1}$/g).test(str) +} + +/** + * Check if the string parses as an int + * + * @param str String to test + * @returns Result of the test + */ +export function isInt(str: string) { + return RegExp(/^[0-9]*$/g).test(str) +} + +/** + * Check if the string parses as a signed int (negative or positive) + * + * @param str String to test + * @returns Result of the test + */ +export function isSignedInt(str: string) { + return RegExp(/^[-+]?[0-9]+$/g).test(str) +}