Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single moment range support and adjacent new ranges intersection interpretation #157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,22 @@ What are the intersecting ranges?
range.intersect(range2); // [moment.range(lol, end)]
```

Include adjacent ranges:

``` js
const a = moment('2016-03-15');
const b = moment('2016-03-20');
const c = moment('2016-03-20');
const d = moment('2016-03-25');

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

range1.intersect(range2) // null
range1.intersect(range2, { adjacent: false }) // null
range1.intersect(range2, { adjacent: true }) // [moment.range('2016-03,20', '2016-03,20')]
```

### Manipulation

#### Add
Expand Down
10 changes: 9 additions & 1 deletion lib/moment-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class DateRange {
return this.diff(unit, rounded);
}

intersect(other) {
intersect(other, options = { adjacent: false }) {
const start = this.start.valueOf();
const end = this.end.valueOf();
const oStart = other.start.valueOf();
Expand All @@ -169,6 +169,14 @@ export class DateRange {
else if ((start <= oStart) && (oStart <= oEnd) && (oEnd <= end)) {
return other;
}
else if (options.adjacent) {
if ((start <= end) && (end == oStart) && (oStart <= oEnd)) {
return new this.constructor(end, end);
}
else if ((oStart <= oEnd) && (oEnd == start) && (start <= end)) {
return new this.constructor(oEnd, oEnd);
}
}

return null;
}
Expand Down
Loading