UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

72 lines (70 loc) • 4.15 kB
import { plainDateOf } from "../../internal/inputChecks.js"; import { resolveLocale } from "../../internal/resolveLocale.js"; import { getLocaleFirstDayOfWeek } from "../../internal/getLocaleFirstDayOfWeek.js"; import { isDateUnitName } from "../../internal/unitNames.js"; import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js"; import { localeStartOfWeek, sameDateBy } from "../../internal/plainDateSteps.js"; import { todayDate } from "../../internal/todayDate.js"; // src/plain/compare/isThisUnit.ts /** * Return true when `value` falls in the same `unit` as today, per the * system clock and system timeZone. * * - Subsumes `isThisWeek`/`isThisMonth`/`isThisYear`: `unit` is drawn from * the same `Temporal.DateUnit` `areDatesEqualBy` uses. * - `"day"` is equivalent to `isRelativeDay(value, 0)`. * - `locale` only affects the `"week"` case — which day the week starts on * varies by locale (e.g. fr-FR: Monday, pt-BR: Sunday). When `unit` is * `"week"` and `locale` is given, the comparison uses `getLocaleStartOfWeek` * instead of the ISO Monday-start default `areDatesEqualBy` otherwise uses. * - Compares against today, so this depends on the **system clock and * system timeZone**. A caller needing determinism should use * `isZonedThisUnit` with an explicit timeZone, or compare against an * explicit reference with `areDatesEqualBy`. * - `unit` accepts the singular or plural name (`"month"` or `"months"`), as Temporal does. * - `locale` may be a BCP 47 tag or a preference list of tags (ECMA-402); the first tag with locale * data sets the week start. * - Returns false for an unsupported unit or invalid input. The locale is validated for every * unit, so an invalid locale returns false for `"day"`, `"month"` and `"year"` too. * - **Compatibility:** before 1.16.0 an invalid locale was ignored for every unit but `"week"`. * * Migrating from date-fns: * - `isThisWeek(value)` → `isThisUnit(value, "week", "pt-BR")`, or any locale whose week starts on * Sunday: date-fns starts the week on Sunday (`weekStartsOn: 0`) by default, GMT on Monday * - `isThisWeek(value, { weekStartsOn: 1 })` → `isThisUnit(value, "week")` * - `isThisMonth(value)` → `isThisUnit(value, "month")` * - `isThisYear(value)` → `isThisUnit(value, "year")` * * @param value ISO PlainDate string * @param unit date unit to compare by ("year" | "month" | "week" | "day", or its plural) * @param locale optional BCP 47 locale tag or preference list — sets the "week" start (e.g. "fr-FR", ["pt-BR", "fr-FR"]) * @returns true if `value` falls in the same `unit` as today, false on an unsupported unit or invalid input * * @example isThisUnit("2024-03-15", "month") // true, if today is any day in March 2024 * @example isThisUnit("2024-03-15", "year") // true, if today is any day in 2024 * @example isThisUnit("2024-02-26", "week", "fr-FR") // true, if today is 2024-03-01 (same fr-FR Monday-start week) * @example isThisUnit("2024-03-15", "hour" as never) // false (unsupported unit) * @example isThisUnit("invalid", "month") // false * @example isThisUnit("2024-03-15", "week", "not-a-locale-!!") // false (invalid locale) * @example isThisUnit("2024-03-15", "day", "not-a-locale-!!") // false (invalid locale, for every unit) * @example isThisUnit("2024-03-15", "months") // true, if today is any day in March 2024 * @example isThisUnit("2024-02-25", "week", ["pt-BR", "fr-FR"]) // true, if today is 2024-02-29 (pt-BR Sunday-start week) */ function isThisUnit(value, unit, locale) { try { const resolvedUnit = resolveDateTimeUnit(unit); if (!isDateUnitName(resolvedUnit) || locale !== void 0 && resolveLocale(locale) === null) return false; const today = todayDate(); const date = plainDateOf(value); if (today === null || date === null) return false; if (resolvedUnit === "week" && locale !== void 0) { const firstDay = getLocaleFirstDayOfWeek(locale); if (firstDay === null) return false; return localeStartOfWeek(date, firstDay).equals(localeStartOfWeek(today, firstDay)); } return sameDateBy(date, today, resolvedUnit, "monday"); } catch { return false; } } export { isThisUnit };