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

Add getters for rrules, exrules, rdates, exdates #347

Merged
merged 6 commits into from
Jun 7, 2019
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,22 @@ Same as `RRule.prototype.before`.

Same as `RRule.prototype.after`.

##### `RRuleSet.prototype.rrules()`

Get list of included rrules in this recurrence set.

##### `RRuleSet.prototype.exrules()`

Get list of excluded rrules in this recurrence set.

##### `RRuleSet.prototype.rdates()`

Get list of included datetimes in this recurrence set.

##### `RRuleSet.prototype.exdates()`

Get list of excluded datetimes in this recurrence set.

* * * * *

#### `rrulestr` Function
Expand Down
37 changes: 37 additions & 0 deletions src/rruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { includes } from './helpers'
import IterResult from './iterresult'
import { iterSet } from './iterset'
import { QueryMethodTypes, IterResultType } from './types'
import { rrulestr } from './rrulestr'
import { optionsToString } from './optionstostring'

function createGetterSetter <T> (fieldName: string) {
Expand Down Expand Up @@ -99,6 +100,42 @@ export default class RRuleSet extends RRule {
_addDate(date, this._exdate)
}

/**
* Get list of included rrules in this recurrence set.
*
* @return List of rrules
*/
rrules () {
return this._rrule.map(e => rrulestr(e.toString()))
}

/**
* Get list of excluded rrules in this recurrence set.
*
* @return List of exrules
*/
exrules () {
return this._exrule.map(e => rrulestr(e.toString()))
}

/**
* Get list of included datetimes in this recurrence set.
*
* @return List of rdates
*/
rdates () {
return this._rdate.map(e => new Date(e.getTime()))
}

/**
* Get list of included datetimes in this recurrence set.
*
* @return List of exdates
*/
exdates () {
return this._exdate.map(e => new Date(e.getTime()))
}

valueOf () {
let result: string[] = []

Expand Down
46 changes: 45 additions & 1 deletion test/rruleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,4 +727,48 @@ describe('RRuleSet', function () {
expect(() => set.rdate('foo' as any)).to.throw()
expect(() => set.exdate('foo' as any)).to.throw()
})
})

describe('getters', () => {
it('rrules()', () => {
let set = new RRuleSet();
let rrule = new RRule({
freq: RRule.YEARLY,
count: 2,
dtstart: parse('19600101T090000'),
tzid: 'America/New_York'
});
set.rrule(rrule);

expect(set.rrules().map(e => e.toString())).eql([rrule.toString()]);
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one small style nitpick: can we make sure there are blank lines in between it blocks? thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


it('exrules()', () => {
let set = new RRuleSet();
let rrule = new RRule({
freq: RRule.YEARLY,
count: 2,
dtstart: parse('19600101T090000'),
tzid: 'America/New_York'
});
set.exrule(rrule);

expect(set.exrules().map(e => e.toString())).eql([rrule.toString()]);
});

it('rdates()', () => {
let set = new RRuleSet();
let dt = parse('19610201T090000');
set.rdate(dt);

expect(set.rdates()).eql([dt]);
});

it('exdates()', () => {
let set = new RRuleSet();
let dt = parse('19610201T090000');
set.exdate(dt);

expect(set.exdates()).eql([dt]);
});
});
});