Skip to content

Commit

Permalink
Add adjacent method and option to overlaps (#92 #112)
Browse files Browse the repository at this point in the history
  • Loading branch information
gf3 committed Dec 12, 2016
1 parent 30ff1c6 commit 25be063
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/moment-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export class DateRange {
this.end = (e === null) ? moment(8640000000000000) : moment(e);
}

adjacent(other: DateRange): bool {
const sameStartEnd = this.start.isSame(other.end);
const sameEndStart = this.end.isSame(other.start);

return (sameStartEnd && (other.start.valueOf() <= this.start.valueOf())) || (sameEndStart && (other.end.valueOf() >= this.end.valueOf()));
}

add(other: DateRange): ?DateRange {
if (this.overlaps(other)) {
return new this.constructor(moment.min(this.start, other.start), moment.max(this.end, other.end));
Expand Down Expand Up @@ -183,8 +190,14 @@ export class DateRange {
return this.isEqual(other);
}

overlaps(other: DateRange): bool {
return this.intersect(other) !== null;
overlaps(other: DateRange, options: { adjacent: bool; } = { adjacent: false }): bool {
const intersect = (this.intersect(other) !== null);

if (options.adjacent && !intersect) {
return this.adjacent(other);
}

return intersect;
}

reverseBy(interval: Shorthand, options?: { exclusive: bool; step: number; } = { exclusive: false, step: 1 }): Iterator<moment> {
Expand Down
71 changes: 71 additions & 0 deletions lib/moment-range_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,63 @@ describe('DateRange', function() {
});
});

describe('#adjacent', function() {
it('should correctly indicate when ranges aren\'t adjacent', function() {
const a = moment.range(d4, d1);
const b = moment.range(d3, d2);

expect(a.adjacent(b)).to.be(false);
});

it('should correctly indicate when a.start == b.start', function() {
const a = moment('15-Mar-2016');
const b = moment('29-Mar-2016');
const c = moment('15-Mar-2016');
const d = moment('30-Mar-2016');

const range1 = moment.range(a, b);
const range2 = moment.range(c, d);

expect(range1.adjacent(range2)).to.be(false);
});

it('should correctly indicate when a.start == b.end', function() {
const a = moment('15-Mar-2016');
const b = moment('29-Mar-2016');
const c = moment('10-Mar-2016');
const d = moment('15-Mar-2016');

const range1 = moment.range(a, b);
const range2 = moment.range(c, d);

expect(range1.adjacent(range2)).to.be(true);
});

it('should correctly indicate when a.end == b.start', function() {
const a = moment('15-Mar-2016');
const b = moment('20-Mar-2016');
const c = moment('20-Mar-2016');
const d = moment('25-Mar-2016');

const range1 = moment.range(a, b);
const range2 = moment.range(c, d);

expect(range1.adjacent(range2)).to.be(true);
});

it('should correctly indicate when a.end == b.end', function() {
const a = moment('15-Mar-2016');
const b = moment('20-Mar-2016');
const c = moment('10-Mar-2016');
const d = moment('20-Mar-2016');

const range1 = moment.range(a, b);
const range2 = moment.range(c, d);

expect(range1.adjacent(range2)).to.be(false);
});
});

describe('#clone()', function() {
it('should deep clone range', function() {
const dr1 = moment().range(sStart, sEnd);
Expand Down Expand Up @@ -633,6 +690,20 @@ describe('DateRange', function() {
expect(dr1.overlaps(dr3)).to.be(false);
expect(dr4.overlaps(dr3)).to.be(false);
});

it('should indicate if ranges overlap if the options is passed in', function() {
const a = moment('15-Mar-2016');
const b = moment('20-Mar-2016');
const c = moment('20-Mar-2016');
const d = moment('25-Mar-2016');

const range1 = moment.range(a, b);
const range2 = moment.range(c, d);

expect(range1.overlaps(range2)).to.be(false);
expect(range1.overlaps(range2, { adjacent: false })).to.be(false);
expect(range1.overlaps(range2, { adjacent: true })).to.be(true);
});
});

describe('#intersect()', function() {
Expand Down

0 comments on commit 25be063

Please sign in to comment.