Skip to content

Commit

Permalink
Add support for parsing an rrule string without frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Jesper Low Madsen committed Apr 28, 2019
1 parent 211d215 commit 9f50e91
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/rruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { includes } from './helpers'
import IterResult from './iterresult'
import { iterSet } from './iterset'
import { QueryMethodTypes, IterResultType } from './types'
import { optionsToString } from './optionstostring'

export default class RRuleSet extends RRule {
public readonly _rrule: RRule[]
public readonly _rdate: Date[]
public readonly _exrule: RRule[]
public readonly _exdate: Date[]

private _dtstart?: Date | null | undefined
private _tzid?: string

/**
Expand All @@ -27,6 +30,10 @@ export default class RRuleSet extends RRule {
this._exdate = []
}

dtstart (dtstart?: Date | null | undefined) {
this._dtstart = dtstart
}

tzid (tzid?: string) {
if (tzid !== undefined) {
this._tzid = tzid
Expand Down Expand Up @@ -94,6 +101,11 @@ export default class RRuleSet extends RRule {

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

if (!this._rrule.length && this._dtstart) {
result = result.concat(optionsToString({ dtstart: this._dtstart }))
}

this._rrule.forEach(function (rrule) {
result = result.concat(rrule.toString().split('\n'))
})
Expand Down
4 changes: 3 additions & 1 deletion src/rrulestr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ function buildRule (s: string, options: Partial<RRuleStrOptions>) {
exdatevals.length
) {
const rset = new RRuleSet(noCache)

rset.dtstart(dtstart)
rset.tzid(tzid || undefined)

rrulevals.forEach(val => {
Expand Down Expand Up @@ -145,7 +147,7 @@ function buildRule (s: string, options: Partial<RRuleStrOptions>) {
return rset
}

const val = rrulevals[0]
const val = rrulevals[0] || {}
return new RRule(groomRruleOptions(
val,
val.dtstart || options.dtstart || dtstart,
Expand Down
11 changes: 11 additions & 0 deletions test/rrulestr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ describe('rrulestr', function () {
)).to.be.instanceof(RRule)
})

it('parses an rrule without frequency', () => {
const rRuleString = 'DTSTART:19970902T090000Z';
const parsedRRuleSet = rrulestr(
rRuleString, { forceset: true }
) as RRuleSet;
expect(parsedRRuleSet.toString()).to.be.equal(rRuleString);

const parsedRRule = rrulestr(rRuleString) as RRule;
expect(parsedRRule.toString()).to.be.equal(rRuleString);
})

it('parses an rruleset when forceset=true', () => {
expect(rrulestr(
'DTSTART:19970902T090000Z\n' +
Expand Down

0 comments on commit 9f50e91

Please sign in to comment.