Skip to content
This repository was archived by the owner on Jul 10, 2019. It is now read-only.

Add now option to format - #27

Merged
ericf merged 3 commits into
formatjs:masterfrom
jbaudanza:master
Mar 9, 2015
Merged

ericf merged 3 commits into
formatjs:masterfrom
jbaudanza:master

Conversation

@jbaudanza

Copy link
Copy Markdown
Contributor

This PR allows the caller to specify a custom value for now.

This implements the solution discussed in #26

@yahoocla

yahoocla commented Mar 8, 2015

Copy link
Copy Markdown

Thank you for submitting this pull request, however I do not see a valid CLA on file for you. Before we can merge this request please visit https://yahoocla.herokuapp.com/ and agree to the terms. Thanks! 😄

@yahoocla

yahoocla commented Mar 8, 2015

Copy link
Copy Markdown

CLA is valid!

Comment thread src/core.js Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This will not pass when now is 0, which is a valid value.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think that if options.now has a value other than undefined, then we should throw if that value is not finite, just like we do for date.

@ericf

ericf commented Mar 8, 2015

Copy link
Copy Markdown
Collaborator

This should behave the following ways for these options.now values (tests should be added for these too):

  • undefined: should not throw, instead the current Date.now() value should be used.
  • !isFinite(options.now): should throw.
  • null: is fine because it passes isFinite(), and will be the same as 0 or the epoch.
  • 0: is fine because it passes isFinite(), and will be the epoch value.

Another interesting case is what happens when the date param is undefined, but an options.now value was provided. Here's what I think that should be:

var relativeFormat = new IntlRelativeFormat('en');
var output = relativeFormat.format(undefined, {options.now: 0});
console.log(output); // => "now"

So the algorithm would be, first figure out the now value, and if the date value is undefined then it becomes the same as the now value which might be user supplied or default to the current value of Date.now() at the time the function is called.

Comment thread src/core.js Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The following needs to be valid, but in the current code will throw.

relativeFormat.format(new Date(), {now: undefined});

Change to:

var now = options && options.now !== undefined ? options.now : dateNow();

@jbaudanza

Copy link
Copy Markdown
Contributor Author

OK, those conditions sound right to me. I just pushed a commit around the same time you left that comment. I think it passes all those criteria but I'll go back and add some tests.

@ericf

ericf commented Mar 8, 2015

Copy link
Copy Markdown
Collaborator

@jbaudanza sounds good, and thanks for doing this! I left one more comment here: #27 (comment)

@jbaudanza

Copy link
Copy Markdown
Contributor Author

OK. thanks for the good feedback. LMK if you want me to rebase or leave the history intact.

@ericf

ericf commented Mar 8, 2015

Copy link
Copy Markdown
Collaborator

@jbaudanza looks good to me, let's keep the history, it tells the important story of how we want to handle date/timestamp values.

I'll merge this in tonight or tomorrow morning and update the docs, etc. This will be in v1.1.0 along with some other changes I have locally that I plan to open a PR for tomorrow. Thanks again!

@jbaudanza

Copy link
Copy Markdown
Contributor Author

no problem. happy to help!

@caridy

caridy commented Mar 9, 2015

Copy link
Copy Markdown
Collaborator

👍

ericf added a commit that referenced this pull request Mar 9, 2015
Add `now` option to format
@ericf
ericf merged commit 2495e18 into formatjs:master Mar 9, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants