From 9cd182fe3ff6e03fed960b00e2c91679533fbf66 Mon Sep 17 00:00:00 2001 From: David Golightly Date: Thu, 9 Jun 2022 15:43:25 +0200 Subject: [PATCH 1/3] validate dates passed in to DateWithZone --- src/datewithzone.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/datewithzone.ts b/src/datewithzone.ts index cac4f393..fc958a5b 100644 --- a/src/datewithzone.ts +++ b/src/datewithzone.ts @@ -5,6 +5,9 @@ export class DateWithZone { public tzid?: string | null constructor(date: Date, tzid?: string | null) { + if (isNaN(date.getTime())) { + throw new RangeError('Invalid date passed to DateWithZone') + } this.date = date this.tzid = tzid } From 75b42ce5469ae9147246ef2e3ab940eca22fb7d0 Mon Sep 17 00:00:00 2001 From: David Golightly Date: Thu, 9 Jun 2022 15:57:36 +0200 Subject: [PATCH 2/3] fix format passed to Date.parse --- test/datewithzone.test.ts | 4 ++-- test/rruleset.test.ts | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/test/datewithzone.test.ts b/test/datewithzone.test.ts index 64ec321a..f69f6bbd 100644 --- a/test/datewithzone.test.ts +++ b/test/datewithzone.test.ts @@ -42,11 +42,11 @@ describe('rezonedDate', () => { const currentLocalDate = new Date(2000, 1, 6, 1, 0, 0) setMockDate(currentLocalDate) - const d = new Date(Date.parse('20101005T110000')) + const d = new Date(Date.parse('2010-10-05T11:00:00')) const dt = new DateWithZone(d, targetZone) expect(dt.rezonedDate()).to.deep.equal( expectedDate( - new Date(Date.parse('20101005T110000')), + new Date(Date.parse('2010-10-05T11:00:00')), currentLocalDate, targetZone ) diff --git a/test/rruleset.test.ts b/test/rruleset.test.ts index 619c726c..cf3de565 100644 --- a/test/rruleset.test.ts +++ b/test/rruleset.test.ts @@ -622,11 +622,11 @@ describe('RRuleSet', function () { set.tzid(targetZone) - set.rdate(new Date(Date.parse('20020301T090000'))) + set.rdate(new Date(Date.parse('2002-03-01T09:00:00'))) expect(set.all()).to.deep.equal([ expectedDate( - new Date(Date.parse('20020301T090000')), + new Date(Date.parse('2002-03-01T09:00:00')), currentLocalDate, targetZone ), @@ -826,8 +826,8 @@ describe('RRuleSet', function () { describe('getters', () => { it('rrules()', () => { - let set = new RRuleSet() - let rrule = new RRule({ + const set = new RRuleSet() + const rrule = new RRule({ freq: RRule.YEARLY, count: 2, dtstart: parse('19600101T090000'), @@ -839,8 +839,8 @@ describe('RRuleSet', function () { }) it('exrules()', () => { - let set = new RRuleSet() - let rrule = new RRule({ + const set = new RRuleSet() + const rrule = new RRule({ freq: RRule.YEARLY, count: 2, dtstart: parse('19600101T090000'), @@ -852,16 +852,16 @@ describe('RRuleSet', function () { }) it('rdates()', () => { - let set = new RRuleSet() - let dt = parse('19610201T090000') + const set = new RRuleSet() + const dt = parse('19610201T090000') set.rdate(dt) expect(set.rdates()).eql([dt]) }) it('exdates()', () => { - let set = new RRuleSet() - let dt = parse('19610201T090000') + const set = new RRuleSet() + const dt = parse('19610201T090000') set.exdate(dt) expect(set.exdates()).eql([dt]) From b315bdf1ac4efe237940c23859dd2ca8dba70a6b Mon Sep 17 00:00:00 2001 From: David Golightly Date: Thu, 9 Jun 2022 16:06:16 +0200 Subject: [PATCH 3/3] test validation --- test/datewithzone.test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/datewithzone.test.ts b/test/datewithzone.test.ts index f69f6bbd..949a5656 100644 --- a/test/datewithzone.test.ts +++ b/test/datewithzone.test.ts @@ -30,6 +30,12 @@ it('returns the time of the date', () => { expect(dt.getTime()).to.equal(d.getTime()) }) +it('rejects invalid dates', () => { + expect(() => new DateWithZone(new Date(undefined))).to.throw( + 'Invalid date passed to DateWithZone' + ) +}) + describe('rezonedDate', () => { it('returns the original date when no zone is given', () => { const d = new Date(Date.UTC(2010, 9, 5, 11, 0, 0))