Skip to content

Add momentjs object properties - #7298

Closed
psealock wants to merge 1 commit into
WordPress:masterfrom
psealock:add/momentjs-object-properties
Closed

psealock wants to merge 1 commit into
WordPress:masterfrom
psealock:add/momentjs-object-properties

Conversation

@psealock

Copy link
Copy Markdown
Contributor

Description

Momentjs has many methods on the moment object. Some of these are very useful. For instance isMoment, duration, or months are quite handy.

In particular, woocommerce/woocommerce-admin#104 requires access to the localData property in order to determine the localized format of a user facing input.

const localizedLongDateFormat = moment.localeData( 'fr' )._longDateFormat.l
const myDate = moment( aStringValue, [ 'YYYY-MM-DD', localizedLongDateFormat ], true );

This way, we can tell how the user has input a date, e.g. M/D/YYYY or D/M/YYYY

How has this been tested?

In the console, we can see momentjs properties available

wp.date.moment.isMoment( wp.date.moment() );
// true

Types of changes

New feature: Momentjs methods available from the library

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

Comment thread .idea/gutenberg.iml Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this and a few other files managed to slip in the pull request when they weren't meant to.

@psealock
psealock force-pushed the add/momentjs-object-properties branch from 0ed7624 to 79578cd Compare June 15, 2018 00:03
@psealock
psealock force-pushed the add/momentjs-object-properties branch from 79578cd to 5c968c9 Compare June 15, 2018 00:04
@psealock

Copy link
Copy Markdown
Contributor Author

@danielbachhuber Thanks, sorry about that. Fixed now

@danielbachhuber
danielbachhuber requested a review from a team June 15, 2018 11:23
@danielbachhuber

Copy link
Copy Markdown
Member

Thanks @psealock. Flagging this for review to see if this is an API we want to expose.

@danielbachhuber danielbachhuber added the [Feature] Extensibility The ability to extend blocks or the editing experience label Jun 15, 2018
@aaronjorbin

Copy link
Copy Markdown
Member

@psealock first off, looks like you linked to a private repo so I'm unable to review the issue you mentioned.

I have concerns about exposing more than is necessary in wp.date since it means that if there is ever a desire to switch to a different date library, we could mean a need to make a backward incompatible change or a need to write a shim. I'm not sure the increased maintenance burdon is beneficial.

@tofumatt tofumatt left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be exposing extra moment methods on our date object. If people want to use moment/moment's APIs they should import it directly. I think we should be exporting only what we explicitly need.

I'm not requesting changes so much as saying we shouldn't do this.

I suppose if there are particular methods (eg localData) that you want we could export them explicitly. But we then have to commit to supporting that API even if we moved away from moment under the hood. I think the aim here should be not to rely on moment's API.

};

// Make available other moment.js methods, such as moment.isMoment()
Object.keys( momentLib ).forEach( ( key ) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I don't think we should be exporting anything at this stage, this is not the right way to expose the library either. I think it'd be better to select the APIs you want to export. Otherwise we should just be exporting a moment property that is the entire API. But looping through the keys and adding a reference is inefficient and also not great for documentation as it doesn't explicitly make clear the APIs we're exporting.

@tofumatt tofumatt added the [Status] Not Implemented Issue/PR we will (likely) not implement. label Jun 17, 2018
@gziolo

gziolo commented Jun 18, 2018

Copy link
Copy Markdown
Member

When we were exposing date as an npm module, I wanted to refactor code to use date everwhere, but as @youknowriad stated in #6658 (comment):

The usage of the regular moment was intentional here. The idea is that we don't need any locale set, we just use moment as an utility function without global locale. (this avoids a dependency between components and date which I think would be nice to keep independent.)

As @tofumatt mentioned above, we don't want to maintain momentjs API. We enqueue momentjs for Gutenberg, so you can use it directly. It is the same approach we took in other places of the application.

I'm closing this PR as won't fix.

@gziolo gziolo closed this Jun 18, 2018
@psealock

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews everyone, I appreciate the comments.

This comment by @gziolo clarifies the desired path for extensions using Gutenberg who require more from the momentjs api to simply use it directly.

We enqueue momentjs for Gutenberg, so you can use it directly. It is the same approach we took in other places of the application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Extensibility The ability to extend blocks or the editing experience [Status] Not Implemented Issue/PR we will (likely) not implement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants