Skip to content

Commit

Permalink
Merge pull request #17 from bjuppa/optimize-tree-shaking
Browse files Browse the repository at this point in the history
Optimize tree shaking
  • Loading branch information
bjuppa authored May 24, 2023
2 parents b738c16 + 8bf6793 commit 52c0255
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 158 deletions.
47 changes: 42 additions & 5 deletions ExPlainDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ComPlainDate, PlainDate } from "./PlainDate.ts";
import { SloppyTime } from "./support/date-time-types.ts";
import { createLocalInstant } from "./utils/createLocalInstant.ts";
import { createInstant } from "./utils/createInstant.ts";
import { Quarter, WeekDay } from "./constants.ts";
import { QuarterNumber, WeekDay, WeekDayNumber } from "./constants.ts";
import { addDays } from "./utils/addDays.ts";
import { addBusinessDays } from "./utils/addBusinessDays.ts";
import { addMonths } from "./utils/addMonths.ts";
Expand Down 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,12 +46,22 @@ 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) */
quarter: () => Quarter;
quarter: () => QuarterNumber;
/** ISO weekday number (1-7) starting with Monday */
weekDayNumber: () => WeekDay;
weekDayNumber: () => WeekDayNumber;
/** Monday to Friday */
isBusinessDay: () => boolean;
/** Saturday or Sunday */
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 Expand Up @@ -229,5 +268,3 @@ export function ExPlainDate(

return exPlainDate;
}

ExPlainDate.fromString = PlainDate.fromString;
44 changes: 36 additions & 8 deletions ExPlainDate_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ Deno.test("can be converted to instant in given timezone", () => {
);
});

Deno.test("can be created from ISO string", () => {
assertEquals(String(ExPlainDate.fromString("2022-02-02")), "2022-02-02");
});

Deno.test("throws error when string only contains year part", () => {
assertThrows(() => ExPlainDate.fromString("2022"));
});

Deno.test("can be converted to instant in UTC", () => {
const exPlainDate = ExPlainDate({ year: 2022, month: 2, day: 2 });
const time = { hour: 23, minute: 59, second: 59, millisecond: 999 };
Expand All @@ -65,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
65 changes: 6 additions & 59 deletions PlainDate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SloppyDate, SloppyTime } from "./support/date-time-types.ts";
import { Month } from "./constants.ts";
import { MonthNumber } from "./constants.ts";
import { createUtcInstant } from "./utils/createUtcInstant.ts";
import { dateParts } from "./support/dateParts.ts";
import {
formatPlainDate,
FormatPlainDateOptions,
Expand All @@ -12,7 +11,7 @@ export interface ComPlainDate {
/** Year may be negative and up to 6 digits */
year: number;
/** Month (1-12) */
month: Month;
month: MonthNumber;
/** Day in month (1-31) */
day: number;

Expand All @@ -35,28 +34,19 @@ export interface ComPlainDate {
* @example
* ```ts
* // "6/12/2023"
* PlainDate.fromString('2023-06-12').toLocaleString('en');
* PlainDate({ year: 2023, month: 6, day: 12 }).toLocaleString('en');
*
* // "6/12"
* PlainDate.fromString('2023-06-12').toLocaleString('en', { month: 'numeric', day: 'numeric' });
* PlainDate({ year: 2023, month: 6, day: 12 }).toLocaleString('en', { month: 'numeric', day: 'numeric' });
*
* // "June 12"
* PlainDate.fromString('2023-06-12').toLocaleString('en', { month: 'long', day: 'numeric' });
* PlainDate({ year: 2023, month: 6, day: 12 }).toLocaleString('en', { month: 'long', day: 'numeric' });
* ```
*/
toLocaleString: (
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 @@ -85,11 +75,6 @@ export interface ComPlainDate {
/** Describes a factory function that creates plain-date objects */
export interface PlainDateFactory<T extends ComPlainDate> {
(x: SloppyDate): T;
/** Create a new plain-date object from an ISO string */
fromString: <T extends ComPlainDate>(
this: PlainDateFactory<T>,
s: string,
) => T;
}

/**
Expand All @@ -112,7 +97,7 @@ export function PlainDate(
constructor: PlainDate,

year: utcDate.getUTCFullYear(),
month: utcDate.getUTCMonth() + 1 as Month,
month: utcDate.getUTCMonth() + 1 as MonthNumber,
day: utcDate.getUTCDate(),

iso: utcDate.toISOString().split("T")[0],
Expand All @@ -129,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 All @@ -175,14 +133,3 @@ export function PlainDate(

return plainDate;
}

PlainDate.fromString = function <T extends ComPlainDate>(
this: PlainDateFactory<T>,
isoDateString: string,
): T {
const parts = dateParts(isoDateString);
if (!parts) {
throw TypeError(`No date parts found in string: ${isoDateString}`);
}
return this(parts);
};
44 changes: 0 additions & 44 deletions PlainDate_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,50 +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 created from ISO string", () => {
assertEquals(String(PlainDate.fromString("2022-02-02")), "2022-02-02");
});

Deno.test("throws error when string only contains year part", () => {
assertThrows(() => PlainDate.fromString("2022"));
});

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ComPlainDate is available as packages for both Deno and npm:
- [npmjs.com/package/complaindate](https://www.npmjs.com/package/complaindate)

The footprint of a tree-shaken and compressed production build starts below
`1.5 kB` when using just the `PlainDate` object API.
`1 kB` when using just the `PlainDate` object API.

## Quick example

Expand Down
64 changes: 35 additions & 29 deletions constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,43 @@ export const DAYS_IN_COMMON_YEAR = 365;
/** 366 */
export const DAYS_IN_LEAP_YEAR = DAYS_IN_COMMON_YEAR + 1;

export const WeekDay = {
MONDAY: 1,
TUESDAY: 2,
WEDNESDAY: 3,
THURSDAY: 4,
FRIDAY: 5,
SATURDAY: 6,
SUNDAY: 7,
} as const;

/** ISO weekday number (1-7) starting with Monday */
export enum WeekDay {
MONDAY = 1,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY,
}
export type WeekDayNumber = typeof WeekDay[keyof typeof WeekDay];

export const Month = {
JANUARY: 1,
FEBRUARY: 2,
MARCH: 3,
APRIL: 4,
MAY: 5,
JUNE: 6,
JULY: 7,
AUGUST: 8,
SEPTEMBER: 9,
OCTOBER: 10,
NOVEMBER: 11,
DECEMBER: 12,
} as const;

/** Month (1-12) */
export enum Month {
JANUARY = 1,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER,
}
export type MonthNumber = typeof Month[keyof typeof Month];

export const Quarter = {
Q1: 1,
Q2: 2,
Q3: 3,
Q4: 4,
} as const;

/** Quarter of year (1-4) */
export enum Quarter {
Q1 = 1,
Q2,
Q3,
Q4,
}
export type QuarterNumber = typeof Quarter[keyof typeof Quarter];
Loading

0 comments on commit 52c0255

Please sign in to comment.