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

exclude and xor #40

Closed neverfox closed 10 years ago

neverfox commented 10 years ago

While I'm sure this can be built up with iterators, it would be great to have an exclude and xor to complement union and intersection, as seen here. Pretty much the first thing I thought of using twix for was an exclude, but it wasn't available. The problem, of course, is how to handle the fact that both can result in two ranges in certain cases. That's probably why it has been avoided. Any thoughts?

var range1 = moment("1982-05-25").twix("1982-05-30");
var range2 = moment("1982-05-27").twix("1982-06-13");

range2.exclude(range1); //=> 5/31/82 - 6/13/1982
icambron commented 10 years ago

This seems reasonable, but I'm wondering if exclude should just be a.xor(b)[0] use a.intersect() on each of the results of a.xor(b) and toss the empties. I'd take pull a pull request either way.

Along similar lines, I'd love to add range.split(date*) and range.split(interval).

icambron commented 10 years ago

Started the branch xor, which a working implementation of xor().

Still needs support for all day ranges and tests for multiple inputs (since it allows for a.xor(b,c)). I also started a couple other features on the same branch; I'll figure out what to do with them as I go.

icambron commented 10 years ago

It's in the develop branch. I switched from exclude to difference

range1.difference(range2);
range1.xor(range2);

range1.difference(range2, range3, ...);
range1.xor(range2, range3, ...);