This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
84 lines (73 loc) · 2.87 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { WeekStartOptions } from '../types.ts'
import addWeeks from '../addWeeks/index.ts'
import startOfWeek from '../startOfWeek/index.ts'
import toDate from '../toDate/index.ts'
import requiredArgs from '../_lib/requiredArgs/index.ts'
/**
* @name eachWeekOfInterval
* @category Interval Helpers
* @summary Return the array of weeks within the specified time interval.
*
* @description
* Return the array of weeks within the specified time interval.
*
* ### v2.0.0 breaking changes:
*
* - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
*
* @param {Interval} interval - the interval. See [Interval]{@link https://date-fns.org/docs/Interval}
* @param {Object} [options] - an object with options.
* @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
* @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
* @returns {Date[]} the array with starts of weeks from the week of the interval start to the week of the interval end
* @throws {TypeError} 1 argument required
* @throws {RangeError} `options.weekStartsOn` must be 0, 1, ..., 6
* @throws {RangeError} The start of an interval cannot be after its end
* @throws {RangeError} Date in interval cannot be `Invalid Date`
*
* @example
* // Each week within interval 6 October 2014 - 23 November 2014:
* var result = eachWeekOfInterval({
* start: new Date(2014, 9, 6),
* end: new Date(2014, 10, 23)
* })
* //=> [
* // Sun Oct 05 2014 00:00:00,
* // Sun Oct 12 2014 00:00:00,
* // Sun Oct 19 2014 00:00:00,
* // Sun Oct 26 2014 00:00:00,
* // Sun Nov 02 2014 00:00:00,
* // Sun Nov 09 2014 00:00:00,
* // Sun Nov 16 2014 00:00:00,
* // Sun Nov 23 2014 00:00:00
* // ]
*/
export default function eachWeekOfInterval(
dirtyInterval: Interval,
options?: WeekStartOptions
): Date[] {
requiredArgs(1, arguments)
const interval = dirtyInterval || {}
const startDate = toDate(interval.start)
const endDate = toDate(interval.end)
var endTime = endDate.getTime()
// Throw an exception if start date is after end date or if any date is `Invalid Date`
if (!(startDate.getTime() <= endTime)) {
throw new RangeError('Invalid interval')
}
const startDateWeek = startOfWeek(startDate, options)
const endDateWeek = startOfWeek(endDate, options)
// Some timezones switch DST at midnight, making start of day unreliable in these timezones, 3pm is a safe bet
startDateWeek.setHours(15)
endDateWeek.setHours(15)
endTime = endDateWeek.getTime()
const weeks = []
let currentWeek = startDateWeek
while (currentWeek.getTime() <= endTime) {
currentWeek.setHours(0)
weeks.push(toDate(currentWeek))
currentWeek = addWeeks(currentWeek, 1)
currentWeek.setHours(15)
}
return weeks
}