Skip to content

Commit

Permalink
Improve nextDay implementation (#2524)
Browse files Browse the repository at this point in the history
Changed: Improved `nextDay` performance by roughly 50%.
  • Loading branch information
fturmel committed Jul 23, 2021
1 parent d889bea commit 833cdfa
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/nextDay/index.ts
@@ -1,10 +1,7 @@
import requiredArgs from '../_lib/requiredArgs/index'
import getDay from '../getDay'
import addDays from '../addDays'
import toDate from '../toDate'
import getDay from '../getDay'
import { Day } from '../types'

const baseMap = [7, 6, 5, 4, 3, 2, 1]
import requiredArgs from '../_lib/requiredArgs/index'

/**
* @name nextDay
Expand All @@ -31,16 +28,9 @@ const baseMap = [7, 6, 5, 4, 3, 2, 1]
*/
export default function nextDay(date: Date | number, day: Day): Date {
requiredArgs(2, arguments)
const map = genMap(day)
return addDays(toDate(date), map[getDay(toDate(date))])
}

function genMap(daysToMove: number): number[] {
if (daysToMove === 0) {
return baseMap
} else {
const mapStart = baseMap.slice(-daysToMove)
const mapEnd = baseMap.slice(0, baseMap.length - daysToMove)
return mapStart.concat(mapEnd)
}
let delta = day - getDay(date)
if (delta <= 0) delta += 7

return addDays(date, delta)
}

0 comments on commit 833cdfa

Please sign in to comment.