Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor format function and add createMomentDate function #61260

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/date/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ _This package assumes that your code will run in an **ES2015+** environment. If

<!-- START TOKEN(Autogenerated API docs) -->

### createMomentDate

Creates a Moment.js date format from a PHP-style date format.

_Parameters_

- _dateString_ `string`: date string
- _dateFormat_ `string`: PHP-style formatting string. See php.net/date.

_Returns_

- `Moment`: date format recognizable by Moment.js.

### date

Formats a date (like `date()` in PHP).
Expand Down
51 changes: 41 additions & 10 deletions packages/date/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ 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 wp.date.format} and {@link wp.date.createMomentDate}, 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 wp.date.format} and {@link wp.date.createMomentDate}, not
* directly.
*/
const formatMap = {
Expand Down Expand Up @@ -442,19 +442,17 @@ const formatMap = {
};

/**
* Formats a date. Does not alter the date's timezone.
* Creates a Moment.js date format from a PHP-style date format.
*
* @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 {Moment} momentDate Moment.js date object
* @param {string} dateFormat PHP-style formatting string.
* See php.net/date.
*
* @return {string} Formatted date.
* @return {string[]} date format recognizable by Moment.js.
*/
export function format( dateFormat, dateValue = new Date() ) {
function createMomentDateFormat( momentDate, dateFormat ) {
let i, char;
const newFormat = [];
const momentDate = momentLib( dateValue );
for ( i = 0; i < dateFormat.length; i++ ) {
char = dateFormat[ i ];
// Is this an escape?
Expand All @@ -478,11 +476,44 @@ export function format( dateFormat, dateValue = new Date() ) {
newFormat.push( '[' + char + ']' );
}
}
return newFormat;
}

/**
* 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.
*
* @return {string} Formatted date.
*/
export function format( dateFormat, dateValue = new Date() ) {
const momentDate = momentLib( dateValue );
const newFormat = createMomentDateFormat( momentDate, dateFormat );
// Join with [] between to separate characters, and replace
// unneeded separators with static text.
return momentDate.format( newFormat.join( '[]' ) );
}

/**
* Creates a Moment.js date format from a PHP-style date format.
*
* @param {string} dateString date string
* @param {string} dateFormat PHP-style formatting string.
* See php.net/date.
*
* @return {Moment} date format recognizable by Moment.js.
*/
export function createMomentDate( dateString, dateFormat ) {
const newFormat = createMomentDateFormat(
momentLib( dateString ),
dateFormat
);
return momentLib( dateString, newFormat.join( '[]' ) );
}

/**
* Formats a date (like `date()` in PHP).
*
Expand Down
12 changes: 12 additions & 0 deletions packages/date/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isInTheFuture,
setSettings,
humanTimeDiff,
createMomentDate,
} from '../';

describe( 'isInTheFuture', () => {
Expand Down Expand Up @@ -660,3 +661,14 @@ describe( 'Moment.js Localization', () => {
} );
} );
} );

describe( 'Function createMomentDate', () => {
it( 'should create a moment date using the provided date format', () => {
const momentDate = createMomentDate(
'2024 年 4 月 24 日 09:48',
'Y 年 n 月 j 日 H:i'
);
expect( console ).toHaveWarned(); // moment throws a warning that value provided is not in a recognized RFC2822 or ISO format.
expect( momentDate.isValid() ).toBe( true );
} );
} );