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.

73 lines (71 loc) • 3.61 kB
import { isValidDate } from "../validate/isValidDate.js"; import { isOptionsArgument } from "../../internal/isObject.js"; import { resolveDateTimeUnit } from "../../internal/resolveDateTimeUnit.js"; import { resolveWeekStartsOn } from "../../internal/resolveWeekStartsOn.js"; import { startOfDate } from "../calculate/startOfDate.js"; import { areDatesEqual } from "./areDatesEqual.js"; // src/plain/compare/areDatesEqualBy.ts const supported = [ "year", "month", "week", "day" ]; /** * Compare two ISO date strings for equality at a given calendar unit. * * - Equality means both values share the same start-of-`unit` boundary, so * `"month"` requires the same month AND year — March 2023 and March 2024 are * NOT equal by month. A calendar month is a year and a month together, as in * Temporal's `PlainYearMonth`, not a bare month-of-year number. This is a * common point of confusion for callers expecting a bare "same month-of-year" * comparison. * - `"day"` matches `areDatesEqual` for PlainDate inputs only: `areDatesEqual` also accepts * PlainDateTime strings (`areDatesEqual("2024-01-01", "2024-01-01T10:00")` is `true`), while this * function accepts PlainDate strings only (`areDatesEqualBy` with those inputs and `"day"` is * `false`). * - `"year"`, `"month"`, `"week"` are computed via `startOfDate`. * - `unit` accepts the singular or plural name (`"day"` or `"days"`), as Temporal does. * - Returns false for an unsupported unit or invalid input. * * Migrating from date-fns: * - `isSameDay(a, b)` → `areDatesEqualBy(a, b, "day")` * - `isSameWeek(a, b)` → `areDatesEqualBy(a, b, "week", { weekStartsOn: "sunday" })`: date-fns * starts the week on Sunday (`weekStartsOn: 0`) by default, GMT on Monday * - `isSameWeek(a, b, { weekStartsOn: 1 })` → `areDatesEqualBy(a, b, "week")` * - `isSameMonth(a, b)` → `areDatesEqualBy(a, b, "month")` * - `isSameYear(a, b)` → `areDatesEqualBy(a, b, "year")` * * @param value1 first ISO date string * @param value2 second ISO date string * @param unit date unit to compare by ("year" | "month" | "week" | "day", or its plural) * @param optionsArg How the week is defined * @returns true if both dates share the same start-of-unit boundary, false on an unsupported unit or invalid input * * @example areDatesEqualBy("2024-03-15", "2024-03-20", "month") // true * @example areDatesEqualBy("2024-03-15", "2024-03-20", "months") // true * @example areDatesEqualBy("2024-03-15", "2024-03-20", "week", { weekStartsOn: "tuesday" }) // false (invalid weekStartsOn) * @example areDatesEqualBy("2023-03-15", "2024-03-15", "month") // false (same month, different year) * @example areDatesEqualBy("2024-03-15", "2024-03-16", "day") // false * @example areDatesEqualBy("2024-03-15", "2024-03-15", "hour" as never) // false (unsupported unit) * @example areDatesEqualBy("invalid", "2024-03-15", "month") // false */ function areDatesEqualBy(value1, value2, unit, optionsArg) { try { if (!isOptionsArgument(optionsArg)) return false; const resolvedUnit = resolveDateTimeUnit(unit); const weekStartsOn = resolveWeekStartsOn(optionsArg?.weekStartsOn); if (!isValidDate(value1) || !isValidDate(value2) || !supported.includes(resolvedUnit) || weekStartsOn === null) return false; if (resolvedUnit === "day") return areDatesEqual(value1, value2); try { const start1 = startOfDate(value1, resolvedUnit, { weekStartsOn }); const start2 = startOfDate(value2, resolvedUnit, { weekStartsOn }); return start1 !== "" && start1 === start2; } catch { return false; } } catch { return false; } } export { areDatesEqualBy };