diff --git a/dates/CHANGELOG.md b/dates/CHANGELOG.md index 58cb7c3..02f342a 100644 --- a/dates/CHANGELOG.md +++ b/dates/CHANGELOG.md @@ -4,6 +4,7 @@ - fix: added `@deno-types` to `DateTime` import - fix: removed `jsr:` prefix from ts examples +- fix: type errors for dates ## 2024-12-12 - 0.1.0 diff --git a/dates/dates.ts b/dates/dates.ts index 179e6d6..b9c9342 100644 --- a/dates/dates.ts +++ b/dates/dates.ts @@ -73,7 +73,7 @@ export const getNow = (): DateTime => DateTime.now() * getUnix(getNow()) * ``` */ -export const getUnix = (date?: DateTime): number => msToUnix((date || getNow()).valueOf()) +export const getUnix = (date?: DateTime): number => msToUnix(getDateTime(date).valueOf()) /** * Get ms (milliseconds) timestamp @@ -88,7 +88,14 @@ export const getUnix = (date?: DateTime): number => msToUnix((date || getNow()). * getMs(getNow()) * ``` */ -export const getMs = (date?: DateTime): number => (date || getNow()).valueOf() +export const getMs = (date?: DateTime): number => getDateTime(date).valueOf() + +/** + * Internal util to get DateTime object + * @param {DateTime} [date] - date object + * @returns {DateTime} DateTime object + */ +const getDateTime = (date?: DateTime): DateTime => (date instanceof DateTime ? date : getNow()) /** * Get ISO string in UTC timezone @@ -103,7 +110,7 @@ export const getMs = (date?: DateTime): number => (date || getNow()).valueOf() * getISO(getNow()) * ``` */ -export const getISO = (date?: DateTime): string => (date || getNow()).toUTC().toISO() +export const getISO = (date?: DateTime): string | null => getDateTime(date).toUTC().toISO() /** * Get ISO string (alias for {@link getISO}) @@ -140,7 +147,7 @@ export const getMsOffset = (ms: number): number => getMs() - ms * getRelative(getNow(), 'nl-NL') * ``` */ -export const getRelative = (date: DateTime, locale = 'en-US'): string => date.toRelative({ locale }) +export const getRelative = (date: DateTime, locale = 'en-US'): string | null => date.toRelative({ locale }) /** * Get year-month-day (YYYYMMDD or YYYY-MM-DD) @@ -157,7 +164,7 @@ export const getRelative = (date: DateTime, locale = 'en-US'): string => date.to * ``` */ export const getYearMonthDay = (date?: DateTime, withDashes = false): string | number => - withDashes ? (date || getNow()).toFormat('yyyy-LL-dd') : Number.parseInt((date || getNow()).toFormat('yyyyLLdd')) + withDashes ? getDateTime(date).toFormat('yyyy-LL-dd') : Number.parseInt(getDateTime(date).toFormat('yyyyLLdd')) /** * Parse ISO string diff --git a/deno.lock b/deno.lock index e649dc7..36e9573 100644 --- a/deno.lock +++ b/deno.lock @@ -9,6 +9,7 @@ "jsr:@std/internal@^1.0.5": "1.0.5", "npm:@biomejs/biome@1.9.4": "1.9.4", "npm:@biomejs/biome@^1.9.4": "1.9.4", + "npm:@types/luxon@*": "3.4.2", "npm:@types/luxon@^3.4.2": "3.4.2", "npm:@types/node@*": "22.5.4", "npm:@types/node@^22.10.2": "22.10.2",