This repository was archived by the owner on Sep 19, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdateranges.js
More file actions
258 lines (218 loc) · 5.56 KB
/
Copy pathdateranges.js
File metadata and controls
258 lines (218 loc) · 5.56 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* eslint-disable no-restricted-syntax */
/* eslint-disable radix */
/* eslint-disable prefer-template */
/* eslint-disable no-restricted-globals */
/* eslint-disable no-param-reassign */
/**
* TODO:
* most if not all of the functions in this file should be refactored to use the
* luxon datetime library. current homegrown date handling functions are buggy
* and not accurate.
*/
/**
* Take one of the many schemas of a date and always output a valid
* Date object or null.
*/
export const extractDate = (dateObj) => {
if (dateObj instanceof Date) {
return dateObj
}
if (!dateObj || !dateObj.month || !dateObj.day || !dateObj.year) {
return null
}
var d = new Date(`${dateObj.month}/${dateObj.day}/${dateObj.year}`)
d.setFullYear(dateObj.year) // addresses an issue where two digit years are assumed to be in 20th or 21st centry, ex: 99 -> 1999 01 -> 2001
return d
}
/**
* Do some fancy decimal rounding allowing for different types:
* - round
* - floor
* - ceil
*
* This was pulled from Mozilla Developer Network:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil
*/
export const decimalAdjust = (type, value, exp) => {
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value)
}
value = +value
exp = +exp
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN
}
// Shift
value = value.toString().split('e')
value = Math[type](+(value[0] + 'e' + (value[1] ? +value[1] - exp : -exp)))
// Shift back
value = value.toString().split('e')
return +(value[0] + 'e' + (value[1] ? +value[1] + exp : exp))
}
/**
* Sort an array of date ranges which have the structure
*
* {
* to: [date],
* from: [date]
* }
*/
export const rangeSorter = (a, b) => {
const af = extractDate(a.from)
const bf = extractDate(b.from)
if (af < bf) {
return -1
}
if (af > bf) {
return 1
}
return 0
}
/**
* Calculate a date in the past
*/
export const daysAgo = (from, days) => (
new Date(from - 1000 * 60 * 60 * 24 * days)
)
/**
* Convert date to UTC
*/
export const utc = (date) => {
if (!date) {
return null
}
if (Object.prototype.toString.call(date) === '[object String]') {
return null
}
return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()))
}
export const now = new Date(new Date().toUTCString())
export const today = utc(now)
export const ten = daysAgo(today, 365 * 10)
/**
* Get the Julian date
*/
export const julian = (date) => {
if (!date) {
return null
}
return (+utc(date) / 86400000 + 2440587.5).toFixed(6)
}
/**
* Find the percentage/position within a date range a particular value has.
*/
export const findPercentage = (max, min, value) => {
let largest = max
let smallest = min
if (min > largest) {
largest = min
smallest = max
}
const pos = ((value - smallest) / (largest - smallest)) * 100
if (pos < 0) {
return 0
}
if (pos > 100) {
return 100
}
return decimalAdjust('round', pos, -2)
}
/**
* Determine how many days are between dates
*/
export const daysBetween = (from, to) => {
if (!from || !to) {
return 0
}
const diff = Math.abs(to.getTime() - from.getTime())
return Math.ceil(diff / (1000 * 3600 * 24))
}
/**
* Determine if a specified year is considered a leap year
*/
export const leapYear = year => (
(year % 4 === 0 && year % 100 !== 0) || year % 400 === 0
)
/**
* Returns how many days are in a month based on the given year
*/
export const daysInMonth = (month, year) => {
const max = 31
const m = parseInt(month || 1) || 1
const y = parseInt(year || 0) || 0
// Bound check on the month
if (m < 1 || m > 12) {
return max
}
// Setup for upperbounds of days in months
const upperBounds = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (y > 0 && leapYear(y)) {
upperBounds[1] = 29
}
return Math.min(max, upperBounds[m - 1])
}
/**
* Determine if a date is valid with leap years considered
*/
export const validDate = (date) => {
if (!date) {
return false
}
const { month, day, year } = date
if (isNaN(month) || isNaN(day) || isNaN(year)) {
return false
}
const m = parseInt(month || 0)
const d = parseInt(day || 0)
const y = parseInt(year || -1)
return (
y >= 0
&& y < 10000
&& (m > 0 && m < 13)
&& (d > 0 && d <= daysInMonth(m, y))
)
}
/**
* Find the gaps in the timeline
*/
export const gaps = (ranges = [], start = ten, buffer = 30) => {
// If any of the ranges covers the entire timeline then return no gaps
for (const range of ranges) {
if (
daysAgo(range.from, -1 * buffer) <= start
&& range.to >= daysAgo(today, buffer)
) {
return []
}
}
const holes = []
const fullStop = today
const length = ranges.length - 1
ranges.sort(rangeSorter).forEach((range, i) => {
if (!range.from || !range.to) {
return
}
// Finds the gaps from the past to the present
const stop = range.from
if (stop > start && daysBetween(start, stop) > buffer) {
holes.push({ from: start, to: range.from })
}
// Set the next start position
start = range.to
// If this is the last date range check for gaps in the future
if (
i === length
&& start < fullStop
&& daysBetween(start, fullStop) > buffer
) {
holes.push({ from: range.to, to: fullStop })
}
})
return holes
}
/**
* Common dates used in calculations
*/
export const julianNow = julian(today)