Skip to content

Commit

Permalink
Make RRuleSet.dtstart() a getter/setter and refactor common code with…
Browse files Browse the repository at this point in the history
… RRuleSet.tzid()
  • Loading branch information
Martin Jesper Low Madsen committed Apr 28, 2019
1 parent 9f50e91 commit e1146a3
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/rruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ import { iterSet } from './iterset'
import { QueryMethodTypes, IterResultType } from './types'
import { optionsToString } from './optionstostring'

function createGetterSetter <T> (fieldName: string) {
return (field?: T) => {
if (field !== undefined) {
this[`_${fieldName}`] = field
}

if (this[`_${fieldName}`] !== undefined) {
return this[`_${fieldName}`]
}

for (let i = 0; i < this._rrule.length; i++) {
const field: T = this._rrule[i].origOptions[fieldName]
if (field) {
return field
}
}
}
}

export default class RRuleSet extends RRule {
public readonly _rrule: RRule[]
public readonly _rdate: Date[]
Expand All @@ -30,27 +49,8 @@ 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
}

if (this._tzid !== undefined) {
return this._tzid
}

for (let i = 0; i < this._rrule.length; i++) {
const tzid = this._rrule[i].origOptions.tzid
if (tzid) {
return tzid
}
}
return undefined
}
dtstart = createGetterSetter.apply(this, ['dtstart'])
tzid = createGetterSetter.apply(this, ['tzid'])

_iter <M extends QueryMethodTypes> (iterResult: IterResult<M>): IterResultType<M> {
return iterSet(
Expand Down

0 comments on commit e1146a3

Please sign in to comment.