forked from target/goalert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
114 lines (94 loc) · 3.36 KB
/
util.js
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
import { DateTime } from 'luxon'
// calcNewActiveIndex returns the newActiveIndex for a swap operation
// -1 will be returned if there was no change
export function calcNewActiveIndex(oldActiveIndex, oldIndex, newIndex) {
if (oldIndex === newIndex) {
return -1
}
if (oldActiveIndex === oldIndex) {
return newIndex
}
if (oldIndex > oldActiveIndex && newIndex <= oldActiveIndex) {
return oldActiveIndex + 1
}
if (oldIndex < oldActiveIndex && newIndex >= oldActiveIndex) {
return oldActiveIndex - 1
}
return -1
}
// formatTime returns the formatted time with the timezone (if different than local timezone)
export function formatTime(time, tz) {
const schedTime = DateTime.fromISO(time, { zone: tz }).toLocaleString(
DateTime.TIME_SIMPLE,
)
const localTime = DateTime.fromISO(time).toLocaleString(DateTime.TIME_SIMPLE)
if (schedTime === localTime) {
return `${schedTime} ${tz}`
}
return `${schedTime} ${tz} (${localTime} local)`
}
// formatDay returns the day given a time and timezone
export function formatDay(time, tz) {
const day = DateTime.fromISO(time, { zone: tz }).weekdayLong
const localDay = DateTime.fromISO(time).weekdayLong
if (day === localDay) {
return `${day}`
}
return `${day} (${localDay})`
}
// formatWeeklySummary returns the summary for a weekly rotation
// taking into consideration extra formatting needed if timezone does not match with local timezone
export function formatWeeklySummary(shiftLength, start, tz) {
let details = ''
const day = DateTime.fromISO(start, { zone: tz }).weekdayLong
const schedTime = DateTime.fromISO(start, { zone: tz }).toLocaleString(
DateTime.TIME_SIMPLE,
)
const localDay = DateTime.fromISO(start).weekdayLong
const localTime = DateTime.fromISO(start).toLocaleString(DateTime.TIME_SIMPLE)
details += 'Hands off '
details += shiftLength === 1 ? 'weekly on' : `every ${shiftLength} weeks on`
details += ` ${day}` + ' at ' + schedTime + ' ' + tz
if (day !== localDay || schedTime !== localTime) {
details += ' (' + localDay + ' at ' + localTime + ' local time)'
}
details += '.'
return details
}
// handoffSummary returns the summary description for the rotation
export function handoffSummary(rotation) {
const tz = rotation.timeZone
if (!tz) return 'Loading handoff information...'
let details = ''
switch (rotation.type) {
case 'hourly':
details += 'First hand off time at ' + formatTime(rotation.start, tz)
details +=
', hands off every ' +
(rotation.shiftLength === 1
? 'hour'
: rotation.shiftLength + ' hours') +
'.'
break
case 'daily':
details += 'Hands off '
details +=
rotation.shiftLength === 1
? 'daily at'
: `every ${rotation.shiftLength} days at`
details += ' ' + formatTime(rotation.start, tz) + '.'
break
case 'weekly':
details += formatWeeklySummary(rotation.shiftLength, rotation.start, tz)
break
}
return details
}
// reorderList will move an item from the oldIndex to the newIndex, preserving order
// returning the result as a new array.
export function reorderList(_items, oldIndex, newIndex) {
const items = _items.slice()
items.splice(oldIndex, 1) // remove 1 element from oldIndex position
items.splice(newIndex, 0, _items[oldIndex]) // add dest to newIndex position
return items
}