moment-range icon indicating copy to clipboard operation
moment-range copied to clipboard

Suggested feature: allow range to explicitly direct itself

Open jrencz opened this issue 9 years ago • 4 comments

Now if interval is passed then it's implicitly directed: https://github.com/gf3/moment-range/blob/72e31df65da59e39dbde540c500d8d7da4f82995/lib/moment-range.js#L321-L323 but if 2 moment objects are passed they are not. And there's no way to direct the range created that way.

It would be nice to be able to pass any 2 dates to range() and call some method that will direct the dates so earlier date is start and later date is end no matter which order they were passed in to the range() function.

It may work like this:

moment.range([
  momentA,
  momentB
].sort((a, b) => Math.sign(a.diff(b))))

Suggested api:

moment.range(momentA, momentB).direct()

optionally direct() method might could take a parameter and if it's falsy or negative the range should be directed from later moment do earlier (Math.sign(b.diff(a)))

jrencz avatar Feb 18 '16 13:02 jrencz

the constructor could implicitly do this? thoughts?

gf3 avatar Dec 08 '16 03:12 gf3

this way it will not be backwards compatible (although logical). IMO Either a method or on next major release

jrencz avatar Dec 13 '16 09:12 jrencz

What about

moment.range(momentA, momentB, true);

That way in the next major version you can throw it away by ignoring the extra argument or exceptioning on === false.

lukemcgregor avatar Aug 24 '17 01:08 lukemcgregor

We're looking at compiling all the breaking changes into the next major release (4.0.0) and I expect this feature will make a good candidate.

We thought the simplest approach was to make the constructor always perform this behaviour. I can't think of any reasons why you would ever want a reverse-time range? Even if a case does exist, it makes sense to keep the library working for forward ranges as the user can easily add their own application logic to keep track of it.

Our proposed solution: https://github.com/rotaready/moment-range/pull/201/

TristanJM avatar Dec 04 '17 21:12 TristanJM