Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make startOfWeek locale-aware by default #3831

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions src/startOfWeek/index.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,59 @@
import { toDate } from "../toDate/index.js";
import type { LocalizedOptions, WeekOptions } from "../types.js";
import type { LocalizedOptions, WeekOptions, Locale } from "../types.js";
import { getDefaultOptions } from "../_lib/defaultOptions/index.js";
import { enUS } from "../locale/index.js";

/**
* The {@link startOfWeek} function options.
*/
export interface StartOfWeekOptions
extends LocalizedOptions<"options">,
WeekOptions {}
WeekOptions { }

/**
* @name startOfWeek
* @category Week Helpers
* @summary Return the start of a week for the given date.
*
* @description
* Return the start of a week for the given date.
* The result will be in the local timezone.
* @description
* Return the start of a week for the given date, considering the locale information
* provided in the options or default settings. The result will be in the local timezone.
*
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
*
* @param date - The original date
* @param options - An object with options
* * @property {Locale} [locale] - The locale object used to determine the week start day.
* If not provided, the default locale or `enUS` is used.
*
*
* @returns The start of a week
*
* @example
* // The start of a week for 2 September 2014 11:55:00:
* const result = startOfWeek(new Date(2014, 8, 2, 11, 55, 0))
* //=> Sun Aug 31 2014 00:00:00
*
* @example
* // If the week starts on Monday, the start of the week for 2 September 2014 11:55:00:
* const result = startOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 })
* //=> Mon Sep 01 2014 00:00:00
*/
 * @example

Check failure on line 32 in src/startOfWeek/index.ts

View workflow job for this annotation

GitHub Actions / tests

Irregular whitespace not allowed
 * // The start of the week for September 2nd, 2014, considering US English locale (Sunday start):

Check failure on line 33 in src/startOfWeek/index.ts

View workflow job for this annotation

GitHub Actions / tests

Irregular whitespace not allowed
 * const result = startOfWeek(new Date(2014, 8, 2))

Check failure on line 34 in src/startOfWeek/index.ts

View workflow job for this annotation

GitHub Actions / tests

Irregular whitespace not allowed
 * //=> Sun Aug 31 2014 00:00:00

Check failure on line 35 in src/startOfWeek/index.ts

View workflow job for this annotation

GitHub Actions / tests

Irregular whitespace not allowed
 *

Check failure on line 36 in src/startOfWeek/index.ts

View workflow job for this annotation

GitHub Actions / tests

Irregular whitespace not allowed
 * @example

Check failure on line 37 in src/startOfWeek/index.ts

View workflow job for this annotation

GitHub Actions / tests

Irregular whitespace not allowed
 * // The start of the week for September 2nd, 2014, considering Arabic (Egypt) locale (Saturday start):

Check failure on line 38 in src/startOfWeek/index.ts

View workflow job for this annotation

GitHub Actions / tests

Irregular whitespace not allowed
 * const result = startOfWeek(new Date(2014, 8, 2), { locale: arDZ })

Check failure on line 39 in src/startOfWeek/index.ts

View workflow job for this annotation

GitHub Actions / tests

Irregular whitespace not allowed
 * //=> Sat Aug 30 2014 00:00:00

Check failure on line 40 in src/startOfWeek/index.ts

View workflow job for this annotation

GitHub Actions / tests

Irregular whitespace not allowed
 */

Check failure on line 41 in src/startOfWeek/index.ts

View workflow job for this annotation

GitHub Actions / tests

Irregular whitespace not allowed
export function startOfWeek<DateType extends Date>(
date: DateType | number | string,
options?: StartOfWeekOptions,
): DateType {
const defaultOptions = getDefaultOptions();
const locale: Locale = options?.locale as Locale || defaultOptions.locale as Locale || enUS


const localeWeekStartsOn = locale.options?.weekStartsOn;


const weekStartsOn =
options?.weekStartsOn ??
options?.locale?.options?.weekStartsOn ??
localeWeekStartsOn ??
defaultOptions.weekStartsOn ??
defaultOptions.locale?.options?.weekStartsOn ??
0;

const _date = toDate(date);
Expand Down
12 changes: 12 additions & 0 deletions src/startOfWeek/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { describe, expect, it } from "vitest";
import { startOfWeek } from "./index.js";
import { enUS, fr } from "../locale/index.js";


describe("startOfWeek", () => {
it("returns the date with the time set to 00:00:00 and the date set to the first day of a week", () => {
Expand Down Expand Up @@ -84,3 +86,13 @@ describe("startOfWeek", () => {
expect(result instanceof Date && isNaN(result.getTime())).toBe(true);
});
});


it("allows to specify which day is the first day of the week in different locales", () => {
const date = new Date(2014, 8 /* Sep */, 2, 11, 55, 0);
const result_enUS = startOfWeek(date, { locale: enUS }); // Using en-US locale
expect(result_enUS).toEqual(new Date(2014, 7 /* Aug */, 31));

const result_fr = startOfWeek(date, { locale: fr }); // Using fr locale
expect(result_fr).toEqual(new Date(2014, 8 /* Sep */, 1));
});
Loading