Skip to content

Commit

Permalink
Merge pull request #2 from jamiter/fix/current-should-include-today
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiter committed Aug 16, 2018
2 parents 4ed1dac + 02ec199 commit e632cc4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ You can use `moment().current(measure)`:
var thisMonth = moment().current('month');

// thisMonth.start = start of the month
// thisMonth.end = yesterday
// thisMonth.end = today
// thisMonth.length = the number of days since the start of this month
```

Expand Down
6 changes: 5 additions & 1 deletion src/relative-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ const rangeSchema = {
},
margin: {
type: Number,
default: 1,
calculate(value?: ?number): number | void | null {
const defaultValue = this.type === RANGE_TYPES.current ? 0 : 1;

return value !== undefined ? value : defaultValue;
},
},
start: {
type: Date,
Expand Down
13 changes: 8 additions & 5 deletions tests/relative-range.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('RelativeRange', function () {
expect(clone.units).to.equal(5);
expect(clone.measure).to.equal('day');
expect(clone.type).to.equal('previous');
expect(clone.margin).to.equal(1);
});
});

Expand All @@ -87,6 +88,7 @@ describe('RelativeRange', function () {
expect(clone.units).to.equal(1);
expect(clone.measure).to.equal('month');
expect(clone.type).to.equal('current');
expect(clone.margin).to.equal(0);
});
});

Expand All @@ -99,6 +101,7 @@ describe('RelativeRange', function () {
expect(clone.units).to.equal(3);
expect(clone.measure).to.equal('month');
expect(clone.type).to.equal('next');
expect(clone.margin).to.equal(1);
});
});

Expand Down Expand Up @@ -282,35 +285,35 @@ describe('RelativeRange', function () {
this.range.measure = 'week';

expect(this.range.start.format(DAY_FORMAT), 'start date').to.equal('3000-02-09');
expect(this.range.end.format(DAY_FORMAT), 'end date').to.equal('3000-02-11');
expect(this.range.end.format(DAY_FORMAT), 'end date').to.equal('3000-02-12');
});

it('isoWeek', function () {
this.range.measure = 'isoWeeks';

expect(this.range.start.format(DAY_FORMAT), 'start date').to.equal('3000-02-10');
expect(this.range.end.format(DAY_FORMAT), 'end date').to.equal('3000-02-11');
expect(this.range.end.format(DAY_FORMAT), 'end date').to.equal('3000-02-12');
});

it('month', function () {
this.range.measure = 'month';

expect(this.range.start.format(DAY_FORMAT), 'start date').to.equal('3000-02-01');
expect(this.range.end.format(DAY_FORMAT), 'end date').to.equal('3000-02-11');
expect(this.range.end.format(DAY_FORMAT), 'end date').to.equal('3000-02-12');
});

it('year', function () {
this.range.measure = 'year';

expect(this.range.start.format(DAY_FORMAT), 'start date').to.equal('3000-01-01');
expect(this.range.end.format(DAY_FORMAT), 'end date').to.equal('3000-02-11');
expect(this.range.end.format(DAY_FORMAT), 'end date').to.equal('3000-02-12');
});

it('quarter', function () {
this.range.measure = 'quarter';

expect(this.range.start.format(DAY_FORMAT), 'start date').to.equal('3000-01-01');
expect(this.range.end.format(DAY_FORMAT), 'end date').to.equal('3000-02-11');
expect(this.range.end.format(DAY_FORMAT), 'end date').to.equal('3000-02-12');
});
});

Expand Down

0 comments on commit e632cc4

Please sign in to comment.