-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make new constructions with years between 0-100 make dates in 1900s
This is consistent with how the builtin Date object works Fix padding in ISO strings to be consistent with Date objects
- Loading branch information
Showing
6 changed files
with
198 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,110 @@ | ||
export default DateOnly; | ||
/** A date object with without time */ | ||
export class DateOnly { | ||
/** | ||
* Crete a DateOnly Object from a number, numbers represent the days since | ||
* 1 Jan 1970 | ||
* @param {number} n The number to be converted to a DateOnly Object | ||
*/ | ||
static fromNumber(n: number): DateOnly; | ||
/** | ||
* Create a DateOnly Object from some string. Wraps new Date(str) | ||
* @param {string} s The string to read the date info from | ||
*/ | ||
static fromString(s: string): DateOnly; | ||
/** | ||
* Create a DateOnly Object from some string. Wraps new Date(str) | ||
* @param {Date} d The date to use in creating a new DateOnly object | ||
* using the date's UTC values | ||
*/ | ||
static fromDate(d: Date): DateOnly; | ||
/** | ||
* Create a new DateOnly object for the current date | ||
* @returns {DateOnly} A donly only object with the current local date | ||
*/ | ||
static now(): DateOnly; | ||
/** | ||
* @private | ||
* Check if an input returns a valid date object | ||
* @param {*} val some input to be passed to new Date and checked for validity | ||
*/ | ||
private static "__#1@#isDate"; | ||
/** | ||
* @private | ||
* Create an invaid DateOnly object | ||
* @returns {DateOnly} A date only object with it's invalid property set true | ||
*/ | ||
private static "__#1@#createInvalidDate"; | ||
/** | ||
* @constructor | ||
* Create a new DateOnly object | ||
* @param {number} [year=1970] The year the of the date | ||
* @param {number} [date=1] The year the of the date | ||
* @param {number} [month=1] The year the of the date | ||
*/ | ||
constructor(year?: number, month?: number, date?: number); | ||
/** | ||
* Return the year only from the DateOnly | ||
* @returns {number} | ||
*/ | ||
getFullYear(): number; | ||
/** | ||
* Return the month value from the DateOnly. | ||
* Indexed from 1, 12 is december | ||
* @returns {number} | ||
*/ | ||
getMonth(): number; | ||
/** | ||
* Return the date value, or the day of the month | ||
* Indexed from 1, max of 31 depending the month | ||
* @returns {number} | ||
*/ | ||
getDate(): number; | ||
/** | ||
* Return the year only from the DateOnly | ||
* @param {number} v The new desired value for the year | ||
*/ | ||
setFullYear(v: number): void; | ||
/** | ||
* Get day of the week | ||
*/ | ||
getDay(): number; | ||
/** | ||
* Return the month value from the DateOnly. | ||
* Indexed from 1, 12 is december | ||
* @param {number} v The new desired value for the value | ||
*/ | ||
setMonth(v: number): void; | ||
/** | ||
* Set the date value, or the day of the month | ||
* @param {number} v The new desired value for the date | ||
*/ | ||
setDate(v: number): void; | ||
/** | ||
* Set the date value, or the day of the month | ||
* @param {string} [sep=-] The seperator to use between date components | ||
* @returns {string} the date represented as a string | ||
*/ | ||
toString(sep?: string): string; | ||
/** | ||
* Return a string in ISO8601 format | ||
* All time values are set to zero | ||
* @returns {string} the date represented as an ISO8601 string | ||
*/ | ||
toISOString(): string; | ||
/** | ||
* JSON serializes the date to an ISO8601 date | ||
* @returns {string} the date represented as an ISO8601 string | ||
*/ | ||
toJSON(): string; | ||
/** | ||
* Returns a Date stirng | ||
*/ | ||
toDateString(): string; | ||
/** | ||
* Returns if the current date time object is valid or not | ||
*/ | ||
isValid(): boolean; | ||
#private; | ||
/** | ||
* Crete a DateOnly Object from a number, numbers represent the days since | ||
* 1 Jan 1970 | ||
* @param {number} n The number to be converted to a DateOnly Object | ||
*/ | ||
static fromNumber(n: number): DateOnly; | ||
/** | ||
* Create a DateOnly Object from some string. Wraps new Date(str) | ||
* @param {string} s The string to read the date info from | ||
*/ | ||
static fromString(s: string): DateOnly; | ||
/** | ||
* Create a DateOnly Object from some string. Wraps new Date(str) | ||
* @param {Date} d The date to use in creating a new DateOnly object | ||
* using the date's UTC values | ||
*/ | ||
static fromDate(d: Date): DateOnly; | ||
/** | ||
* Create a new DateOnly object for the current date | ||
* @returns {DateOnly} A donly only object with the current local date | ||
*/ | ||
static now(): DateOnly; | ||
/** | ||
* @private | ||
* Check if an input returns a valid date object | ||
* @param {*} val some input to be passed to new Date and checked for validity | ||
*/ | ||
private static "__#1@#isDate"; | ||
/** | ||
* @private | ||
* Create an invaid DateOnly object | ||
* @returns {DateOnly} A date only object with it's invalid property set true | ||
*/ | ||
private static "__#1@#createInvalidDate"; | ||
/** | ||
* @constructor | ||
* Create a new DateOnly object | ||
* @param {number} [year=1970] The year the of the date | ||
* @param {number} [date=1] The year the of the date | ||
* @param {number} [month=1] The year the of the date | ||
*/ | ||
constructor(year?: number, month?: number, date?: number); | ||
/** | ||
* Return the year only from the DateOnly | ||
* @returns {number} | ||
*/ | ||
getFullYear(): number; | ||
/** | ||
* Return the month value from the DateOnly. | ||
* Indexed from 1, 12 is december | ||
* @returns {number} | ||
*/ | ||
getMonth(): number; | ||
/** | ||
* Return the date value, or the day of the month | ||
* Indexed from 1, max of 31 depending the month | ||
* @returns {number} | ||
*/ | ||
getDate(): number; | ||
/** | ||
* Return the year only from the DateOnly | ||
* @param {number} v The new desired value for the year | ||
*/ | ||
setFullYear(v: number): void; | ||
/** | ||
* Get day of the week | ||
*/ | ||
getDay(): number; | ||
/** | ||
* Return the month value from the DateOnly. | ||
* Indexed from 1, 12 is december | ||
* @param {number} v The new desired value for the value | ||
*/ | ||
setMonth(v: number): void; | ||
/** | ||
* Set the date value, or the day of the month | ||
* @param {number} v The new desired value for the date | ||
*/ | ||
setDate(v: number): void; | ||
/** | ||
* Set the date value, or the day of the month | ||
* @param {string} [sep=-] The seperator to use between date components | ||
* @returns {string} the date represented as a string | ||
*/ | ||
toString(sep?: string): string; | ||
/** | ||
* Return a string in ISO8601 format | ||
* All time values are set to zero | ||
* @returns {string} the date represented as an ISO8601 string | ||
*/ | ||
toISOString(): string; | ||
/** | ||
* JSON serializes the date to an ISO8601 date | ||
* @returns {string} the date represented as an ISO8601 string | ||
*/ | ||
toJSON(): string; | ||
/** | ||
* Returns a Date stirng | ||
*/ | ||
toDateString(): string; | ||
/** | ||
* Returns if the current date time object is valid or not | ||
*/ | ||
isValid(): boolean; | ||
#private; | ||
} | ||
//# sourceMappingURL=index.d.ts.map | ||
//# sourceMappingURL=index.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { describe, expect, test } from "bun:test"; | ||
import { DateOnly } from "../lib"; | ||
|
||
describe("Creating strings from date only options", () => { | ||
const testCases = [ | ||
{ year: 2044, month: 3, date: 9, expected: "2044-03-09" }, | ||
{ year: 2044, month: 10, date: 9, expected: "2044-10-09" }, | ||
{ year: 2044, month: 3, date: 19, expected: "2044-03-19" }, | ||
{ year: 2044, month: 11, date: 22, expected: "2044-11-22" }, | ||
{ year: 44, month: 11, date: 22, expected: "1944-11-22" }, | ||
]; | ||
|
||
for (const tc of testCases) { | ||
test(`ToString: ${tc.expected}`, () => { | ||
const d = new DateOnly(tc.year, tc.month, tc.date); | ||
expect(d.toString()).toBe(tc.expected); | ||
}); | ||
} | ||
|
||
for (const tc of testCases) { | ||
test(`ISO String: ${tc.expected}`, () => { | ||
const d = new DateOnly(tc.year, tc.month, tc.date); | ||
expect(d.toISOString()).toBe(`${tc.expected}T00:00:00.000Z`); | ||
}); | ||
} | ||
|
||
test("ISO String: bad DateOnly", () => { | ||
const d = DateOnly.fromString("THIS WILL MAKE AN INVALID DateOnly kj;a!!!"); | ||
expect(d.toISOString()).toBe("Invalid DateOnly"); | ||
}); | ||
|
||
test("Date Strings", () => { | ||
const loops = 200; | ||
for (let k = 0; k < loops; k++) { | ||
let y = Math.ceil(Math.random() * 4000); | ||
const m = Math.ceil(Math.random() * 12); | ||
const day = Math.ceil(Math.random() * 28); | ||
|
||
if (Math.random() < 0.2) { | ||
y *= -1; | ||
} | ||
|
||
const dt = new Date(Date.UTC(y, m - 1, day, 0, 0, 0)); | ||
const d = new DateOnly(y, m, day); | ||
expect(d.toISOString()).toBe(dt.toISOString()); | ||
} | ||
}); | ||
}); |