From e1146a337a3ed6218aef87485558df29511a2187 Mon Sep 17 00:00:00 2001 From: Martin Jesper Low Madsen Date: Sun, 28 Apr 2019 19:06:52 +0200 Subject: [PATCH] Make RRuleSet.dtstart() a getter/setter and refactor common code with RRuleSet.tzid() --- src/rruleset.ts | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/rruleset.ts b/src/rruleset.ts index 90260788..6947c9da 100644 --- a/src/rruleset.ts +++ b/src/rruleset.ts @@ -6,6 +6,25 @@ import { iterSet } from './iterset' import { QueryMethodTypes, IterResultType } from './types' import { optionsToString } from './optionstostring' +function createGetterSetter (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[] @@ -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 (iterResult: IterResult): IterResultType { return iterSet(