Skip to content

Commit

Permalink
Merge pull request #53 from bjuppa/rework-formatInstant
Browse files Browse the repository at this point in the history
Rework format instant
  • Loading branch information
bjuppa authored Jul 4, 2023
2 parents 2781e9e + 116a1b4 commit fe93971
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
9 changes: 5 additions & 4 deletions utils/formatInstant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export type FormatInstantOptions = Omit<Intl.DateTimeFormatOptions, "timeZone">;
* 1. `Intl` {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument | locale },
* defaults to system's locale if not given.
* 2. `Intl` {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat | format options },
* defaults to include a "short" timezone-style if no options given.
* 3. A named IANA timezone.
* defaults to include a "short" timezone-style unless `timeZoneName` is explicitly `undefined`.
* 3. A named IANA timezone, defaults to system's timezone if not given.
*
* @param locale `Intl` locale,
* @returns A curried function that takes `Intl` format options and returns another curried function that takes a timezone and returns the final curried function that operates on JS `Date` objects
Expand All @@ -25,11 +25,12 @@ export type FormatInstantOptions = Omit<Intl.DateTimeFormatOptions, "timeZone">;
* format24hDateTime(new Date()); // "6/11/2023, 16:54:32"
* ```
*/
export function formatInstant(locale: Intl.LocalesArgument = undefined): (
export function formatInstant(locale?: Intl.LocalesArgument): (
options?: FormatInstantOptions,
) => (timezone: string) => (instant: Date) => string {
) => (timezone?: string) => (instant: Date) => string {
return (options = { timeZoneName: "short" }) => (timezone) => (instant) =>
instant.toLocaleString(locale, {
timeZoneName: "short",
...options,
timeZone: timezone,
});
Expand Down
32 changes: 30 additions & 2 deletions utils/formatInstant_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,41 @@ Deno.test("takes Intl options", () => {
);
});

Deno.test("ignores timezone in options", () => {
Deno.test("adds timeZoneName to options if left out", () => {
const instant = new Date("2023-06-10T13:20:30+0100");
assertEquals(
formatInstant("en")({ hourCycle: "h23" })("Europe/London")(
instant,
),
"6/10/2023, 13:20:30 GMT+1",
);
});

Deno.test("doesn't add timeZoneName when explicitly set to undefined", () => {
const instant = new Date("2023-06-10T13:20:30+0100");
assertEquals(
formatInstant("en")({ timeZoneName: undefined })("Europe/London")(
instant,
),
"6/10/2023, 1:20:30 PM",
);
});

Deno.test("ignores timeZone in options", () => {
const instant = new Date("2023-06-10T13:20:30+0100");
assertEquals(
// @ts-ignore: Pass timeZone even if not allowed by type
formatInstant("en")({ timeZone: "America/Chicago" })("Europe/London")(
instant,
),
"6/10/2023, 1:20:30 PM",
"6/10/2023, 1:20:30 PM GMT+1",
);
});

Deno.test("accepts leaving all parameters empty for system defaults and short timezone name", () => {
const instant = new Date("2023-06-10T13:20:30+0100");
assertEquals(
formatInstant()()()(instant),
instant.toLocaleString(undefined, { timeZoneName: "short" }),
);
});

0 comments on commit fe93971

Please sign in to comment.