From 640038144e2bd4c8fb3a58da99cfed73812127ef Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Wed, 4 Dec 2024 17:17:48 +0530 Subject: [PATCH 1/4] Convert date package to TypeScript --- packages/date/README.md | 34 +-- packages/date/src/{index.js => index.ts} | 256 ++++++++++------------- packages/date/src/types.ts | 129 ++++++++++++ 3 files changed, 260 insertions(+), 159 deletions(-) rename packages/date/src/{index.js => index.ts} (61%) create mode 100644 packages/date/src/types.ts diff --git a/packages/date/README.md b/packages/date/README.md index 4f0a64c24aa71..56eed73cb3f8d 100644 --- a/packages/date/README.md +++ b/packages/date/README.md @@ -28,8 +28,8 @@ _Related_ _Parameters_ - _dateFormat_ `string`: PHP-style formatting string. See php.net/date. -- _dateValue_ `Moment | Date | string | undefined`: Date object or string, parsable by moment.js. -- _timezone_ `string | number | undefined`: Timezone to output result in or a UTC offset. Defaults to timezone from site. +- _dateValue_ `Moment | Date | string`: Date object or string, parsable by moment.js. +- _timezone_ `string`: Timezone to output result in or a UTC offset. Defaults to timezone from site. _Returns_ @@ -49,12 +49,12 @@ _Related_ _Parameters_ - _dateFormat_ `string`: PHP-style formatting string. See php.net/date. -- _dateValue_ `Moment | Date | string | undefined`: Date object or string, parsable by moment.js. -- _timezone_ `string | number | boolean | undefined=`: Timezone to output result in or a UTC offset. Defaults to timezone from site. Notice: `boolean` is effectively deprecated, but still supported for backward compatibility reasons. +- _dateValue_ `Moment | Date | string`: Date object or string, parsable by moment.js. +- _timezone_ `string | number | boolean`: Timezone to output result in or a UTC offset. Defaults to timezone from site. Notice: `boolean` is effectively deprecated, but still supported for backward compatibility reasons. _Returns_ -- `string`: Formatted date. +- Formatted date. ### format @@ -63,11 +63,11 @@ Formats a date. Does not alter the date's timezone. _Parameters_ - _dateFormat_ `string`: PHP-style formatting string. See php.net/date. -- _dateValue_ `Moment | Date | string | undefined`: Date object or string, parsable by moment.js. +- _dateValue_ `Moment | Date | string`: Date object or string, parsable by moment.js. _Returns_ -- `string`: Formatted date. +- Formatted date. ### getDate @@ -75,11 +75,11 @@ Create and return a JavaScript Date Object from a date string in the WP timezone _Parameters_ -- _dateString_ `?string`: Date formatted in the WP timezone. +- _dateString_ `string | null`: Date formatted in the WP timezone. _Returns_ -- `Date`: Date +- Date ### getSettings @@ -96,11 +96,11 @@ Formats a date (like `date()` in PHP), in the UTC timezone. _Parameters_ - _dateFormat_ `string`: PHP-style formatting string. See php.net/date. -- _dateValue_ `Moment | Date | string | undefined`: Date object or string, parsable by moment.js. +- _dateValue_ `Moment | Date | string`: Date object or string, parsable by moment.js. _Returns_ -- `string`: Formatted date in English. +- Formatted date in English. ### gmdateI18n @@ -109,11 +109,11 @@ Formats a date (like `wp_date()` in PHP), translating it into site's locale and _Parameters_ - _dateFormat_ `string`: PHP-style formatting string. See php.net/date. -- _dateValue_ `Moment | Date | string | undefined`: Date object or string, parsable by moment.js. +- _dateValue_ `Moment | Date | string`: Date object or string, parsable by moment.js. _Returns_ -- `string`: Formatted date. +- Formatted date. ### humanTimeDiff @@ -121,12 +121,12 @@ Returns a human-readable time difference between two dates, like human_time_diff _Parameters_ -- _from_ `Moment | Date | string`: From date, in the WP timezone. -- _to_ `Moment | Date | string | undefined`: To date, formatted in the WP timezone. +- _from_ `Moment | Date | string | number`: From date, in the WP timezone. +- _to_ `Moment | Date | string | number`: To date, formatted in the WP timezone. _Returns_ -- `string`: Human-readable time difference. +- Human-readable time difference. ### isInTheFuture @@ -138,7 +138,7 @@ _Parameters_ _Returns_ -- `boolean`: Is in the future. +- Is in the future. ### setSettings diff --git a/packages/date/src/index.js b/packages/date/src/index.ts similarity index 61% rename from packages/date/src/index.js rename to packages/date/src/index.ts index 8d8f53fd8bc10..42fe71ae65460 100644 --- a/packages/date/src/index.js +++ b/packages/date/src/index.ts @@ -1,6 +1,7 @@ /** * External dependencies */ +import type { Moment } from 'moment'; import momentLib from 'moment'; import 'moment-timezone/moment-timezone'; import 'moment-timezone/moment-timezone-utils'; @@ -9,54 +10,10 @@ import 'moment-timezone/moment-timezone-utils'; * WordPress dependencies */ import deprecated from '@wordpress/deprecated'; - -/** @typedef {import('moment').Moment} Moment */ -/** @typedef {import('moment').LocaleSpecification} MomentLocaleSpecification */ - -/** - * @typedef MeridiemConfig - * @property {string} am Lowercase AM. - * @property {string} AM Uppercase AM. - * @property {string} pm Lowercase PM. - * @property {string} PM Uppercase PM. - */ - -/** - * @typedef FormatsConfig - * @property {string} time Time format. - * @property {string} date Date format. - * @property {string} datetime Datetime format. - * @property {string} datetimeAbbreviated Abbreviated datetime format. - */ - -/** - * @typedef TimezoneConfig - * @property {string} offset Offset setting. - * @property {string} offsetFormatted Offset setting with decimals formatted to minutes. - * @property {string} string The timezone as a string (e.g., `'America/Los_Angeles'`). - * @property {string} abbr Abbreviation for the timezone. - */ - -/* eslint-disable jsdoc/valid-types */ -/** - * @typedef L10nSettings - * @property {string} locale Moment locale. - * @property {MomentLocaleSpecification['months']} months Locale months. - * @property {MomentLocaleSpecification['monthsShort']} monthsShort Locale months short. - * @property {MomentLocaleSpecification['weekdays']} weekdays Locale weekdays. - * @property {MomentLocaleSpecification['weekdaysShort']} weekdaysShort Locale weekdays short. - * @property {MeridiemConfig} meridiem Meridiem config. - * @property {MomentLocaleSpecification['relativeTime']} relative Relative time config. - * @property {0|1|2|3|4|5|6} startOfWeek Day that the week starts on. - */ -/* eslint-enable jsdoc/valid-types */ - /** - * @typedef DateSettings - * @property {L10nSettings} l10n Localization settings. - * @property {FormatsConfig} formats Date/time formats config. - * @property {TimezoneConfig} timezone Timezone settings. + * Internal dependencies */ +import type { DateSettings } from './types'; const WP_ZONE = 'WP'; @@ -66,8 +23,7 @@ const VALID_UTC_OFFSET = /^[+-][0-1][0-9](:?[0-9][0-9])?$/; // Changes made here will likely need to be synced with Core in the file // src/wp-includes/script-loader.php in `wp_default_packages_inline_scripts()`. -/** @type {DateSettings} */ -let settings = { +let settings: DateSettings = { l10n: { locale: 'en', months: [ @@ -139,9 +95,9 @@ let settings = { /** * Adds a locale to moment, using the format supplied by `wp_localize_script()`. * - * @param {DateSettings} dateSettings Settings, including locale data. + * @param dateSettings Settings, including locale data. */ -export function setSettings( dateSettings ) { +export function setSettings( dateSettings: DateSettings ) { settings = dateSettings; setupWPTimezone(); @@ -259,31 +215,25 @@ function setupWPTimezone() { // Date constants. /** * Number of seconds in one minute. - * - * @type {number} */ const MINUTE_IN_SECONDS = 60; /** * Number of minutes in one hour. - * - * @type {number} */ const HOUR_IN_MINUTES = 60; /** * Number of seconds in one hour. - * - * @type {number} */ const HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS; /** * Map of PHP formats to Moment.js formats. * - * These are used internally by {@link wp.date.format}, and are either + * These are used internally by {@link format}, and are either * a string representing the corresponding Moment.js format code, or a * function which returns the formatted string. * - * This should only be used through {@link wp.date.format}, not + * This should only be used through {@link format}, not * directly. */ const formatMap = { @@ -297,11 +247,11 @@ const formatMap = { /** * Gets the ordinal suffix. * - * @param {Moment} momentDate Moment instance. + * @param momentDate Moment instance. * - * @return {string} Formatted date. + * @return Formatted date. */ - S( momentDate ) { + S( momentDate: Moment ) { // Do - D. const num = momentDate.format( 'D' ); const withOrdinal = momentDate.format( 'Do' ); @@ -312,11 +262,11 @@ const formatMap = { /** * Gets the day of the year (zero-indexed). * - * @param {Moment} momentDate Moment instance. + * @param momentDate Moment instance. * - * @return {string} Formatted date. + * @return Formatted date. */ - z( momentDate ) { + z( momentDate: Moment ) { // DDD - 1. return ( parseInt( momentDate.format( 'DDD' ), 10 ) - 1 ).toString(); }, @@ -332,11 +282,11 @@ const formatMap = { /** * Gets the days in the month. * - * @param {Moment} momentDate Moment instance. + * @param momentDate Moment instance. * - * @return {number} Formatted date. + * @return Formatted date. */ - t( momentDate ) { + t( momentDate: Moment ) { return momentDate.daysInMonth(); }, @@ -344,11 +294,11 @@ const formatMap = { /** * Gets whether the current year is a leap year. * - * @param {Moment} momentDate Moment instance. + * @param momentDate Moment instance. * - * @return {string} Formatted date. + * @return Formatted date. */ - L( momentDate ) { + L( momentDate: Moment ) { return momentDate.isLeapYear() ? '1' : '0'; }, o: 'GGGG', @@ -361,11 +311,11 @@ const formatMap = { /** * Gets the current time in Swatch Internet Time (.beats). * - * @param {Moment} momentDate Moment instance. + * @param momentDate Moment instance. * - * @return {number} Formatted date. + * @return Formatted date. */ - B( momentDate ) { + B( momentDate: Moment ) { const timezoned = momentLib( momentDate ).utcOffset( 60 ); const seconds = parseInt( timezoned.format( 's' ), 10 ), minutes = parseInt( timezoned.format( 'm' ), 10 ), @@ -393,11 +343,11 @@ const formatMap = { /** * Gets whether the timezone is in DST currently. * - * @param {Moment} momentDate Moment instance. + * @param momentDate Moment instance. * - * @return {string} Formatted date. + * @return Formatted date. */ - I( momentDate ) { + I( momentDate: Moment ) { return momentDate.isDST() ? '1' : '0'; }, O: 'ZZ', @@ -406,11 +356,11 @@ const formatMap = { /** * Gets the timezone offset in seconds. * - * @param {Moment} momentDate Moment instance. + * @param momentDate Moment instance. * - * @return {number} Formatted date. + * @return Formatted date. */ - Z( momentDate ) { + Z( momentDate: Moment ) { // Timezone offset in seconds. const offset = momentDate.format( 'Z' ); const sign = offset[ 0 ] === '-' ? -1 : 1; @@ -429,11 +379,11 @@ const formatMap = { /** * Formats the date as RFC2822. * - * @param {Moment} momentDate Moment instance. + * @param momentDate Moment instance. * - * @return {string} Formatted date. + * @return Formatted date. */ - r( momentDate ) { + r( momentDate: Moment ) { return momentDate .locale( 'en' ) .format( 'ddd, DD MMM YYYY HH:mm:ss ZZ' ); @@ -444,14 +394,17 @@ const formatMap = { /** * Formats a date. Does not alter the date's timezone. * - * @param {string} dateFormat PHP-style formatting string. - * See php.net/date. - * @param {Moment | Date | string | undefined} dateValue Date object or string, - * parsable by moment.js. + * @param dateFormat PHP-style formatting string. + * See php.net/date. + * @param dateValue Date object or string, + * parsable by moment.js. * - * @return {string} Formatted date. + * @return Formatted date. */ -export function format( dateFormat, dateValue = new Date() ) { +export function format( + dateFormat: string, + dateValue: Moment | Date | string = new Date() +) { let i, char; const newFormat = []; const momentDate = momentLib( dateValue ); @@ -465,8 +418,7 @@ export function format( dateFormat, dateValue = new Date() ) { continue; } if ( char in formatMap ) { - const formatter = - formatMap[ /** @type {keyof formatMap} */ ( char ) ]; + const formatter = formatMap[ char as keyof typeof formatMap ]; if ( typeof formatter !== 'string' ) { // If the format is a function, call it. newFormat.push( '[' + formatter( momentDate ) + ']' ); @@ -486,20 +438,24 @@ export function format( dateFormat, dateValue = new Date() ) { /** * Formats a date (like `date()` in PHP). * - * @param {string} dateFormat PHP-style formatting string. - * See php.net/date. - * @param {Moment | Date | string | undefined} dateValue Date object or string, parsable - * by moment.js. - * @param {string | number | undefined} timezone Timezone to output result in or a - * UTC offset. Defaults to timezone from - * site. + * @param dateFormat PHP-style formatting string. + * See php.net/date. + * @param dateValue Date object or string, parsable + * by moment.js. + * @param timezone Timezone to output result in or a + * UTC offset. Defaults to timezone from + * site. * * @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones * @see https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC * * @return {string} Formatted date in English. */ -export function date( dateFormat, dateValue = new Date(), timezone ) { +export function date( + dateFormat: string, + dateValue: Moment | Date | string = new Date(), + timezone?: string +) { const dateMoment = buildMoment( dateValue, timezone ); return format( dateFormat, dateMoment ); } @@ -507,14 +463,17 @@ export function date( dateFormat, dateValue = new Date(), timezone ) { /** * Formats a date (like `date()` in PHP), in the UTC timezone. * - * @param {string} dateFormat PHP-style formatting string. - * See php.net/date. - * @param {Moment | Date | string | undefined} dateValue Date object or string, - * parsable by moment.js. + * @param dateFormat PHP-style formatting string. + * See php.net/date. + * @param dateValue Date object or string, + * parsable by moment.js. * - * @return {string} Formatted date in English. + * @return Formatted date in English. */ -export function gmdate( dateFormat, dateValue = new Date() ) { +export function gmdate( + dateFormat: string, + dateValue: Moment | Date | string = new Date() +) { const dateMoment = momentLib( dateValue ).utc(); return format( dateFormat, dateMoment ); } @@ -525,22 +484,26 @@ export function gmdate( dateFormat, dateValue = new Date() ) { * Backward Compatibility Notice: if `timezone` is set to `true`, the function * behaves like `gmdateI18n`. * - * @param {string} dateFormat PHP-style formatting string. - * See php.net/date. - * @param {Moment | Date | string | undefined} dateValue Date object or string, parsable by - * moment.js. - * @param {string | number | boolean | undefined=} timezone Timezone to output result in or a - * UTC offset. Defaults to timezone from - * site. Notice: `boolean` is effectively - * deprecated, but still supported for - * backward compatibility reasons. + * @param dateFormat PHP-style formatting string. + * See php.net/date. + * @param dateValue Date object or string, parsable by + * moment.js. + * @param timezone Timezone to output result in or a + * UTC offset. Defaults to timezone from + * site. Notice: `boolean` is effectively + * deprecated, but still supported for + * backward compatibility reasons. * * @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones * @see https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC * - * @return {string} Formatted date. + * @return Formatted date. */ -export function dateI18n( dateFormat, dateValue = new Date(), timezone ) { +export function dateI18n( + dateFormat: string, + dateValue: Moment | Date | string = new Date(), + timezone?: string | number | boolean +) { if ( true === timezone ) { return gmdateI18n( dateFormat, dateValue ); } @@ -558,14 +521,17 @@ export function dateI18n( dateFormat, dateValue = new Date(), timezone ) { * Formats a date (like `wp_date()` in PHP), translating it into site's locale * and using the UTC timezone. * - * @param {string} dateFormat PHP-style formatting string. - * See php.net/date. - * @param {Moment | Date | string | undefined} dateValue Date object or string, - * parsable by moment.js. + * @param dateFormat PHP-style formatting string. + * See php.net/date. + * @param dateValue Date object or string, + * parsable by moment.js. * - * @return {string} Formatted date. + * @return Formatted date. */ -export function gmdateI18n( dateFormat, dateValue = new Date() ) { +export function gmdateI18n( + dateFormat: string, + dateValue: Moment | Date | string = new Date() +) { const dateMoment = momentLib( dateValue ).utc(); dateMoment.locale( settings.l10n.locale ); return format( dateFormat, dateMoment ); @@ -574,11 +540,11 @@ export function gmdateI18n( dateFormat, dateValue = new Date() ) { /** * Check whether a date is considered in the future according to the WordPress settings. * - * @param {string} dateValue Date String or Date object in the Defined WP Timezone. + * @param dateValue Date String or Date object in the Defined WP Timezone. * - * @return {boolean} Is in the future. + * @return Is in the future. */ -export function isInTheFuture( dateValue ) { +export function isInTheFuture( dateValue: string ) { const now = momentLib.tz( WP_ZONE ); const momentObject = momentLib.tz( dateValue, WP_ZONE ); @@ -588,11 +554,11 @@ export function isInTheFuture( dateValue ) { /** * Create and return a JavaScript Date Object from a date string in the WP timezone. * - * @param {?string} dateString Date formatted in the WP timezone. + * @param dateString Date formatted in the WP timezone. * - * @return {Date} Date + * @return Date */ -export function getDate( dateString ) { +export function getDate( dateString?: string | null ) { if ( ! dateString ) { return momentLib.tz( WP_ZONE ).toDate(); } @@ -603,12 +569,15 @@ export function getDate( dateString ) { /** * Returns a human-readable time difference between two dates, like human_time_diff() in PHP. * - * @param {Moment | Date | string} from From date, in the WP timezone. - * @param {Moment | Date | string | undefined} to To date, formatted in the WP timezone. + * @param from From date, in the WP timezone. + * @param to To date, formatted in the WP timezone. * - * @return {string} Human-readable time difference. + * @return Human-readable time difference. */ -export function humanTimeDiff( from, to ) { +export function humanTimeDiff( + from: Moment | Date | string | number, + to?: Moment | Date | string | number +) { const fromMoment = momentLib.tz( from, WP_ZONE ); const toMoment = to ? momentLib.tz( to, WP_ZONE ) : momentLib.tz( WP_ZONE ); return fromMoment.from( toMoment ); @@ -617,23 +586,26 @@ export function humanTimeDiff( from, to ) { /** * Creates a moment instance using the given timezone or, if none is provided, using global settings. * - * @param {Moment | Date | string | undefined} dateValue Date object or string, parsable - * by moment.js. - * @param {string | number | undefined} timezone Timezone to output result in or a - * UTC offset. Defaults to timezone from - * site. + * @param dateValue Date object or string, parsable + * by moment.js. + * @param timezone Timezone to output result in or a + * UTC offset. Defaults to timezone from + * site. * * @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones * @see https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC * - * @return {Moment} a moment instance. + * @return A moment instance. */ -function buildMoment( dateValue, timezone = '' ) { +function buildMoment( + dateValue?: Moment | Date | string, + timezone: string | number = '' +) { const dateMoment = momentLib( dateValue ); if ( timezone && ! isUTCOffset( timezone ) ) { // The ! isUTCOffset() check guarantees that timezone is a string. - return dateMoment.tz( /** @type {string} */ ( timezone ) ); + return dateMoment.tz( timezone as string ); } if ( timezone && isUTCOffset( timezone ) ) { @@ -650,11 +622,11 @@ function buildMoment( dateValue, timezone = '' ) { /** * Returns whether a certain UTC offset is valid or not. * - * @param {number|string} offset a UTC offset. + * @param offset a UTC offset. * - * @return {boolean} whether a certain UTC offset is valid or not. + * @return whether a certain UTC offset is valid or not. */ -function isUTCOffset( offset ) { +function isUTCOffset( offset: number | string ) { if ( 'number' === typeof offset ) { return true; } diff --git a/packages/date/src/types.ts b/packages/date/src/types.ts new file mode 100644 index 0000000000000..fde6dc49c0980 --- /dev/null +++ b/packages/date/src/types.ts @@ -0,0 +1,129 @@ +/** + * External dependencies + */ +import type { LocaleSpecification as MomentLocaleSpecification } from 'moment'; + +export type MeridiemConfig = { + /** + * Lowercase AM. + */ + am: string; + + /** + * Uppercase AM. + */ + AM: string; + + /** + * Lowercase PM. + */ + pm: string; + + /** + * Uppercase PM. + */ + PM: string; +}; + +export type FormatsConfig = { + /** + * Time format. + */ + time: string; + + /** + * Date format. + */ + date: string; + + /** + * Datetime format. + */ + datetime: string; + + /** + * Abbreviated datetime format. + */ + datetimeAbbreviated: string; +}; + +export type TimezoneConfig = { + /** + * Offset setting. + */ + offset: string; + + /** + * Offset setting with decimals formatted to minutes. + */ + offsetFormatted: string; + + /** + * The timezone as a string (e.g., `'America/Los_Angeles'`). + */ + string: string; + + /** + * Abbreviation for the timezone. + */ + abbr: string; +}; + +export type L10nSettings = { + /** + * Moment locale. + */ + locale: string; + + /** + * Locale months. + */ + months: MomentLocaleSpecification[ 'months' ]; + + /** + * Locale months short. + */ + monthsShort: MomentLocaleSpecification[ 'monthsShort' ]; + + /** + * Locale weekdays. + */ + weekdays: MomentLocaleSpecification[ 'weekdays' ]; + + /** + * Locale weekdays short. + */ + weekdaysShort: MomentLocaleSpecification[ 'weekdaysShort' ]; + + /** + * Meridiem config. + */ + meridiem: MeridiemConfig; + + /** + * Relative time config. + */ + relative: MomentLocaleSpecification[ 'relativeTime' ]; + + /** + * Day that the week starts on. + */ + startOfWeek: 0 | 1 | 2 | 3 | 4 | 5 | 6; +}; + +export type DateSettings = { + /** + * Localization settings. + */ + l10n: L10nSettings; + + /** + * Date/time formats config. + */ + formats: FormatsConfig; + + /** + * Timezone settings. + */ + timezone: TimezoneConfig; +}; From 73c5a2613b5aae9833d2c66f814bebc927ac20cc Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Wed, 4 Dec 2024 17:49:29 +0530 Subject: [PATCH 2/4] Export types --- packages/date/src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/date/src/index.ts b/packages/date/src/index.ts index 42fe71ae65460..701c3a2f6877a 100644 --- a/packages/date/src/index.ts +++ b/packages/date/src/index.ts @@ -15,6 +15,8 @@ import deprecated from '@wordpress/deprecated'; */ import type { DateSettings } from './types'; +export * from './types'; + const WP_ZONE = 'WP'; // This regular expression tests positive for UTC offsets as described in ISO 8601. From a1c9a0fc49871aabae8d48feb7aa5eb5477c0b9e Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Wed, 4 Dec 2024 18:00:52 +0530 Subject: [PATCH 3/4] Allow date value as number --- packages/date/README.md | 12 ++++++------ packages/date/src/index.ts | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/date/README.md b/packages/date/README.md index 56eed73cb3f8d..5ea2e472479a1 100644 --- a/packages/date/README.md +++ b/packages/date/README.md @@ -28,7 +28,7 @@ _Related_ _Parameters_ - _dateFormat_ `string`: PHP-style formatting string. See php.net/date. -- _dateValue_ `Moment | Date | string`: Date object or string, parsable by moment.js. +- _dateValue_ `Moment | Date | string | number`: Date object or string, parsable by moment.js. - _timezone_ `string`: Timezone to output result in or a UTC offset. Defaults to timezone from site. _Returns_ @@ -49,7 +49,7 @@ _Related_ _Parameters_ - _dateFormat_ `string`: PHP-style formatting string. See php.net/date. -- _dateValue_ `Moment | Date | string`: Date object or string, parsable by moment.js. +- _dateValue_ `Moment | Date | string | number`: Date object or string, parsable by moment.js. - _timezone_ `string | number | boolean`: Timezone to output result in or a UTC offset. Defaults to timezone from site. Notice: `boolean` is effectively deprecated, but still supported for backward compatibility reasons. _Returns_ @@ -63,7 +63,7 @@ Formats a date. Does not alter the date's timezone. _Parameters_ - _dateFormat_ `string`: PHP-style formatting string. See php.net/date. -- _dateValue_ `Moment | Date | string`: Date object or string, parsable by moment.js. +- _dateValue_ `Moment | Date | string | number`: Date object or string, parsable by moment.js. _Returns_ @@ -96,7 +96,7 @@ Formats a date (like `date()` in PHP), in the UTC timezone. _Parameters_ - _dateFormat_ `string`: PHP-style formatting string. See php.net/date. -- _dateValue_ `Moment | Date | string`: Date object or string, parsable by moment.js. +- _dateValue_ `Moment | Date | string | number`: Date object or string, parsable by moment.js. _Returns_ @@ -109,7 +109,7 @@ Formats a date (like `wp_date()` in PHP), translating it into site's locale and _Parameters_ - _dateFormat_ `string`: PHP-style formatting string. See php.net/date. -- _dateValue_ `Moment | Date | string`: Date object or string, parsable by moment.js. +- _dateValue_ `Moment | Date | string | number`: Date object or string, parsable by moment.js. _Returns_ @@ -134,7 +134,7 @@ Check whether a date is considered in the future according to the WordPress sett _Parameters_ -- _dateValue_ `string`: Date String or Date object in the Defined WP Timezone. +- _dateValue_ `Date | string`: Date String or Date object in the Defined WP Timezone. _Returns_ diff --git a/packages/date/src/index.ts b/packages/date/src/index.ts index 701c3a2f6877a..b4717cc90e8c4 100644 --- a/packages/date/src/index.ts +++ b/packages/date/src/index.ts @@ -405,7 +405,7 @@ const formatMap = { */ export function format( dateFormat: string, - dateValue: Moment | Date | string = new Date() + dateValue: Moment | Date | string | number = new Date() ) { let i, char; const newFormat = []; @@ -455,7 +455,7 @@ export function format( */ export function date( dateFormat: string, - dateValue: Moment | Date | string = new Date(), + dateValue: Moment | Date | string | number = new Date(), timezone?: string ) { const dateMoment = buildMoment( dateValue, timezone ); @@ -474,7 +474,7 @@ export function date( */ export function gmdate( dateFormat: string, - dateValue: Moment | Date | string = new Date() + dateValue: Moment | Date | string | number = new Date() ) { const dateMoment = momentLib( dateValue ).utc(); return format( dateFormat, dateMoment ); @@ -503,7 +503,7 @@ export function gmdate( */ export function dateI18n( dateFormat: string, - dateValue: Moment | Date | string = new Date(), + dateValue: Moment | Date | string | number = new Date(), timezone?: string | number | boolean ) { if ( true === timezone ) { @@ -532,7 +532,7 @@ export function dateI18n( */ export function gmdateI18n( dateFormat: string, - dateValue: Moment | Date | string = new Date() + dateValue: Moment | Date | string | number = new Date() ) { const dateMoment = momentLib( dateValue ).utc(); dateMoment.locale( settings.l10n.locale ); @@ -546,7 +546,7 @@ export function gmdateI18n( * * @return Is in the future. */ -export function isInTheFuture( dateValue: string ) { +export function isInTheFuture( dateValue: Date | string ) { const now = momentLib.tz( WP_ZONE ); const momentObject = momentLib.tz( dateValue, WP_ZONE ); @@ -600,7 +600,7 @@ export function humanTimeDiff( * @return A moment instance. */ function buildMoment( - dateValue?: Moment | Date | string, + dateValue?: Moment | Date | string | number, timezone: string | number = '' ) { const dateMoment = momentLib( dateValue ); From dab856ad447e760da9a0b439eb4b304956642fbb Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Wed, 4 Dec 2024 18:02:52 +0530 Subject: [PATCH 4/4] Update CHANGELOG.md --- packages/date/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/date/CHANGELOG.md b/packages/date/CHANGELOG.md index 21ddf97506403..b9cf7f0ba6e68 100644 --- a/packages/date/CHANGELOG.md +++ b/packages/date/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Enhancements + +- Improved TypeScript definitions ([67573](https://github.com/WordPress/gutenberg/pull/67573)) + ## 5.13.0 (2024-11-27) ## 5.12.0 (2024-11-16)