Skip to content

Commit

Permalink
Move localized string methods to ExPlainDate
Browse files Browse the repository at this point in the history
  • Loading branch information
bjuppa committed May 24, 2023
1 parent fd49ffd commit 3fb7111
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 72 deletions.
39 changes: 39 additions & 0 deletions ExPlainDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { isLastDayOfMonth } from "./utils/isLastDayOfMonth.ts";
import { isFirstDayOfYear } from "./utils/isFirstDayOfYear.ts";
import { isLastDayOfYear } from "./utils/isLastDayOfYear.ts";
import { SloppyDate } from "./mod.ts";
import { formatPlainDate } from "./utils/formatPlainDate.ts";

/**
* Describes an extended plain-date object with extra properties and
Expand All @@ -45,6 +46,16 @@ export interface ExtendedPlainDate extends ComPlainDate {
*/
toInstant: (timezone: string, time?: SloppyTime) => Date;

toLocaleStringMedium: (locale?: Intl.LocalesArgument) => string;
toLocaleStringLong: (locale?: Intl.LocalesArgument) => string;
toLocaleStringFull: (locale?: Intl.LocalesArgument) => string;
dayName: (locale?: Intl.LocalesArgument) => string;
dayNameShort: (locale?: Intl.LocalesArgument) => string;
dayNameNarrow: (locale?: Intl.LocalesArgument) => string;
monthName: (locale?: Intl.LocalesArgument) => string;
monthNameShort: (locale?: Intl.LocalesArgument) => string;
monthNameNarrow: (locale?: Intl.LocalesArgument) => string;

/** Day of the year (1-366) */
ordinal: () => number;
/** Quarter of the year (1-4) */
Expand Down Expand Up @@ -123,6 +134,34 @@ export function ExPlainDate(
});
},

toLocaleStringMedium(locale = undefined) {
return formatPlainDate(locale)({ dateStyle: "medium" })(this);
},
toLocaleStringLong(locale = undefined) {
return formatPlainDate(locale)({ dateStyle: "long" })(this);
},
toLocaleStringFull(locale = undefined) {
return formatPlainDate(locale)({ dateStyle: "full" })(this);
},
dayName(locale = undefined) {
return formatPlainDate(locale)({ weekday: "long" })(this);
},
dayNameShort(locale = undefined) {
return formatPlainDate(locale)({ weekday: "short" })(this);
},
dayNameNarrow(locale = undefined) {
return formatPlainDate(locale)({ weekday: "narrow" })(this);
},
monthName(locale = undefined) {
return formatPlainDate(locale)({ month: "long" })(this);
},
monthNameShort(locale = undefined) {
return formatPlainDate(locale)({ month: "short" })(this);
},
monthNameNarrow(locale = undefined) {
return formatPlainDate(locale)({ month: "narrow" })(this);
},

ordinal() {
return ordinal(this);
},
Expand Down
36 changes: 36 additions & 0 deletions ExPlainDate_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,42 @@ Deno.test("can be converted to instant in UTC", () => {
);
});

Deno.test("day name can be localized", () => {
const exPlainDate = ExPlainDate({ year: 2020, month: 6, day: 13 });

assertEquals(exPlainDate.dayName("sv"), "lördag");
});

Deno.test("short day name can be localized", () => {
const exPlainDate = ExPlainDate({ year: 2020, month: 6, day: 13 });

assertEquals(exPlainDate.dayNameShort("sv"), "lör");
});

Deno.test("narrow day name can be localized", () => {
const exPlainDate = ExPlainDate({ year: 2020, month: 6, day: 13 });

assertEquals(exPlainDate.dayNameNarrow("sv"), "L");
});

Deno.test("month name can be localized", () => {
const exPlainDate = ExPlainDate({ year: 2020, month: 2, day: 13 });

assertEquals(exPlainDate.monthName("sv"), "februari");
});

Deno.test("short month name can be localized", () => {
const exPlainDate = ExPlainDate({ year: 2020, month: 2, day: 13 });

assertEquals(exPlainDate.monthNameShort("sv"), "feb.");
});

Deno.test("narrow month name can be localized", () => {
const exPlainDate = ExPlainDate({ year: 2020, month: 2, day: 13 });

assertEquals(exPlainDate.monthNameNarrow("sv"), "F");
});

