Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Publish v0.3.0-alpha.12
Browse files Browse the repository at this point in the history
- move time to ts
  • Loading branch information
soerenmeier committed Mar 18, 2024
1 parent 04e4630 commit 1363576
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 541 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fire-lib-js",
"version": "0.3.0-alpha.11",
"version": "0.3.0-alpha.12",
"author": "Sören Meier <[email protected]>",
"type": "module",
"scripts": {
Expand Down
116 changes: 0 additions & 116 deletions src/time/Date.d.ts

This file was deleted.

67 changes: 39 additions & 28 deletions src/time/Date.js → src/time/Date.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/**
* @module time/date
*/

import { padZero } from '../utils/utils.js';
import { fromAny } from './localization.js';
import { padZero } from '../utils/utils';
import { fromAny } from './localization';

const DAY_IN_MS = 1000 * 60 * 60 * 24;

function isDateObject(val) {
function isDateObject(val: any): val is Date {
return typeof (val ? val.__isDateObject__ : null) === 'function';
}

Expand All @@ -17,9 +13,15 @@ function isDateObject(val) {
* @exports time/date/Date
* */
export default class Date {
raw: globalThis.Date;

static __parsetype__() {}
__isDateObject__() {}
static parse(val) {

/**
* Tries to creates a new Date instance
*/
static parse(val: any) {
if (
typeof val !== 'string' &&
typeof val !== 'number' &&
Expand All @@ -33,12 +35,20 @@ export default class Date {
/**
* Create a new Date instance
* @constructor
* @param {Date|string|number} date - The date value. Default is
* @param date - The date value. Default is
* current date.
* If it's a string or number, convert it to a date.
* @throws {Error} Will throw an error if date is an invalid Date.
*/
constructor(date = null) {
* @throws Will throw an error if date is an invalid Date.
*/
constructor(
date:
| Date
| globalThis.Date
| string
| number
| null
| undefined = null,
) {
if (typeof date === 'undefined' || date === null) {
this.raw = new globalThis.Date();
this.raw.setHours(0, 0, 0, 0);
Expand All @@ -56,17 +66,18 @@ export default class Date {

this.raw = new globalThis.Date(date);

// @ts-ignore
if (isNaN(this.raw)) {
throw new Error('invalid Date');
}
}

/**
* Check if the date represented by this instance is today
* @returns {boolean} True if the date represented by this instance is
* @returns True if the date represented by this instance is
* today, otherwise false
*/
isToday() {
isToday(): boolean {
const today = new Date();
return (
this.year == today.year &&
Expand All @@ -79,31 +90,31 @@ export default class Date {
* Get the year from the date ex: 2023
* @returns {number} The year in which the date occurs
*/
get year() {
get year(): number {
return this.raw.getFullYear();
}

/**
* Get the month from the date (0 indexed)
* @returns {number} The month in which the date occurs, zero-indexed
*/
get month() {
get month(): number {
return this.raw.getMonth();
}

/**
* Get the date (day of the month 1-31)
* @returns {number} The day of the month on which the date occurs
*/
get date() {
get date(): number {
return this.raw.getDate();
}

/**
* Get the week of the year
* @returns {number} The week of the year in which the date occurs
*/
get week() {
get week(): number {
const nearestThursday = this.clone();
nearestThursday.raw.setDate(this.date + 4 - this.dayMoToSu);
const firstDay = new Date(new globalThis.Date(this.year, 0, 1));
Expand All @@ -116,7 +127,7 @@ export default class Date {
* @returns {number} The day of the week on which the date occurs,
* zero-indexed with 0 for Sunday
*/
get day() {
get day(): number {
return this.raw.getDay();
}

Expand All @@ -125,15 +136,15 @@ export default class Date {
* @returns {number} The day of the week on which the date occurs, with 1
* for Monday and 7 for Sunday
*/
get dayMoToSu() {
get dayMoToSu(): number {
return this.day || 7;
}

/**
* Get the number of milliseconds since 1 January 1970 00:00:00 UTC
* @returns {number} The number of milliseconds since the Unix Epoch
*/
get time() {
get time(): number {
return this.raw.getTime();
}

Expand All @@ -142,7 +153,7 @@ export default class Date {
* @returns {Date} A new Date object with the same year, month, and
* date
*/
clone() {
clone(): Date {
return new Date(new globalThis.Date(this.year, this.month, this.date));
}

Expand All @@ -152,7 +163,7 @@ export default class Date {
* @returns {string|null} The name of the month in the given language,
* or null if the language is not provided
*/
toStrMonth(lang = null) {
toStrMonth(lang: string | null = null): string | null {
const l = fromAny(lang);
return (l && l.months[this.month]) ?? null;
}
Expand All @@ -163,7 +174,7 @@ export default class Date {
* @returns {string|null} The name of the day in the given language,
* or null if the language is not provided
*/
toStrDay(lang = null) {
toStrDay(lang: string | null = null): string | null {
const l = fromAny(lang);
return (l && l.days[this.day]) ?? null;
}
Expand All @@ -174,7 +185,7 @@ export default class Date {
* @returns {string|null} The first letter of the day in the given
* language, or null if the language is not provided
*/
toStrDayLetter(lang = null) {
toStrDayLetter(lang: string | null = null): string | null {
const l = fromAny(lang);
return (l && l.daysLetter[this.day]) ?? null;
}
Expand All @@ -183,15 +194,15 @@ export default class Date {
* Get a short representation of the date (dd.mm)
* @returns {string} A string representing the date in the form dd.mm
*/
toStrShort() {
toStrShort(): string {
return `${padZero(this.date)}.${padZero(this.month + 1)}`;
}

/**
* Get a representation of the date (dd.mm.yyyy)
* @returns {string} A string representing the date in the form dd.mm.yyyy
*/
toStr() {
toStr(): string {
const month = padZero(this.month + 1);
return `${padZero(this.date)}.${month}.${this.year}`;
}
Expand All @@ -200,7 +211,7 @@ export default class Date {
* Get a representation of the date suitable for a browser (yyyy-mm-dd)
* @returns {string} A string representing the date in the form yyyy-mm-dd
*/
toBrowserDate() {
toBrowserDate(): string {
const month = padZero(this.month + 1);
return `${this.year}-${month}-${padZero(this.date)}`;
}
Expand Down
Loading

0 comments on commit 1363576

Please sign in to comment.