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..18e4f224 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 [] } @@ -28,7 +28,10 @@ const every = function (start, unit, end) { 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)) { @@ -44,7 +47,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..ca6cdc8d 100644 --- a/test/every.test.js +++ b/test/every.test.js @@ -18,6 +18,24 @@ 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, 0, 'interval/step count too large for range') + + t.end() +}) + test('monday-sunday', (t) => { let days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] let start = spacetime('April 8th 2019').startOf('week') 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[]