-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransforms.js
66 lines (54 loc) · 2.04 KB
/
transforms.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
const moment = require('moment');
const MS_PER_HOUR = 1000 * 60 * 60;
const shallowClone = (obj) =>
Object.assign({}, obj);
// Transform the given event to a simpler summary structure
const selectSummary = (ev) => ({
uid: ev.uid,
start: ev.start,
end: ev.end,
summary: ev.summary,
description: ev.description,
});
// Return a transformer that converts the given property to a MomentJS object
const dateToMoment = (field) => (ev) =>
Object.assign(shallowClone(ev), {
[field]: moment(ev[field]),
});
// Return a transformer that converts the given properties to a MomentJS object
const datesToMoments = (...fieldNames) => (ev) =>
fieldNames.reduce((acc, field) => dateToMoment(field)(acc), shallowClone(ev));
// Transform the given event to include a durationInHours property
const addDurationInHours = (ev) =>
Object.assign(shallowClone(ev), {
durationInHours: getDuration(ev) / MS_PER_HOUR,
});
const getDuration = (ev) =>
ev.end - ev.start;
// Return a transformer that converts the given date property to an ISO 8601 string
const dateToIso = (field) => (ev) =>
Object.assign(shallowClone(ev), {
[field]: ev[field].toISOString(),
});
// Return a transformer that converts the given date properties to an ISO 8601 string
const datesToIso = (...fieldNames) => (ev) =>
fieldNames.reduce((acc, field) => dateToIso(field)(acc), shallowClone(ev));
// Return a transformer that converts the given date property to the given format string
const dateToFormat = (format, field) => (ev) => {
return Object.assign(shallowClone(ev), {
[field]: moment(ev[field]).format(format),
});
}
// Return a transformer that converts the given date properties to the given format string
const datesToFormat = (format, ...fieldNames) => (ev) =>
fieldNames.reduce((acc, field) => dateToFormat(format, field)(acc), shallowClone(ev));
module.exports = {
selectSummary,
dateToMoment,
datesToMoments,
addDurationInHours,
dateToIso,
datesToIso,
dateToFormat,
datesToFormat,
};