Skip to content

Commit

Permalink
Merge pull request #411 from alexmanzo/every-step-count
Browse files Browse the repository at this point in the history
Add stepCount parameter for every()
  • Loading branch information
spencermountain authored Mar 2, 2024
2 parents fbc1153 + e5228d7 commit 8bfc981
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions src/methods/every.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
}
Expand All @@ -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)) {
Expand All @@ -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
}
Expand Down
18 changes: 18 additions & 0 deletions test/every.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down

0 comments on commit 8bfc981

Please sign in to comment.