forked from adamgibbons/ics
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
22 changed files
with
826 additions
and
2 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 |
---|---|---|
|
@@ -2,4 +2,3 @@ node_modules | |
.DS_Store | ||
write-event.js | ||
output.ics | ||
dist/ |
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,30 @@ | ||
'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', | ||
method: 'PUBLISH', | ||
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)() | ||
}; | ||
|
||
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,124 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.createEvent = createEvent; | ||
exports.createEvents = createEvents; | ||
|
||
var _v = require('uuid/v4'); | ||
|
||
var _v2 = _interopRequireDefault(_v); | ||
|
||
var _pipeline = require('./pipeline'); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
function assignUniqueId(event) { | ||
event.uid = event.uid || (0, _v2.default)(); | ||
return (0, _pipeline.validateEvent)((0, _pipeline.buildEvent)(event)); | ||
} | ||
|
||
function applyInitialFormatting(_ref) { | ||
var error = _ref.error, | ||
value = _ref.value; | ||
|
||
if (error) { | ||
return { error: error, value: null }; | ||
} | ||
|
||
return { error: null, value: (0, _pipeline.formatEvent)(value) }; | ||
} | ||
|
||
function reformatEventsByPosition(_ref2, idx, list) { | ||
var error = _ref2.error, | ||
value = _ref2.value; | ||
|
||
if (error) return { error: error, value: 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, _ref3, idx) { | ||
var error = _ref3.error, | ||
value = _ref3.value; | ||
|
||
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; | ||
} | ||
|
||
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)); | ||
} | ||
|
||
function createEvents(events, cb) { | ||
if (!events) { | ||
return { error: Error('one argument is required'), value: null }; | ||
} | ||
|
||
if (events.length === 1) { | ||
return createEvent(events[0], cb); | ||
} | ||
|
||
var _events$map$map$map$r = events.map(assignUniqueId).map(applyInitialFormatting).map(reformatEventsByPosition).reduce(catenateEvents, { error: null, value: null }), | ||
error = _events$map$map$map$r.error, | ||
value = _events$map$map$map$r.value; | ||
|
||
if (!cb) { | ||
return { error: error, value: value }; | ||
} | ||
|
||
return cb(error, 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,46 @@ | ||
'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, | ||
method = attributes.method, | ||
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,87 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = formatEvent; | ||
|
||
var _utils = require('../utils'); | ||
|
||
var _lodash = require('lodash'); | ||
|
||
var _lodash2 = _interopRequireDefault(_lodash); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
function formatEvent() { | ||
var attributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var title = attributes.title, | ||
productId = attributes.productId, | ||
method = attributes.method, | ||
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 += (0, _utils.foldLine)('PRODID:' + productId) + '\r\n'; | ||
icsFormat += (0, _utils.foldLine)('METHOD:' + method) + '\r\n'; | ||
icsFormat += 'X-PUBLISHED-TTL:PT1H\r\n'; | ||
icsFormat += 'BEGIN:VEVENT\r\n'; | ||
icsFormat += 'UID:' + uid + '\r\n'; | ||
icsFormat += (0, _utils.foldLine)('SUMMARY:' + (title ? (0, _utils.setSummary)(title) : title)) + '\r\n'; | ||
icsFormat += 'DTSTAMP:' + timestamp + '\r\n'; | ||
|
||
// All day events like anniversaries must be specified as VALUE type DATE | ||
icsFormat += 'DTSTART' + (start && start.length == 3 ? ";VALUE=DATE" : "") + ':' + (0, _utils.setDate)(start, startType) + '\r\n'; | ||
|
||
// End is not required for all day events on single days (like anniversaries) | ||
if (!(_lodash2.default.isEqual(start, end) && end && end.length == 3)) { | ||
if (end && end.length == 3) { | ||
icsFormat += 'DTEND;VALUE=DATE:' + (0, _utils.setDate)(end, startType) + '\r\n'; | ||
} else if (end) { | ||
icsFormat += 'DTEND:' + (0, _utils.setDate)(end, startType) + '\r\n'; | ||
} | ||
} | ||
|
||
icsFormat += description ? (0, _utils.foldLine)('DESCRIPTION:' + (0, _utils.setDescription)(description)) + '\r\n' : ''; | ||
icsFormat += url ? (0, _utils.foldLine)('URL:' + url) + '\r\n' : ''; | ||
icsFormat += geo ? (0, _utils.foldLine)('GEO:' + (0, _utils.setGeolocation)(geo)) + '\r\n' : ''; | ||
icsFormat += location ? (0, _utils.foldLine)('LOCATION:' + location) + '\r\n' : ''; | ||
icsFormat += status ? (0, _utils.foldLine)('STATUS:' + status) + '\r\n' : ''; | ||
icsFormat += categories ? (0, _utils.foldLine)('CATEGORIES:' + categories) + '\r\n' : ''; | ||
icsFormat += organizer ? (0, _utils.foldLine)('ORGANIZER;' + (0, _utils.setOrganizer)(organizer)) + '\r\n' : ''; | ||
|
||
if (attendees) { | ||
attendees.map(function (attendee) { | ||
icsFormat += (0, _utils.foldLine)('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; |
Oops, something went wrong.