Skip to content

Commit

Permalink
update: functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calebpitan committed Oct 25, 2024
1 parent d97c888 commit 84dddda
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/components/__tests__/EditableText.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, it, expect } from 'vitest'

import { mount } from '@vue/test-utils'
import EditableText from '../editable/EditableText.vue'

const sleep = (till: number) =>
new Promise<number>((resolve) => setTimeout(() => resolve(till), till))
import { describe, expect, it } from 'vitest'

import { sleep } from '@/utils'

import EditableText from '../editable/EditableText.vue'

describe('EditableText', () => {
it('renders properly', () => {
Expand Down
59 changes: 59 additions & 0 deletions src/utils/scheduling.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, expect, it } from 'vitest'

import {
nextHourlySchedule,
nextWeeklySchedule,
nextWeeklyScheduleCorrectionFactor
} from './scheduling'

const timestamp = new Date('2024-10-21T14:19:00.000Z')

describe('#nextHourlySchedule', () => {
it('should compute the next hourly datetime', () => {
const next = nextHourlySchedule(timestamp, 1)
const expected = new Date()

expected.setUTCHours(
expected.getUTCHours() + (expected.getUTCMinutes() > timestamp.getUTCMinutes() ? 1 : 0),
timestamp.getUTCMinutes(),
timestamp.getUTCSeconds(),
timestamp.getUTCMilliseconds()
)

console.log(expected.getUTCHours(), next)

expect(next.toISOString()).toEqual(expected.toISOString())
})
})

describe('#nextWeeklySchedule', () => {
it('should compute the next weekly datetime', () => {
{
const schedule = { weeks: 1, weekday: 4 }
const next = nextWeeklySchedule(timestamp, schedule.weeks)
expect(next.toISOString()).toEqual('2024-10-28T14:19:00.000Z')
}

{
const schedule = { weeks: 2, weekday: 4 }
const next = nextWeeklySchedule(timestamp, schedule.weeks)
expect(next.toISOString()).toEqual('2024-11-04T14:19:00.000Z')
}
})
})

describe('#nextWeeklyScheduleCorrectionFactor', () => {
it('should compute the correction factor for adjusting days of weekly schedule', () => {
{
const schedule = { weeks: 1, weekday: 4 }
const next = nextWeeklySchedule(timestamp, schedule.weeks)
const correctionFactor = nextWeeklyScheduleCorrectionFactor(next, schedule.weekday)
const nextDate = new Date(Math.round(next.getTime() * correctionFactor))

expect(nextDate.toISOString()).toEqual('2024-10-31T14:19:00.000Z')
}
})
})

// const weekdayOffset = next.getUTCDay() - schedule.weekday
// next.setDate(next.getDate() + weekdayOffset * -1)
86 changes: 86 additions & 0 deletions src/utils/scheduling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,89 @@ export const ORDINAL_OPTIONS_GROUP: readonly OrdinalsGroup[] = Object.freeze([
items: [{ label: 'Last', alt: 'Last', value: 'last' }]
}
])

export function nextHourlySchedule(timestamp: Date, hours: number) {
const hourMillis = 3_600_000 * hours
const currentMillis = Date.now()
const timestampMillis = timestamp.getTime()

if (timestampMillis >= currentMillis) return timestamp

const elapsed = currentMillis - timestampMillis
const elapsedHoursFactor = elapsed / hourMillis
const nextHourFactor = Math.ceil(elapsedHoursFactor)
const nextHourMillis = hourMillis * nextHourFactor
const nextMillis = timestampMillis + nextHourMillis

return new Date(nextMillis)
}

export function nextDailySchedule(timestamp: Date, days: number) {
const dayMillis = 86_400_000 * days
const currentMillis = Date.now()
const timestampMillis = timestamp.getTime()

if (timestampMillis >= currentMillis) return timestamp

const elapsed = currentMillis - timestampMillis
const elapsedDaysFactor = elapsed / dayMillis
const nextDayFactor = Math.ceil(elapsedDaysFactor)
const nextDayMillis = dayMillis * nextDayFactor
const nextMillis = timestampMillis + nextDayMillis

return new Date(nextMillis)
}

// function nextDailySchedule(timestamp: Date, days: number) {
// const DAY_MS = 86_400_000 * days
// const currentMillis = Date.now()
// const timestampMillis = timestamp.getTime()

// if (timestampMillis >= currentMillis) return timestamp

// const elapsed = currentMillis - timestampMillis
// const elapsedDays = elapsed / DAY_MS
// const nextDay = Math.sign(days) === -1 ? Math.floor(elapsedDays) : Math.ceil(elapsedDays)
// const nextDayMillis = Math.abs(nextDay) * DAY_MS
// const nextMillis = timestampMillis + nextDayMillis

// return new Date(nextMillis)
// }

export function nextWeeklySchedule(timestamp: Date, weeks: number) {
const weekMillis = 604_800_000 * weeks
const currentMillis = Date.now()
const timestampMillis = timestamp.getTime()

if (timestampMillis >= currentMillis) return timestamp

const elapsed = currentMillis - timestampMillis
const elapsedWeeksFactor = elapsed / weekMillis
const nextWeekFactor = Math.ceil(elapsedWeeksFactor)
const nextWeekMillis = weekMillis * nextWeekFactor
const nextMillis = timestampMillis + nextWeekMillis

return new Date(nextMillis)
}

export function nextWeeklyScheduleCorrectionFactor(nextWeeklyTimestamp: Date, weekday: number) {
// `nextWeeklySchedule` add `x` weeks to the originating timestamp.
//
// `let x = 1` [1wk; 604800000ms] such that:
//
// on Thu 24, Oct 2024, `nextWeeklySchedule("2024-10-24")` returns `"2024-10-31"`
// which is a Thu 31, Oct 2024.
//
// But the user wants the event repeating every 1 week on a Tuesday (weekday = 2);
// So we subtract Tuesday (weekday = 2) from Thursday (weekday = 4) to compute our
// `weekdayOffset`.
//
// We go ahead and clone `nextWeeklyTimestamp`, setting date to be 31 + 2 * -1
// which gives `29` and that's a Tuesday one week away from the past week of 24th
const weekdayOffset = nextWeeklyTimestamp.getUTCDay() - weekday
const newWeeklyTimestamp = new Date(nextWeeklyTimestamp.getTime())

newWeeklyTimestamp.setDate(newWeeklyTimestamp.getDate() + weekdayOffset * -1)

return newWeeklyTimestamp.getTime() / nextWeeklyTimestamp.getTime()
}

0 comments on commit 84dddda

Please sign in to comment.