From d1bb374f637d2e260db588569b51a7a119e40763 Mon Sep 17 00:00:00 2001 From: Alex Manzo Date: Thu, 29 Feb 2024 14:59:31 -0500 Subject: [PATCH 1/3] Add stepCount to every function --- src/methods.js | 4 ++-- src/methods/every.js | 4 ++-- test/every.test.js | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/methods.js b/src/methods.js index 2c4df087..e96fb821 100644 --- a/src/methods.js +++ b/src/methods.js @@ -103,14 +103,14 @@ const methods = { return s }, //get each week/month/day between a -> b - every: function (unit, to) { + every: function (unit, to, stepCount) { // allow swapping these params: if (typeof unit === 'object' && typeof to === 'string') { let tmp = to to = unit unit = tmp } - return every(this, unit, to) + return every(this, unit, to, stepCount) }, isAwake: function () { let hour = this.hour() diff --git a/src/methods/every.js b/src/methods/every.js index 66a2b109..627b701d 100644 --- a/src/methods/every.js +++ b/src/methods/every.js @@ -14,7 +14,7 @@ const isDay = function (unit) { // return a list of the weeks/months/days between a -> b // returns spacetime objects in the timezone of the input -const every = function (start, unit, end) { +const every = function (start, unit, end, stepCount = 1) { if (!unit || !end) { return [] } @@ -44,7 +44,7 @@ const every = function (start, unit, end) { let result = [] while (d.isBefore(end)) { result.push(d) - d = d.add(1, unit) + d = d.add(stepCount, unit) } return result } diff --git a/test/every.test.js b/test/every.test.js index df4733f8..9def6380 100644 --- a/test/every.test.js +++ b/test/every.test.js @@ -18,6 +18,25 @@ test('every-unit', (t) => { t.end() }) +test('step-count', (t) => { + let start = spacetime('April 6th 2019', 'Europe/Paris') + let end = spacetime('April 20th 2019', 'Europe/Paris').add(3, 'years') + + let biannualInterval = start.every('quarter', end, 2) + t.equal(biannualInterval.length, 6, 'every 2 quarters') + t.equal(biannualInterval[0].timezone().name, 'Europe/Paris', 'results in right timezone') + + let fortnights = start.every('week', end, 2) + t.equal(fortnights.length, 80, 'every fortnight') + t.equal(biannualInterval[0].timezone().name, 'Europe/Paris', 'results in right timezone') + + let everyFourYears = start.every('years', end, 4) + t.equal(everyFourYears.length, 1, 'every four years') + t.equal(everyFourYears[0].timezone().name, 'Europe/Paris', 'results in right timezone') + + t.end() +}) + test('monday-sunday', (t) => { let days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] let start = spacetime('April 8th 2019').startOf('week') From eb86b353c2a451eeee07f949e8c3b6d42c6bf6ee Mon Sep 17 00:00:00 2001 From: Alex Manzo Date: Thu, 29 Feb 2024 15:02:28 -0500 Subject: [PATCH 2/3] Update types. --- types/types.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/types.d.ts b/types/types.d.ts index 4be20a42..e12380c8 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -66,7 +66,7 @@ export interface Spacetime { round: (unit: TimeUnit) => Spacetime /** list all dates up to a certain time */ - every: (unit: Spacetime | string | TimeUnit, end: Spacetime | string | TimeUnit) => Spacetime[] + every: (unit: Spacetime | string | TimeUnit, end: Spacetime | string | TimeUnit, stepCount: number) => Spacetime[] /** list all dates up to a certain time */ each: (unit: TimeUnit, end: Spacetime | string) => Spacetime[] From e5228d7dfc6504cf12e79b14465f6938966a6913 Mon Sep 17 00:00:00 2001 From: Alex Manzo Date: Thu, 29 Feb 2024 16:00:36 -0500 Subject: [PATCH 3/3] Handle stepCount exceeding time range. --- src/methods/every.js | 5 ++++- test/every.test.js | 3 +-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/methods/every.js b/src/methods/every.js index 627b701d..18e4f224 100644 --- a/src/methods/every.js +++ b/src/methods/every.js @@ -28,7 +28,10 @@ const every = function (start, unit, end, stepCount = 1) { start = end end = tmp } - + //prevent going beyond end if unit/stepCount > than the range + if (start.diff(end, unit) < stepCount) { + return [] + } //support 'every wednesday' let d = start.clone() if (isDay(unit)) { diff --git a/test/every.test.js b/test/every.test.js index 9def6380..ca6cdc8d 100644 --- a/test/every.test.js +++ b/test/every.test.js @@ -31,8 +31,7 @@ test('step-count', (t) => { t.equal(biannualInterval[0].timezone().name, 'Europe/Paris', 'results in right timezone') let everyFourYears = start.every('years', end, 4) - t.equal(everyFourYears.length, 1, 'every four years') - t.equal(everyFourYears[0].timezone().name, 'Europe/Paris', 'results in right timezone') + t.equal(everyFourYears.length, 0, 'interval/step count too large for range') t.end() })