icambron / twix.js

:hourglass::left_right_arrow: A date range plugin for moment.js
https://isaaccambron.com/twix.js/
MIT License
379 stars 54 forks source link

issue with difference behaviour #70

Closed mathpere closed 9 years ago

mathpere commented 9 years ago

Hi!

First of all, thanks for this very useful library!

I met some unexpected issue with twix.js.

var range1 = moment('Fri Jun 05 2015 10:30:00 GMT+0200').twix('Fri Jun 12 2015 06:00:00 GMT+0200');

// The first argument is out of the range1
var diff =  range1.difference(
  moment('Fri Jun 05 2015 09:30:00 GMT+0200').twix('Fri Jun 05 2015 10:30:00 GMT+0200'),
  moment('Mon Jun 08 2015 10:00:00 GMT+0200').twix('Thu Jun 11 2015 10:00:00 GMT+0200')
);

// NB: I know that the moment instanciations are deprecated, this is just for example.

I was expected the following result array:

[
  Fri Jun 05 2015 10:30:00 GMT+0200 - Mon Jun 08 2015 10:00:00 GMT+0200, 
  Thu Jun 11 2015 10:00:00 GMT+0200 - Fri Jun 12 2015 06:00:00 GMT+0200
]

But I get this result:

[
  Thu Jun 11 2015 10:00:00 GMT+0200 - Fri Jun 12 2015 06:00:00 GMT+0200
]

Twix.js totally skips the first difference.

If I change the first difference argument by +1 minute as following:

var range1 = moment('Fri Jun 05 2015 10:30:00 GMT+0200').twix('Fri Jun 12 2015 06:00:00 GMT+0200');

// The first argument has intersection with the range1
var diff =  range1.difference(
  moment('Fri Jun 05 2015 09:30:00 GMT+0200').twix('Fri Jun 05 2015 10:31:00 GMT+0200'),
  moment('Mon Jun 08 2015 10:00:00 GMT+0200').twix('Thu Jun 11 2015 10:00:00 GMT+0200')
);

I get this expected result:

[
   Fri Jun 05 2015 10:31:00 GMT+0200 - Mon Jun 08 2015 10:00:00 GMT+0200, 
   Thu Jun 11 2015 10:00:00 GMT+0200 - Fri Jun 12 2015 06:00:00 GMT+0200
]

If I change the first difference argument by -1 minute as following:

var range1 = moment('Fri Jun 05 2015 10:30:00 GMT+0200').twix('Fri Jun 12 2015 06:00:00 GMT+0200');

// The first argument is no intersection with the range1 (=out)
var diff =  range1.difference(
  moment('Fri Jun 05 2015 09:30:00 GMT+0200').twix('Fri Jun 05 2015 10:29:00 GMT+0200'),
  moment('Mon Jun 08 2015 10:00:00 GMT+0200').twix('Thu Jun 11 2015 10:00:00 GMT+0200')
);

I get this expected result:

[
   Fri Jun 05 2015 10:30:00 GMT+0200 - Mon Jun 08 2015 10:00:00 GMT+0200, 
   Thu Jun 11 2015 10:00:00 GMT+0200 - Fri Jun 12 2015 06:00:00 GMT+0200
]

So this issue happens when one of the difference arguments has exactly the same date.

Can you confirm my observations?

icambron commented 9 years ago

Yeah, I can confirm. Looks like a bug in xor (which difference uses internally):

> var range1 = moment('Fri Jun 05 2015 10:30:00 GMT+0200').twix('Fri Jun 12 2015 06:00:00 GMT+0200');
> var range2 =  moment('Fri Jun 05 2015 09:30:00 GMT+0200').twix('Fri Jun 05 2015 10:30:00 GMT+0200');
> var range3 = moment('Fri Jun 05 2015 09:30:00 GMT+0200').twix('Fri Jun 05 2015 10:29:00 GMT+0200');
> range1.xor(range2).map(function(r){return r.format();});
[ 'Jun 5, 3:30 AM - Jun 12, 12 AM' ]
> range1.xor(range3).map(function(r){return r.format();});
[ 'Jun 5, 3:30 - 4:29 AM',
  'Jun 5, 4:30 AM - Jun 12, 12 AM' ]

I don't have time to dig into it right now, but I'm guessing there's something wrong with this line: https://github.com/icambron/twix.js/blob/develop/src/twix.coffee#L205. I'll see if I can set aside some time to debug it, but it might be a bit. Feel free to take a crack at it!

icambron commented 9 years ago

Fixed in the develop branch, will be in the next release. Thanks for the bug report!