Skip to content

Commit

Permalink
Move factory fromString to parsePlainDate util
Browse files Browse the repository at this point in the history
  • Loading branch information
bjuppa committed May 24, 2023
1 parent 940b8cc commit fd49ffd
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 38 deletions.
2 changes: 0 additions & 2 deletions ExPlainDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,3 @@ export function ExPlainDate(

return exPlainDate;
}

ExPlainDate.fromString = PlainDate.fromString;
8 changes: 0 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 Down
23 changes: 3 additions & 20 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 { MonthNumber } from "./constants.ts";
import { createUtcInstant } from "./utils/createUtcInstant.ts";
import { dateParts } from "./support/dateParts.ts";
import {
formatPlainDate,
FormatPlainDateOptions,
Expand Down Expand Up @@ -35,13 +34,13 @@ 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: (
Expand Down Expand Up @@ -85,11 +84,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 Down Expand Up @@ -175,14 +169,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);
};
8 changes: 0 additions & 8 deletions PlainDate_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ Deno.test("narrow month name can be localized", () => {
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
3 changes: 3 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export type {

export type { PlainDateMapFn } from "./support/function-signatures.ts";

// Utils for parsing strings
export { parsePlainDate } from "./utils/parsePlainDate.ts";

// Utils for splitting native JS `Date` objects into separate date & time objects
export { splitDateTime } from "./utils/splitDateTime.ts";
export { splitLocalDateTime } from "./utils/splitLocalDateTime.ts";
Expand Down
13 changes: 13 additions & 0 deletions utils/parsePlainDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ComPlainDate, PlainDate } from "../PlainDate.ts";
import { dateParts } from "../support/dateParts.ts";

/**
* Create a new plain-date object from an ISO date string.
*/
export function parsePlainDate(isoDateString: string): ComPlainDate {
const parts = dateParts(isoDateString);
if (!parts) {
throw TypeError(`No date parts found in string: ${isoDateString}`);
}
return PlainDate(parts);
}
10 changes: 10 additions & 0 deletions utils/parsePlainDate_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { parsePlainDate } from "./parsePlainDate.ts";
import { assertEquals, assertThrows } from "../dev_deps.ts";

Deno.test("can create plain-date from ISO string", () => {
assertEquals(String(parsePlainDate("2022-02-02")), "2022-02-02");
});

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

0 comments on commit fd49ffd

Please sign in to comment.