forked from adamgibbons/ics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (105 loc) · 3.23 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { nanoid } from 'nanoid'
import {
buildEvent,
validateEvent,
formatEvent
} from './pipeline'
function assignUniqueId(event) {
event.uid = event.uid || nanoid()
return event
}
function validateAndBuildEvent(event) {
return validateEvent(buildEvent(event))
}
function applyInitialFormatting({ error, value }) {
if (error) {
return { error, value: null }
}
return { error: null, value: formatEvent(value) }
}
function reformatEventsByPosition({ error, value }, idx, list) {
if (error) return { error, value }
if (idx === 0) {
// beginning of list
return { value: value.slice(0, value.indexOf('END:VCALENDAR')), error: null }
}
if (idx === list.length - 1) {
// end of list
return { value: value.slice(value.indexOf('BEGIN:VEVENT')), error: null}
}
return { error: null, value: value.slice(value.indexOf('BEGIN:VEVENT'), value.indexOf('END:VEVENT') + 12) }
}
function catenateEvents(accumulator, { error, value }, idx) {
if (error) {
accumulator.error = error
accumulator.value = null
return accumulator
}
if (accumulator.value) {
accumulator.value = accumulator.value.concat(value)
return accumulator
}
accumulator.value = value
return accumulator
}
export function convertTimestampToArray(timestamp, inputType = 'local') {
const dateArray = [];
const d = new Date(timestamp);
dateArray.push(inputType === 'local' ? d.getFullYear() : d.getUTCFullYear());
dateArray.push((inputType === 'local' ? d.getMonth() : d.getUTCMonth()) + 1);
dateArray.push(inputType === 'local' ? d.getDate() : d.getUTCDate());
dateArray.push(inputType === 'local' ? d.getHours() : d.getUTCHours());
dateArray.push(inputType === 'local' ? d.getMinutes() : d.getUTCMinutes());
return dateArray;
}
export function createEvent (attributes, cb) {
if (!attributes) { Error('Attributes argument is required') }
assignUniqueId(attributes)
if (!cb) {
// No callback, so return error or value in an object
const { error, value } = validateAndBuildEvent(attributes)
if (error) return { error, value }
let event = ''
try {
event = formatEvent(value)
} catch(error) {
return { error, value: null }
}
return { error: null, value: event }
}
// Return a node-style callback
const { error, value } = validateAndBuildEvent(attributes)
if (error) return cb(error)
return cb(null, formatEvent(value))
}
export function createEvents (events, cb) {
if (!events) {
return { error: Error('one argument is required'), value: null }
}
if (events.length === 0) {
const {error, value: dummy} = createEvent({
start: [2000, 10, 5, 5, 0],
duration: { hours: 1 }
})
if (error) return {error, value: null}
return {
error: null,
value: (
dummy.slice(0, dummy.indexOf('BEGIN:VEVENT')) +
dummy.slice(dummy.indexOf('END:VEVENT') + 10 + 2)
)
}
}
if (events.length === 1) {
return createEvent(events[0], cb)
}
const { error, value } = events.map(assignUniqueId)
.map(validateAndBuildEvent)
.map(applyInitialFormatting)
.map(reformatEventsByPosition)
.reduce(catenateEvents, { error: null, value: null })
if (!cb) {
return { error, value }
}
return cb(error, value)
}