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

After before modifier #393

Merged
merged 4 commits into from
Jun 11, 2017
Merged
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
19 changes: 13 additions & 6 deletions docs/Modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,30 @@ The value of a modifier can be either:
```
will match the days between the 12th and the 16th of January.

- **an object with a `before` key**, to match the days before the given date:
- **an object with a `before` and/or `after` key**, to match the days before and/or after the given date:
```js
const range = {
before: new Date(),
before: new Date(),
}
```
will match all the past the days (i.e. the days before today).

- **an object with a `after` key**, to match the days after the given date:
The code above will match all the past the days (i.e. the days before today).

```js
const range = {
after: new Date(2018, 0, 1),
}
```

will match all the days after the January, 1st 2018.
The code above will match all the days after the January, 1st 2018.

```js
const range = {
after: new Date(2020, 5, 20),
before: new Date(2020, 5, 30),
}
```

The code above will match all the days between the 30th and the 20th of April 2018.

- **an object with a `daysOfWeek` array**, to match specific days of week:

Expand Down
3 changes: 3 additions & 0 deletions src/ModifiersUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export function dayMatchesModifier(day, modifier) {
if (isRangeOfDates(mod)) {
return isDayInRange(day, mod);
}
if (mod.after && mod.before) {
return isDayAfter(day, mod.after) && isDayBefore(day, mod.before);
}
if (mod.after) {
return isDayAfter(day, mod.after);
}
Expand Down
43 changes: 34 additions & 9 deletions test/ModifierUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ describe('ModifiersUtils', () => {
);
expect(match2).to.be.true;
});
it('matches "after" and "before" modifiers', () => {
const afterModifier = {
after: new Date(2015, 8, 10),
before: new Date(2015, 8, 18),
};
const match = ModifiersUtils.dayMatchesModifier(
new Date(2015, 8, 15),
afterModifier
);
expect(match).to.be.true;

const fail = ModifiersUtils.dayMatchesModifier(
new Date(2014, 8, 9),
afterModifier
);
expect(fail).to.be.false;
});
it('matches "after" modifiers', () => {
const afterModifier = {
after: new Date(2015, 8, 18),
Expand Down Expand Up @@ -212,15 +229,9 @@ describe('ModifiersUtils', () => {
describe('getModifiersForDay', () => {
it('returns an array of modifiers', () => {
const modifierFunctions = {
yes() {
return true;
},
no() {
return false;
},
maybe(d) {
return d.getMonth() === 8;
},
yes: () => true,
no: () => false,
maybe: d => d.getMonth() === 8,
};
let modifiers = ModifiersUtils.getModifiersForDay(
new Date(2015, 8, 19),
Expand Down Expand Up @@ -348,6 +359,20 @@ describe('ModifiersUtils', () => {
);
expect(modifiers2.indexOf('foo')).to.equal(0);
});
it('returns an "after/before" modifier', () => {
const afterModifier = {
foo: {
after: new Date(2015, 8, 10),
before: new Date(2015, 8, 18),
},
};
const modifiers = ModifiersUtils.getModifiersForDay(
new Date(2015, 8, 15),
afterModifier
);
expect(modifiers).to.have.length(1);
expect(modifiers.indexOf('foo')).to.equal(0);
});
it('returns an "after" modifier', () => {
const afterModifier = {
foo: {
Expand Down
Loading