Deno.test("Months and days can be added in any order with same result", () => {
// The next month only has 28 days
const exPlainDate = ExPlainDate({ year: 2022, month: 1, day: 31 });
Expand Down
36 changes: 0 additions & 36 deletions PlainDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ export interface ComPlainDate {
locale?: Intl.LocalesArgument,
options?: FormatPlainDateOptions,
) => string;
toLocaleStringMedium: (locale?: Intl.LocalesArgument) => string;
toLocaleStringLong: (locale?: Intl.LocalesArgument) => string;
toLocaleStringFull: (locale?: Intl.LocalesArgument) => string;
dayName: (locale?: Intl.LocalesArgument) => string;
dayNameShort: (locale?: Intl.LocalesArgument) => string;
dayNameNarrow: (locale?: Intl.LocalesArgument) => string;
monthName: (locale?: Intl.LocalesArgument) => string;
monthNameShort: (locale?: Intl.LocalesArgument) => string;
monthNameNarrow: (locale?: Intl.LocalesArgument) => string;

/**
* Get a native JS `Date` object in UTC.
Expand Down Expand Up @@ -123,33 +114,6 @@ export function PlainDate(
toLocaleString(locale = undefined, options = { dateStyle: "short" }) {
return formatPlainDate(locale)(options)(this);
},
toLocaleStringMedium(locale = undefined) {
return formatPlainDate(locale)({ dateStyle: "medium" })(this);
},
toLocaleStringLong(locale = undefined) {
return formatPlainDate(locale)({ dateStyle: "long" })(this);
},
toLocaleStringFull(locale = undefined) {
return formatPlainDate(locale)({ dateStyle: "full" })(this);
},
dayName(locale = undefined) {
return formatPlainDate(locale)({ weekday: "long" })(this);
},
dayNameShort(locale = undefined) {
return formatPlainDate(locale)({ weekday: "short" })(this);
},
dayNameNarrow(locale = undefined) {
return formatPlainDate(locale)({ weekday: "narrow" })(this);
},
monthName(locale = undefined) {
return formatPlainDate(locale)({ month: "long" })(this);
},
monthNameShort(locale = undefined) {
return formatPlainDate(locale)({ month: "short" })(this);
},
monthNameNarrow(locale = undefined) {
return formatPlainDate(locale)({ month: "narrow" })(this);
},

toUtcInstant({ hour = 0, minute = 0, second = 0, millisecond = 0 } = {}) {
return (hour || minute || second || millisecond)
Expand Down
36 changes: 0 additions & 36 deletions PlainDate_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,42 +65,6 @@ Deno.test("can be localized", () => {
);
});

Deno.test("day name can be localized", () => {
const plainDate = PlainDate({ year: 2020, month: 6, day: 13 });

assertEquals(plainDate.dayName("sv"), "lördag");
});

Deno.test("short day name can be localized", () => {
const plainDate = PlainDate({ year: 2020, month: 6, day: 13 });

assertEquals(plainDate.dayNameShort("sv"), "lör");
});

Deno.test("narrow day name can be localized", () => {
const plainDate = PlainDate({ year: 2020, month: 6, day: 13 });

assertEquals(plainDate.dayNameNarrow("sv"), "L");
});

Deno.test("month name can be localized", () => {
const plainDate = PlainDate({ year: 2020, month: 2, day: 13 });

assertEquals(plainDate.monthName("sv"), "februari");
});

Deno.test("short month name can be localized", () => {
const plainDate = PlainDate({ year: 2020, month: 2, day: 13 });

assertEquals(plainDate.monthNameShort("sv"), "feb.");
});

Deno.test("narrow month name can be localized", () => {
const plainDate = PlainDate({ year: 2020, month: 2, day: 13 });

assertEquals(plainDate.monthNameNarrow("sv"), "F");
});

Deno.test("can be converted to instant in UTC", () => {
const plainDate = PlainDate({ year: 2022, month: 2, day: 2 });
const time = { hour: 23, minute: 59, second: 59, millisecond: 999 };
Expand Down

0 comments on commit 3fb7111

Please sign in to comment.