From 13635766e2a72fc79224bbf58e051e8cb3914890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Meier?= Date: Mon, 18 Mar 2024 02:02:05 +0100 Subject: [PATCH] Publish v0.3.0-alpha.12 - move time to ts --- package.json | 2 +- src/time/Date.d.ts | 116 ------------ src/time/{Date.js => Date.ts} | 67 ++++--- src/time/DateTime.d.ts | 166 ------------------ src/time/{DateTime.js => DateTime.ts} | 66 +++---- src/time/DateTimeRange.d.ts | 39 ---- .../{DateTimeRange.js => DateTimeRange.ts} | 53 ++++-- src/time/Duration.d.ts | 39 ---- src/time/Duration.js | 94 ---------- src/time/Duration.ts | 115 ++++++++++++ src/time/{localization.js => localization.ts} | 31 +++- 11 files changed, 247 insertions(+), 541 deletions(-) delete mode 100644 src/time/Date.d.ts rename src/time/{Date.js => Date.ts} (82%) delete mode 100644 src/time/DateTime.d.ts rename src/time/{DateTime.js => DateTime.ts} (85%) delete mode 100644 src/time/DateTimeRange.d.ts rename src/time/{DateTimeRange.js => DateTimeRange.ts} (62%) delete mode 100644 src/time/Duration.d.ts delete mode 100644 src/time/Duration.js create mode 100644 src/time/Duration.ts rename src/time/{localization.js => localization.ts} (77%) diff --git a/package.json b/package.json index d05cd84..fef7c32 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fire-lib-js", - "version": "0.3.0-alpha.11", + "version": "0.3.0-alpha.12", "author": "Sören Meier ", "type": "module", "scripts": { diff --git a/src/time/Date.d.ts b/src/time/Date.d.ts deleted file mode 100644 index b95c7e1..0000000 --- a/src/time/Date.d.ts +++ /dev/null @@ -1,116 +0,0 @@ -export default class Date { - /** - * Tries to creates a new Date instance - */ - static parse(val: any): Date; - - /** - * Create a new Date instance - * @constructor - * @param {Date|string|number} 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?: Date | string | number); - - /** - * Check if the date represented by this instance is today - * @returns {boolean} True if the date represented by this instance is - * today, otherwise false - */ - isToday(): boolean; - - /** - * Get the year from the date ex: 2023 - * @returns {number} The year in which the date occurs - */ - get year(): number; - - /** - * Get the month from the date (0 indexed) - * @returns {number} The month in which the date occurs, zero-indexed - */ - get month(): number; - - /** - * Get the date (day of the month 1-31) - * @returns {number} The day of the month on which the date occurs - */ - get date(): number; - - /** - * Get the week of the year - * @returns {number} The week of the year in which the date occurs - */ - get week(): number; - - /** - * Get the day of the week (0 indexed) - * @returns {number} The day of the week on which the date occurs, - * zero-indexed with 0 for Sunday - */ - get day(): number; - - /** - * Get the day of the week (1 indexed with Monday as 1) - * @returns {number} The day of the week on which the date occurs, with 1 - * for Monday and 7 for Sunday - */ - get dayMoToSu(): number; - - /** - * 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(): number; - - /** - * Create a new Date object representing the same day - * @returns {Date} A new Date object with the same year, month, and - * date - */ - clone(): Date; - - /** - * Get the name of the month in a given language - * @param {string|null} lang - The language to get the month name in - * @returns {string|null} The name of the month in the given language, - * or null if the language is not provided - */ - toStrMonth(lang?: string | null): string | null; - - /** - * Get the name of the day in a given language - * @param {string|null} lang - The language to get the day name in - * @returns {string|null} The name of the day in the given language, - * or null if the language is not provided - */ - toStrDay(lang?: string | null): string | null; - - /** - * Get the first letter of the day in a given language, in uppercase - * @param {string|null} lang - The language to get the day name in - * @returns {string|null} The first letter of the day in the given - * language, or null if the language is not provided - */ - toStrDayLetter(lang?: string | null): string | null; - - /** - * Get a short representation of the date (dd.mm) - * @returns {string} A string representing the date in the form dd.mm - */ - toStrShort(): string; - - /** - * Get a representation of the date (dd.mm.yyyy) - * @returns {string} A string representing the date in the form dd.mm.yyyy - */ - toStr(): string; - - /** - * 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(): string; -} diff --git a/src/time/Date.js b/src/time/Date.ts similarity index 82% rename from src/time/Date.js rename to src/time/Date.ts index a1b5c0c..0e7fe84 100644 --- a/src/time/Date.js +++ b/src/time/Date.ts @@ -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'; } @@ -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' && @@ -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); @@ -56,6 +66,7 @@ export default class Date { this.raw = new globalThis.Date(date); + // @ts-ignore if (isNaN(this.raw)) { throw new Error('invalid Date'); } @@ -63,10 +74,10 @@ export default class 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 && @@ -79,7 +90,7 @@ 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(); } @@ -87,7 +98,7 @@ export default class Date { * 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(); } @@ -95,7 +106,7 @@ export default class Date { * 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(); } @@ -103,7 +114,7 @@ export default class Date { * 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)); @@ -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(); } @@ -125,7 +136,7 @@ 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; } @@ -133,7 +144,7 @@ export default class Date { * 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(); } @@ -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)); } @@ -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; } @@ -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; } @@ -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; } @@ -183,7 +194,7 @@ 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)}`; } @@ -191,7 +202,7 @@ export default class Date { * 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}`; } @@ -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)}`; } diff --git a/src/time/DateTime.d.ts b/src/time/DateTime.d.ts deleted file mode 100644 index 196b193..0000000 --- a/src/time/DateTime.d.ts +++ /dev/null @@ -1,166 +0,0 @@ -export default class DateTime { - raw: Date; - - /** - * Tries to creates a new DateTime instance - */ - static parse(val: any): DateTime; - - /** - * Create a new DateTime instance - * @constructor - * @param {Date|DateTime|string|number} date - The date value. Default is - * current date/time. - * 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?: Date | DateTime | string | number); - - /** - * Create a new DateTime instance representing today's date - * @static - * @returns {DateTime} A DateTime object representing today's date with the - * time set to 0 - */ - static today(): DateTime; - - /** - * Check if the date represented by this instance is today - * @returns {boolean} True if the date represented by this instance is - * today, otherwise false - */ - isToday(): boolean; - - /** - * Get the year from the date ex: 2023 - * @returns {number} The year in which the date occurs - */ - get year(): number; - - /** - * Get the month from the date (0 indexed) - * @returns {number} The month in which the date occurs, zero-indexed - */ - get month(): number; - - /** - * Get the date (day of the month 1-31) - * @returns {number} The day of the month on which the date occurs - */ - get date(): number; - - /** - * Get the week of the year - * @returns {number} The week of the year in which the date occurs - */ - get week(): number; - - /** - * Get the day of the week (0 indexed) - * @returns {number} The day of the week on which the date occurs, - * zero-indexed with 0 for Sunday - */ - get day(): number; - - /** - * Get the day of the week (1 indexed with Monday as 1) - * @returns {number} The day of the week on which the date occurs, with 1 - * for Monday and 7 for Sunday - */ - get dayMoToSu(): number; - - /** - * Get the hours part of the date - * @returns {number} The hour of the day on which the date occurs, from 0 - * to 23 - */ - get hours(): number; - - /** - * Get the minutes part of the date - * @returns {number} The minute of the hour on which the date occurs, from - * 0 to 59 - */ - get minutes(): number; - - /** - * Get the seconds part of the date - * @returns {number} The second of the minute on which the date occurs, - * from 0 to 59 - */ - get seconds(): number; - - /** - * Get the milliseconds part of the date - * @returns {number} The milliseconds of the second on which the date - * occurs, from 0 to 999 - */ - get millis(): number; - - /** - * 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(): number; - - /** - * Create a new DateTime object representing the same day - * @returns {DateTime} A new DateTime object with the same year, month, and - * date - */ - cloneDay(): DateTime; - - /** - * Get the name of the month in a given language - * @param {string|null} lang - The language to get the month name in - * @returns {string|null} The name of the month in the given language, - * or null if the language is not provided - */ - toStrMonth(lang?: string | null): string | null; - - /** - * Get the name of the day in a given language - * @param {string|null} lang - The language to get the day name in - * @returns {string|null} The name of the day in the given language, - * or null if the language is not provided - */ - toStrDay(lang?: string | null): string | null; - - /** - * Get the first letter of the day in a given language, in uppercase - * @param {string|null} lang - The language to get the day name in - * @returns {string|null} The first letter of the day in the given - * language, or null if the language is not provided - */ - toStrDayLetter(lang?: string | null): string | null; - - /** - * Get a short representation of the date (dd.mm) - * @returns {string} A string representing the date in the form dd.mm - */ - toStrShortDate(): string; - - /** - * Get a representation of the date (dd.mm.yyyy) - * @returns {string} A string representing the date in the form dd.mm.yyyy - */ - toStrDate(): string; - - /** - * 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(): string; - - /** - * Get a representation of the time with seconds (hh:mm:ss) - * @returns {string} A string representing the time in the form hh:mm:ss - */ - toStrFullTime(): string; - - /** - * Get a representation of the time without seconds (hh:mm) - * @returns {string} A string representing the time in the form hh:mm - */ - toStrShortTime(): string; -} diff --git a/src/time/DateTime.js b/src/time/DateTime.ts similarity index 85% rename from src/time/DateTime.js rename to src/time/DateTime.ts index 2bd93ad..be75057 100644 --- a/src/time/DateTime.js +++ b/src/time/DateTime.ts @@ -1,13 +1,9 @@ -/** - * @module time/datetime - */ - -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 isDateTimeObject(val) { +function isDateTimeObject(val: any): val is DateTime { return typeof (val ? val.__isDateTimeObject__ : null) === 'function'; } @@ -17,9 +13,12 @@ function isDateTimeObject(val) { * @exports time/datetime/DateTime * */ export default class DateTime { + raw: Date; + static __parsetype__() {} __isDateTimeObject__() {} - static parse(val) { + + static parse(val: any): DateTime { if ( typeof val !== 'string' && typeof val !== 'number' && @@ -33,12 +32,12 @@ export default class DateTime { /** * Create a new DateTime instance * @constructor - * @param {Date|DateTime|string|number} date - The date value. Default is + * @param date - The date value. Default is * current date/time. * If it's a string or number, convert it to a date. - * @throws {Error} Will throw an error if date is an invalid Date. + * @throws Will throw an error if date is an invalid Date. */ - constructor(date = null) { + constructor(date: Date | DateTime | string | number | null = null) { if (typeof date === 'undefined' || date === null) { this.raw = new Date(); return; @@ -53,6 +52,7 @@ export default class DateTime { this.raw = new Date(date); + // @ts-ignore if (isNaN(this.raw)) throw new Error('invalid Date'); } @@ -62,7 +62,7 @@ export default class DateTime { * @returns {DateTime} A DateTime object representing today's date with the * time set to 0 */ - static today() { + static today(): DateTime { const date = new DateTime(); date.raw.setHours(0, 0, 0, 0); return date; @@ -73,7 +73,7 @@ export default class DateTime { * @returns {boolean} True if the date represented by this instance is * today, otherwise false */ - isToday() { + isToday(): boolean { const today = new DateTime(); return ( this.year == today.year && @@ -86,7 +86,7 @@ export default class DateTime { * 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(); } @@ -94,7 +94,7 @@ export default class DateTime { * 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(); } @@ -102,7 +102,7 @@ export default class DateTime { * 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(); } @@ -110,7 +110,7 @@ export default class DateTime { * 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.cloneDay(); nearestThursday.raw.setDate(this.date + 4 - this.dayMoToSu); const firstDay = new DateTime(new Date(this.year, 0, 1)); @@ -123,7 +123,7 @@ export default class DateTime { * @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(); } @@ -132,7 +132,7 @@ export default class DateTime { * @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; } @@ -141,7 +141,7 @@ export default class DateTime { * @returns {number} The hour of the day on which the date occurs, from 0 * to 23 */ - get hours() { + get hours(): number { return this.raw.getHours(); } @@ -150,7 +150,7 @@ export default class DateTime { * @returns {number} The minute of the hour on which the date occurs, from * 0 to 59 */ - get minutes() { + get minutes(): number { return this.raw.getMinutes(); } @@ -159,7 +159,7 @@ export default class DateTime { * @returns {number} The second of the minute on which the date occurs, * from 0 to 59 */ - get seconds() { + get seconds(): number { return this.raw.getSeconds(); } @@ -168,7 +168,7 @@ export default class DateTime { * @returns {number} The milliseconds of the second on which the date * occurs, from 0 to 999 */ - get millis() { + get millis(): number { return this.raw.getMilliseconds(); } @@ -176,7 +176,7 @@ export default class DateTime { * 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(); } @@ -185,7 +185,7 @@ export default class DateTime { * @returns {DateTime} A new DateTime object with the same year, month, and * date */ - cloneDay() { + cloneDay(): DateTime { return new DateTime(new Date(this.year, this.month, this.date)); } @@ -195,7 +195,7 @@ export default class DateTime { * @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; } @@ -206,7 +206,7 @@ export default class DateTime { * @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; } @@ -217,7 +217,7 @@ export default class DateTime { * @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; } @@ -226,7 +226,7 @@ export default class DateTime { * Get a short representation of the date (dd.mm) * @returns {string} A string representing the date in the form dd.mm */ - toStrShortDate() { + toStrShortDate(): string { return `${padZero(this.date)}.${padZero(this.month + 1)}`; } @@ -234,7 +234,7 @@ export default class DateTime { * Get a representation of the date (dd.mm.yyyy) * @returns {string} A string representing the date in the form dd.mm.yyyy */ - toStrDate() { + toStrDate(): string { const month = padZero(this.month + 1); return `${padZero(this.date)}.${month}.${this.year}`; } @@ -243,7 +243,7 @@ export default class DateTime { * 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)}`; } @@ -252,7 +252,7 @@ export default class DateTime { * Get a representation of the time with seconds (hh:mm:ss) * @returns {string} A string representing the time in the form hh:mm:ss */ - toStrFullTime() { + toStrFullTime(): string { const minutes = padZero(this.minutes); const seconds = padZero(this.seconds); return `${padZero(this.hours)}:${minutes}:${seconds}`; @@ -262,7 +262,7 @@ export default class DateTime { * Get a representation of the time without seconds (hh:mm) * @returns {string} A string representing the time in the form hh:mm */ - toStrShortTime() { + toStrShortTime(): string { return `${padZero(this.hours)}:${padZero(this.minutes)}`; } diff --git a/src/time/DateTimeRange.d.ts b/src/time/DateTimeRange.d.ts deleted file mode 100644 index 96e332f..0000000 --- a/src/time/DateTimeRange.d.ts +++ /dev/null @@ -1,39 +0,0 @@ -import DateTime from './DateTime'; - -export default class DateTimeRange { - from: DateTime; - to: DateTime; - - /** - * Create a new DateTimeRange instance - * @constructor - * @param {DateTime} from - The start date value - * @param {DateTime} to - The end date value - */ - constructor(from: DateTime, to: DateTime); - - /** - * returns - * 10.10.2020 - 10.12.2023 - * 10.10 - 10.12.2020 - * 11 - 10.10.2020 - * 10.10.2020 - */ - toStrDate(): string; - - /** - * returns - * 10:30 - 14:40 - * 10:30 - */ - toStrShortTime(): string; - - /** - * returns - * 03.07.2023 18:00 - 04.07.2024 18:00 - * 04.08 18:00 - 03.07.2023 18:00 - * 04.07 18:00 - 05.07.2023 18:00 - * 18:00 - 19:35 05.07.2023 - */ - toStrDateShortTime(): string; -} diff --git a/src/time/DateTimeRange.js b/src/time/DateTimeRange.ts similarity index 62% rename from src/time/DateTimeRange.js rename to src/time/DateTimeRange.ts index 120e367..9ec0454 100644 --- a/src/time/DateTimeRange.js +++ b/src/time/DateTimeRange.ts @@ -1,20 +1,29 @@ -import { padZero } from '../utils/utils.js'; +import { padZero } from '../utils/utils'; +import type DateTime from './DateTime'; export default class DateTimeRange { - /// expects - /// from: DateTime - /// to: DateTime - constructor(from, to) { + from: DateTime; + to: DateTime; + + /** + * Create a new DateTimeRange instance + * @constructor + * @param {DateTime} from - The start date value + * @param {DateTime} to - The end date value + */ + constructor(from: DateTime, to: DateTime) { this.from = from; this.to = to; } - // returns - // 10.10.2020 - 10.12.2023 - // 10.10 - 10.12.2020 - // 11 - 10.10.2020 - // 10.10.2020 - toStrDate() { + /** + * returns + * 10.10.2020 - 10.12.2023 + * 10.10 - 10.12.2020 + * 11 - 10.10.2020 + * 10.10.2020 + */ + toStrDate(): string { const f = this.from; const t = this.to; @@ -33,9 +42,12 @@ export default class DateTimeRange { return t.toStrDate(); } - // returns 10:30 - 14:40 - // returns 10:30 - toStrShortTime() { + /** + * returns + * 10:30 - 14:40 + * 10:30 + */ + toStrShortTime(): string { const f = this.from.toStrShortTime(); const t = this.to.toStrShortTime(); @@ -43,11 +55,14 @@ export default class DateTimeRange { return f; } - // returns 03.07.2023 18:00 - 04.07.2024 18:00 - // returns 04.08 18:00 - 03.07.2023 18:00 - // returns 04.07 18:00 - 05.07.2023 18:00 - // returns 18:00 - 19:35 05.07.2023 - toStrDateShortTime() { + /** + * returns + * 03.07.2023 18:00 - 04.07.2024 18:00 + * 04.08 18:00 - 03.07.2023 18:00 + * 04.07 18:00 - 05.07.2023 18:00 + * 18:00 - 19:35 05.07.2023 + */ + toStrDateShortTime(): string { const f = this.from; const t = this.to; diff --git a/src/time/Duration.d.ts b/src/time/Duration.d.ts deleted file mode 100644 index 2ecdfd0..0000000 --- a/src/time/Duration.d.ts +++ /dev/null @@ -1,39 +0,0 @@ -import DateTime from './DateTime'; - -export default class Duration { - /** - * Create a new Duration instance - * - * @param {number} duration - The duration in milliseconds - */ - constructor(duration: number); - - /** - * Calculate the duration between two dates - * - * @param {DateTime} from - The start date - * @param {DateTime} to - The end date - */ - static from(from: DateTime, to: DateTime): Duration; - - /** - * Calculate the duration from now to a date - * - * @param {DateTime} to - The end date - */ - static toNow(to: DateTime): Duration; - - get seconds(): number; - - get minutes(): number; - - get hours(): number; - - get days(): number; - - get weeks(): number; - - toStrByDays(lang?: string | null): string; - - toStr(lang?: string | null): string; -} diff --git a/src/time/Duration.js b/src/time/Duration.js deleted file mode 100644 index 0bde4d6..0000000 --- a/src/time/Duration.js +++ /dev/null @@ -1,94 +0,0 @@ -import { fromAny } from './localization.js'; - -export default class Duration { - constructor(millis) { - // + is in the future - is in the past - this.millis = millis; - } - - // from and to need to be a DateTime object - static from(from, to) { - return new Duration(to.time - from.time); - } - - // to needs to be a DateTime object - static toNow(to) { - return new Duration(to.time - Date.now()); - } - - get seconds() { - return this.millis / 1000; - } - - get minutes() { - return this.seconds / 60; - } - - get hours() { - return this.minutes / 60; - } - - get days() { - return this.hours / 24; - } - - get weeks() { - return this.days / 7; - } - - /// returns null if lang is undefined or days < 1 - toStrByDays(lang = null) { - // month is maybe not always accurate - let days = this.days; - let l = fromAny(lang); - if (l === null) return null; - l = l.intWords; - - let pre = l.afterGram; - if (days < 0) { - pre = l.beforeGram; - days *= -1; - } - - if (days < 1) return null; - if (days < 7) return intInfo(pre, days, l.day); - if (days < 30) return intInfo(pre, days / 7, l.week); - if (days < 30 * 12) return intInfo(pre, days / 30, l.month); - return intInfo(pre, days / (30 * 12), l.year); - } - - /// lang can be a string or a lang item (see localization.js) - /// if null == uses default set in localization - toStr(lang = null) { - let l = fromAny(lang); - if (l === null) return null; - - if (Math.abs(this.days) >= 1) return this.toStrByDays(l); - - l = l.intWords; - - let secs = this.seconds; - let pre = l.afterGram; - if (secs < 0) { - pre = l.beforeGram; - secs *= -1; - } - - if (secs < 60) return intInfo(pre, secs, l.second); - if (secs < 60 * 60) return intInfo(pre, secs / 60, l.minute); - - // because days > 1 get filtered out before - // this must be in hours - return intInfo(pre, secs / (60 * 60), l.hour); - } -} - -// int = intelligent -function intInfo(gram, num, [oneUnit, one, many]) { - num = Math.round(num); - if (num <= 1) { - num = oneUnit; - many = one; - } - return gram(num, many); -} diff --git a/src/time/Duration.ts b/src/time/Duration.ts new file mode 100644 index 0000000..b7dac43 --- /dev/null +++ b/src/time/Duration.ts @@ -0,0 +1,115 @@ +import type DateTime from './DateTime'; +import { type Localization, fromAny } from './localization'; + +export default class Duration { + millis: number; + + /** + * Create a new Duration instance + * + * @param duration - The duration in milliseconds + */ + constructor(millis: number) { + // + is in the future - is in the past + this.millis = millis; + } + + /** + * Calculate the duration between two dates + * + * @param {DateTime} from - The start date + * @param {DateTime} to - The end date + */ + static from(from: DateTime, to: DateTime): Duration { + return new Duration(to.time - from.time); + } + + /** + * Calculate the duration from now to a date + * + * @param {DateTime} to - The end date + */ + static toNow(to: DateTime): Duration { + return new Duration(to.time - Date.now()); + } + + get seconds(): number { + return this.millis / 1000; + } + + get minutes(): number { + return this.seconds / 60; + } + + get hours(): number { + return this.minutes / 60; + } + + get days(): number { + return this.hours / 24; + } + + get weeks(): number { + return this.days / 7; + } + + /// returns null if lang is undefined or days < 1 + toStrByDays(lang: string | Localization | null = null): string | null { + // month is maybe not always accurate + let days = this.days; + let l = fromAny(lang); + if (l === null) return null; + const words = l.intWords; + + let pre = words.afterGram; + if (days < 0) { + pre = words.beforeGram; + days *= -1; + } + + if (days < 1) return null; + if (days < 7) return intInfo(pre, days, words.day); + if (days < 30) return intInfo(pre, days / 7, words.week); + if (days < 30 * 12) return intInfo(pre, days / 30, words.month); + return intInfo(pre, days / (30 * 12), words.year); + } + + /// lang can be a string or a lang item (see localization.js) + /// if null == uses default set in localization + toStr(lang: string | null = null): string | null { + let l = fromAny(lang); + if (l === null) return null; + const words = l.intWords; + + if (Math.abs(this.days) >= 1) return this.toStrByDays(l); + + let secs = this.seconds; + let pre = words.afterGram; + if (secs < 0) { + pre = words.beforeGram; + secs *= -1; + } + + if (secs < 60) return intInfo(pre, secs, words.second); + if (secs < 60 * 60) return intInfo(pre, secs / 60, words.minute); + + // because days > 1 get filtered out before + // this must be in hours + return intInfo(pre, secs / (60 * 60), words.hour); + } +} + +// int = intelligent +function intInfo( + gram: (num: string | number, unit: string) => string, + num: number, + [oneUnit, one, many]: [string, string, string], +): string { + num = Math.round(num); + let num2: number | string = num; + if (num <= 1) { + num2 = oneUnit; + many = one; + } + return gram(num2, many); +} diff --git a/src/time/localization.js b/src/time/localization.ts similarity index 77% rename from src/time/localization.js rename to src/time/localization.ts index 9e8619d..f8dddd9 100644 --- a/src/time/localization.js +++ b/src/time/localization.ts @@ -1,11 +1,30 @@ export let DEFAULT_LANG = 'german'; -export function setDefault(lang) { +export function setDefault(lang: string) { DEFAULT_LANG = lang; } +export type gramFn = (num: number | string, unit: string) => string; + +export type Localization = { + months: string[]; + days: string[]; + daysLetter: string[]; + intWords: { + beforeGram: gramFn; + afterGram: gramFn; + second: [string, string, string]; + minute: [string, string, string]; + hour: [string, string, string]; + day: [string, string, string]; + week: [string, string, string]; + month: [string, string, string]; + year: [string, string, string]; + }; +}; + // s cannot be null -export function fromString(s) { +export function fromString(s: string): Localization | null { switch (s.toLowerCase()) { case 'german': case 'de': @@ -21,14 +40,14 @@ export function fromString(s) { } } -export function fromAny(a) { +export function fromAny(a: any): Localization | null { if (typeof a === 'string') return fromString(a); if (a === null) return fromString(DEFAULT_LANG); // we expect it to already be a lang item (for example german) return a; } -export const german = { +export const german: Localization = { months: [ 'Januar', 'Februar', @@ -67,7 +86,7 @@ export const german = { }, }; -export const english = { +export const english: Localization = { months: [ 'January', 'February', @@ -106,7 +125,7 @@ export const english = { }, }; -export const french = { +export const french: Localization = { months: [ 'Janvier', 'Février',