generate-ical 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.prettierrc ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "useTabs": true,
3
+ "tabWidth": 4,
4
+ "semi": false,
5
+ "printWidth": 120,
6
+ "arrowParens": "avoid"
7
+ }
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "generate-ical",
3
+ "version": "1.0.0",
4
+ "author": {
5
+ "name": "Lukas Wiklund",
6
+ "email": "lukas@wiklund.se",
7
+ "url": "https://github.com/Hadermite"
8
+ },
9
+ "main": "./dist/index.js",
10
+ "scripts": {
11
+ "build": "tsc"
12
+ },
13
+ "devDependencies": {
14
+ "typescript": "^4.9.3"
15
+ },
16
+ "dependencies": {
17
+ "uuid-random": "^1.3.2"
18
+ }
19
+ }
package/src/index.ts ADDED
@@ -0,0 +1,111 @@
1
+ import uuid from "uuid-random"
2
+ type Event = {
3
+ productId: string
4
+ startDate: Date
5
+ title: string
6
+ description?: { plain: string; html?: string }
7
+ location?: {
8
+ title: string
9
+ address?: string
10
+ geo?: {
11
+ lat: number
12
+ lon: number
13
+ }
14
+ }
15
+ organizer?: {
16
+ name: string
17
+ email?: string
18
+ mailTo?: string
19
+ sentBy?: string
20
+ }
21
+ url?: string
22
+ } & ({ isAllDay: true; endDate?: Date } | { isAllDay: false; endDate: Date })
23
+
24
+ function generate(data: Event): string {
25
+ const id = uuid()
26
+ let result = ""
27
+
28
+ function addRow(row: string): void {
29
+ result += `${row}\r\n`
30
+ }
31
+
32
+ addRow("BEGIN:VEVENT")
33
+ addRow(`UID:${id}`)
34
+ addRow(`SEQUENCE:0`)
35
+ addRow(`DTSTAMP:${formatDateTime(new Date())}`)
36
+ addRow(`SUMMARY:${escape(data.title)}`)
37
+
38
+ if (data.description !== undefined) {
39
+ const { plain, html } = data.description
40
+ addRow(`DESCRIPTION:${plain}`)
41
+
42
+ if (html !== undefined) {
43
+ addRow(`X-ALT-DESC;FMTTYPE=text/html:${escape(html)}`)
44
+ }
45
+ }
46
+
47
+ if (data.isAllDay) {
48
+ addRow(`DTSTART;VALUE=DATE:${formatDate(data.startDate)}`)
49
+ if (data.endDate !== undefined) {
50
+ addRow(`DTEND;VALUE=DATE:${formatDate(data.endDate)}`)
51
+ }
52
+ } else {
53
+ addRow(`DTSTART:${formatDateTime(data.startDate)}`)
54
+ addRow(`DTEND:${formatDateTime(data.endDate)}`)
55
+ }
56
+
57
+ if (data.location !== undefined) {
58
+ addRow(`LOCATION:${escape(data.location.title)}`)
59
+ if (data.location.address !== undefined) {
60
+ addRow(escape(data.location.address))
61
+ }
62
+
63
+ if (data.location.geo !== undefined) {
64
+ const { lat, lon } = data.location.geo
65
+ addRow(`GEO:${escape(lat.toString())};${escape(lon.toString())}`)
66
+ }
67
+ }
68
+
69
+ if (data.organizer !== undefined) {
70
+ const { name, sentBy, email, mailTo } = data.organizer
71
+ let string = `ORGANIZER;CN=${escapeInQuotes(name)}`
72
+ if (sentBy !== undefined) {
73
+ string += `;SENT-BY="mailto:${escapeInQuotes(sentBy)}"`
74
+ }
75
+ if (email !== undefined && mailTo !== undefined) {
76
+ string += `;EMAIL=${escape(email)}`
77
+ }
78
+ if (email !== undefined) {
79
+ string += `:mailto:${escape(mailTo ?? email)}`
80
+ }
81
+ addRow(string)
82
+ }
83
+
84
+ if (data.url !== undefined) {
85
+ addRow(`URL;VALUE=URI:${escape(data.url)}`)
86
+ }
87
+
88
+ return result
89
+ }
90
+
91
+ export function escape(string: string): string {
92
+ return string.replace(/[\\;,]/g, match => "\\" + match).replace(/(?:\r\n|\r|\n)/g, "\\n")
93
+ }
94
+
95
+ export function escapeInQuotes(string: string): string {
96
+ return string.replace(/[\\;,"]/g, match => "\\" + match).replace(/(?:\r\n|\r|\n)/g, "\\n")
97
+ }
98
+
99
+ function formatDate(date: Date): string {
100
+ const year = date.getUTCFullYear().toString()
101
+ const month = `${date.getUTCMonth() + 1}`.padStart(2, "0")
102
+ const day = `${date.getUTCDate()}`.padStart(2, "0")
103
+ return `${year}${month}${day}`
104
+ }
105
+
106
+ function formatDateTime(date: Date): string {
107
+ const hours = `${date.getUTCHours()}`.padStart(2, "0")
108
+ const minutes = `${date.getUTCMinutes()}`.padStart(2, "0")
109
+ const seconds = `${date.getUTCSeconds()}`.padStart(2, "0")
110
+ return `${formatDate(date)}T${hours}${minutes}${seconds}Z`
111
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2017",
4
+ "module": "None",
5
+ "moduleResolution": "Node",
6
+ "declaration": true,
7
+ "esModuleInterop": true,
8
+ "sourceMap": true,
9
+ "strict": true,
10
+ "outDir": "./dist"
11
+ },
12
+ "include": ["src"]
13
+ }