-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a467d4
commit 5c9fa7a
Showing
15 changed files
with
601 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
|
||
var _v = require('uuid/v1'); | ||
|
||
var _v2 = _interopRequireDefault(_v); | ||
|
||
var _moment = require('moment'); | ||
|
||
var _moment2 = _interopRequireDefault(_moment); | ||
|
||
var _utils = require('./utils'); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
var now = (0, _moment2.default)().utc(); | ||
|
||
var defaults = { | ||
title: 'Untitled event', | ||
productId: 'adamgibbons/ics', | ||
uid: (0, _v2.default)(), | ||
timestamp: (0, _utils.setDateWithUTCtime)([now.get('year'), now.get('month') + 1, now.get('date'), now.get('hours'), now.get('minutes'), now.get('seconds')]), | ||
start: (0, _utils.setDateWithUTCtime)(), | ||
duration: { | ||
hours: 1 | ||
} | ||
}; | ||
|
||
exports.default = defaults; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.createEvent = createEvent; | ||
|
||
var _lodash = require('lodash'); | ||
|
||
var _lodash2 = _interopRequireDefault(_lodash); | ||
|
||
var _pipeline = require('./pipeline'); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
function createEvent(attributes, cb) { | ||
if (!attributes) { | ||
Error('attributes argument is required'); | ||
} | ||
|
||
if (!cb) { | ||
// No callback, so return error or value in an object | ||
var _validateEvent = (0, _pipeline.validateEvent)((0, _pipeline.buildEvent)(attributes)), | ||
_error = _validateEvent.error, | ||
_value = _validateEvent.value; | ||
|
||
if (_error) return { error: _error, value: _value }; | ||
|
||
var event = ''; | ||
|
||
try { | ||
event = (0, _pipeline.formatEvent)(_value); | ||
} catch (error) { | ||
return { error: error, value: null }; | ||
} | ||
|
||
return { error: null, value: event }; | ||
} | ||
|
||
// Return a node-style callback | ||
|
||
var _validateEvent2 = (0, _pipeline.validateEvent)((0, _pipeline.buildEvent)(attributes)), | ||
error = _validateEvent2.error, | ||
value = _validateEvent2.value; | ||
|
||
if (error) return cb(error); | ||
|
||
return cb(null, (0, _pipeline.formatEvent)(value)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = buildEvent; | ||
|
||
var _lodash = require('lodash'); | ||
|
||
var _lodash2 = _interopRequireDefault(_lodash); | ||
|
||
var _defaults = require('../defaults'); | ||
|
||
var _defaults2 = _interopRequireDefault(_defaults); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
function buildEvent() { | ||
var attributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var title = attributes.title, | ||
productId = attributes.productId, | ||
uid = attributes.uid, | ||
start = attributes.start, | ||
startType = attributes.startType, | ||
duration = attributes.duration, | ||
end = attributes.end, | ||
description = attributes.description, | ||
url = attributes.url, | ||
geo = attributes.geo, | ||
location = attributes.location, | ||
status = attributes.status, | ||
categories = attributes.categories, | ||
organizer = attributes.organizer, | ||
attendees = attributes.attendees, | ||
alarms = attributes.alarms; | ||
|
||
// fill in default values where necessary | ||
|
||
var output = Object.assign({}, _defaults2.default, attributes); | ||
|
||
// remove falsey values | ||
var cleanOutput = _lodash2.default.pickBy(output, _lodash2.default.identity); | ||
|
||
return cleanOutput; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = formatEvent; | ||
|
||
var _utils = require('../utils'); | ||
|
||
function formatEvent() { | ||
var attributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var title = attributes.title, | ||
productId = attributes.productId, | ||
uid = attributes.uid, | ||
timestamp = attributes.timestamp, | ||
start = attributes.start, | ||
startType = attributes.startType, | ||
duration = attributes.duration, | ||
end = attributes.end, | ||
description = attributes.description, | ||
url = attributes.url, | ||
geo = attributes.geo, | ||
location = attributes.location, | ||
status = attributes.status, | ||
categories = attributes.categories, | ||
organizer = attributes.organizer, | ||
attendees = attributes.attendees, | ||
alarms = attributes.alarms; | ||
|
||
|
||
var icsFormat = ''; | ||
icsFormat += 'BEGIN:VCALENDAR\r\n'; | ||
icsFormat += 'VERSION:2.0\r\n'; | ||
icsFormat += 'CALSCALE:GREGORIAN\r\n'; | ||
icsFormat += 'PRODID:' + productId + '\r\n'; | ||
icsFormat += 'BEGIN:VEVENT\r\n'; | ||
icsFormat += 'UID:' + uid + '\r\n'; | ||
icsFormat += 'SUMMARY:' + title + '\r\n'; | ||
icsFormat += 'DTSTAMP:' + timestamp + '\r\n'; | ||
icsFormat += 'DTSTART:' + (0, _utils.setDate)(start, startType) + '\r\n'; | ||
icsFormat += end ? 'DTEND:' + (0, _utils.setDate)(end, startType) + '\r\n' : ''; | ||
icsFormat += description ? 'DESCRIPTION:' + description + '\r\n' : ''; | ||
icsFormat += url ? 'URL:' + url + '\r\n' : ''; | ||
icsFormat += geo ? 'GEO:' + (0, _utils.setGeolocation)(geo) + '\r\n' : ''; | ||
icsFormat += location ? 'LOCATION:' + location + '\r\n' : ''; | ||
icsFormat += status ? 'STATUS:' + status + '\r\n' : ''; | ||
icsFormat += categories ? 'CATEGORIES:' + categories + '\r\n' : ''; | ||
icsFormat += organizer ? 'ORGANIZER;' + (0, _utils.setContact)(organizer) + '\r\n' : ''; | ||
if (attendees) { | ||
attendees.map(function (attendee) { | ||
icsFormat += 'ATTENDEE;' + (0, _utils.setContact)(attendee) + '\r\n'; | ||
}); | ||
} | ||
if (alarms) { | ||
alarms.map(function (alarm) { | ||
icsFormat += (0, _utils.setAlarm)(alarm); | ||
}); | ||
} | ||
icsFormat += duration ? 'DURATION:' + (0, _utils.formatDuration)(duration) + '\r\n' : ''; | ||
icsFormat += 'END:VEVENT\r\n'; | ||
icsFormat += 'END:VCALENDAR\r\n'; | ||
|
||
return icsFormat; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.validateEvent = exports.formatEvent = exports.buildEvent = undefined; | ||
|
||
var _build = require('./build'); | ||
|
||
var _build2 = _interopRequireDefault(_build); | ||
|
||
var _format = require('./format'); | ||
|
||
var _format2 = _interopRequireDefault(_format); | ||
|
||
var _validate = require('./validate'); | ||
|
||
var _validate2 = _interopRequireDefault(_validate); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
exports.buildEvent = _build2.default; | ||
exports.formatEvent = _format2.default; | ||
exports.validateEvent = _validate2.default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
|
||
var _schema = require('../schema'); | ||
|
||
var _schema2 = _interopRequireDefault(_schema); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
exports.default = _schema2.default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = validateEvent; | ||
|
||
var _joi = require('joi'); | ||
|
||
var _joi2 = _interopRequireDefault(_joi); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
var dateTimeSchema = _joi2.default.array().min(5).max(7).ordered(_joi2.default.number().integer(), _joi2.default.number().integer().min(1).max(12), _joi2.default.number().integer().min(1).max(31), _joi2.default.number().integer().min(0).max(23), _joi2.default.number().integer().min(0).max(60), _joi2.default.number().integer().min(0).max(60)); | ||
|
||
var durationSchema = _joi2.default.object().keys({ | ||
weeks: _joi2.default.number(), | ||
days: _joi2.default.number(), | ||
hours: _joi2.default.number(), | ||
minutes: _joi2.default.number(), | ||
seconds: _joi2.default.number() | ||
}); | ||
|
||
var contactSchema = _joi2.default.object().keys({ | ||
name: _joi2.default.string(), | ||
email: _joi2.default.string().email() | ||
}); | ||
|
||
var alarmSchema = _joi2.default.object().keys({ | ||
action: _joi2.default.string().regex(/audio|display|email/).required(), | ||
trigger: _joi2.default.array().required(), | ||
description: _joi2.default.string(), | ||
duration: durationSchema, | ||
repeat: _joi2.default.number(), | ||
attach: _joi2.default.string().uri(), | ||
summary: _joi2.default.string(), | ||
attendee: contactSchema, | ||
'x-prop': _joi2.default.any(), | ||
'iana-prop': _joi2.default.any() | ||
}); | ||
|
||
var schema = _joi2.default.object().keys({ | ||
timestamp: _joi2.default.any(), | ||
title: _joi2.default.string(), | ||
productId: _joi2.default.string(), | ||
uid: _joi2.default.string().required(), | ||
start: dateTimeSchema.required(), | ||
duration: durationSchema, | ||
startType: _joi2.default.string(), | ||
end: dateTimeSchema, | ||
description: _joi2.default.string(), | ||
url: _joi2.default.string().uri(), | ||
geo: _joi2.default.object().keys({ lat: _joi2.default.number(), lon: _joi2.default.number() }), | ||
location: _joi2.default.string(), | ||
status: _joi2.default.string().regex(/TENTATIVE|CANCELLED|CONFIRMED/), | ||
categories: _joi2.default.array().items(_joi2.default.string()), | ||
organizer: contactSchema, | ||
attendees: _joi2.default.array().items(contactSchema), | ||
alarms: _joi2.default.array().items(alarmSchema) | ||
}); | ||
|
||
function validateEvent(candidate) { | ||
var _Joi$validate = _joi2.default.validate(candidate, schema), | ||
error = _Joi$validate.error, | ||
value = _Joi$validate.value; | ||
|
||
return { error: error, value: value }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = formatDuration; | ||
function formatDuration() { | ||
var attributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var weeks = attributes.weeks, | ||
days = attributes.days, | ||
hours = attributes.hours, | ||
minutes = attributes.minutes, | ||
seconds = attributes.seconds; | ||
|
||
|
||
var formattedDuration = 'P'; | ||
formattedDuration += weeks ? weeks + 'W' : ''; | ||
formattedDuration += days ? days + 'D' : ''; | ||
formattedDuration += 'T'; | ||
formattedDuration += hours ? hours + 'H' : ''; | ||
formattedDuration += minutes ? minutes + 'M' : ''; | ||
formattedDuration += seconds ? seconds + 'S' : ''; | ||
|
||
return formattedDuration; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.formatDuration = exports.setAlarm = exports.setContact = exports.setGeolocation = exports.setDateWithLocalTime = exports.setDateWithUTCtime = exports.setDate = undefined; | ||
|
||
var _setDate = require('./set-date'); | ||
|
||
var _setDate2 = _interopRequireDefault(_setDate); | ||
|
||
var _setDateWithUtcTime = require('./set-date-with-utc-time'); | ||
|
||
var _setDateWithUtcTime2 = _interopRequireDefault(_setDateWithUtcTime); | ||
|
||
var _setDateWithLocalTime = require('./set-date-with-local-time'); | ||
|
||
var _setDateWithLocalTime2 = _interopRequireDefault(_setDateWithLocalTime); | ||
|
||
var _setGeolocation = require('./set-geolocation'); | ||
|
||
var _setGeolocation2 = _interopRequireDefault(_setGeolocation); | ||
|
||
var _setContact = require('./set-contact'); | ||
|
||
var _setContact2 = _interopRequireDefault(_setContact); | ||
|
||
var _setAlarm = require('./set-alarm'); | ||
|
||
var _setAlarm2 = _interopRequireDefault(_setAlarm); | ||
|
||
var _formatDuration = require('./format-duration'); | ||
|
||
var _formatDuration2 = _interopRequireDefault(_formatDuration); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
exports.setDate = _setDate2.default; | ||
exports.setDateWithUTCtime = _setDateWithUtcTime2.default; | ||
exports.setDateWithLocalTime = _setDateWithLocalTime2.default; | ||
exports.setGeolocation = _setGeolocation2.default; | ||
exports.setContact = _setContact2.default; | ||
exports.setAlarm = _setAlarm2.default; | ||
exports.formatDuration = _formatDuration2.default; |
Oops, something went wrong.