Skip to content

Commit

Permalink
Merge pull request #198 from jessicarobins/copy-options
Browse files Browse the repository at this point in the history
prevent new RRule from mutating passed-in options
  • Loading branch information
arolson101 committed Feb 15, 2018
2 parents 7a14410 + 01d3270 commit 64708c8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/rrule.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,16 @@
// used by toString()
this.origOptions = {}

this.options = {}

var invalid = []
var keys = Object.keys(options)
var defaultKeys = Object.keys(RRule.DEFAULT_OPTIONS)

// Shallow copy for origOptions and check for invalid
// Shallow copy for options and origOptions and check for invalid
keys.forEach(function (key) {
this.origOptions[key] = options[key]
this.options[key] = options[key]
if (!contains(defaultKeys, key)) invalid.push(key)
}, this)

Expand All @@ -464,10 +467,10 @@

// Merge in default options
defaultKeys.forEach(function (key) {
if (!contains(keys, key)) options[key] = RRule.DEFAULT_OPTIONS[key]
})
if (!contains(keys, key)) this.options[key] = RRule.DEFAULT_OPTIONS[key]
}, this)

var opts = this.options = options
var opts = this.options

if (opts.byeaster !== null) opts.freq = RRule.YEARLY
if (!opts.dtstart) opts.dtstart = new Date(new Date().setMilliseconds(0))
Expand Down
18 changes: 18 additions & 0 deletions test/rrule.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ describe('RRule', function () {
})
})

it('does not mutate the passed-in options object', function () {
var options = {
freq: RRule.MONTHLY,
dtstart: new Date(2013, 0, 1),
count: 3,
bymonthday: [28]
}
var rule = new RRule(options)

assert.deepEqual(options, {
freq: RRule.MONTHLY,
dtstart: new Date(2013, 0, 1),
count: 3,
bymonthday: [28]
})
assert.deepEqual(rule.origOptions, options)
})

testRecurring('missing Feb 28 https://github.com/jakubroztocil/rrule/issues/21',
new RRule({
freq: RRule.MONTHLY,
Expand Down

0 comments on commit 64708c8

Please sign in to comment.