generate-ical 1.0.6 → 2.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/README.md CHANGED
@@ -1,16 +1,22 @@
1
1
  # generate-ical
2
2
 
3
- This is a lightweight and typed utility to generate iCal files. With minimal dependencies and minimal code and complexity, this can be used almost everywhere.
3
+ A lightweight, typed utility for generating iCal files and calendar URLs. With minimal dependencies and minimal code complexity, this can be used almost everywhere.
4
4
 
5
- It doesn't depend on the implementation, e.g Express, Koa, file system, etc. This will just generate the file content as a string for you to do whatever you want with.
5
+ It doesn't depend on any specific implementation (e.g., Express, Koa, file system, etc.). It simply generates the file content as a string for you to use however you want.
6
6
 
7
7
  ## Install
8
8
 
9
9
  `npm install generate-ical`
10
10
 
11
- ## Usage
11
+ ## Functions
12
+
13
+ ### `generateIcal`
14
+
15
+ Generates an iCal file content as a string.
12
16
 
13
17
  ```typescript
18
+ import { generateIcal } from "generate-ical"
19
+
14
20
  const fileContent = generateIcal({
15
21
  title: "My First Event",
16
22
  description: {
@@ -26,18 +32,86 @@ const fileContent = generateIcal({
26
32
  })
27
33
  ```
28
34
 
35
+ ### `generateGoogleUrl`
36
+
37
+ Generates a Google Calendar URL for adding an event.
38
+
39
+ ```typescript
40
+ import { generateGoogleUrl } from "generate-ical"
41
+
42
+ const googleUrl = generateGoogleUrl({
43
+ title: "My First Event",
44
+ description: {
45
+ plain: "My event description",
46
+ },
47
+ isAllDay: false,
48
+ startDate: new Date("2023-01-01T18:00:00.000Z"),
49
+ endDate: new Date("2023-01-01T22:00:00.000Z"),
50
+ location: { title: "My place" },
51
+ url: "https://optional-video-call.com",
52
+ })
53
+ ```
54
+
55
+ ### `generateOffice365Url`
56
+
57
+ Generates an Office 365 Calendar URL for adding an event.
58
+
59
+ ```typescript
60
+ import { generateOffice365Url } from "generate-ical"
61
+
62
+ const office365Url = generateOffice365Url({
63
+ title: "My First Event",
64
+ description: {
65
+ plain: "My event description",
66
+ },
67
+ isAllDay: false,
68
+ startDate: new Date("2023-01-01T18:00:00.000Z"),
69
+ endDate: new Date("2023-01-01T22:00:00.000Z"),
70
+ location: { title: "My place" },
71
+ url: "https://optional-video-call.com",
72
+ })
73
+ ```
74
+
29
75
  ## Examples
30
76
 
31
77
  ### File System
32
78
 
33
79
  ```typescript
80
+ import { generateIcal } from "generate-ical"
81
+ import fs from "fs"
82
+
83
+ const fileContent = generateIcal({
84
+ /* ... */
85
+ })
34
86
  fs.writeFileSync("./Event.ics", fileContent)
35
87
  ```
36
88
 
37
89
  ### Koa
38
90
 
39
91
  ```typescript
92
+ import { generateIcal } from "generate-ical"
93
+
40
94
  context.response.set("Content-Type", "text/calendar")
41
95
  context.response.set("Content-Disposition", `attachment; filename="Event.ics"`)
42
- context.body = fileContent
96
+ context.body = generateIcal({
97
+ /* ... */
98
+ })
99
+ ```
100
+
101
+ ### Redirect to Calendar
102
+
103
+ ```typescript
104
+ import { generateGoogleUrl, generateOffice365Url } from "generate-ical"
105
+
106
+ const event = {
107
+ /* ... */
108
+ }
109
+
110
+ router.get("/add-to-google", (req, res) => {
111
+ res.redirect(generateGoogleUrl(event))
112
+ })
113
+
114
+ router.get("/add-to-office365", (req, res) => {
115
+ res.redirect(generateOffice365Url(event))
116
+ })
43
117
  ```
@@ -0,0 +1,51 @@
1
+ type Event = {
2
+ startDate: Date;
3
+ title: string;
4
+ description?: {
5
+ plain: string;
6
+ html?: string;
7
+ };
8
+ location?: {
9
+ title: string;
10
+ address?: string;
11
+ geo?: {
12
+ lat: number;
13
+ lon: number;
14
+ };
15
+ };
16
+ organizer?: {
17
+ name: string;
18
+ mailTo?: string;
19
+ sentBy?: string;
20
+ };
21
+ url?: string;
22
+ } & ({
23
+ isAllDay: true;
24
+ endDate?: Date;
25
+ } | {
26
+ isAllDay: false;
27
+ endDate: Date;
28
+ });
29
+
30
+ /**
31
+ *
32
+ * @param data - The event data to generate the iCal file for.
33
+ * @returns The iCal file content as a string.
34
+ */
35
+ declare function generateIcal(data: Event): string;
36
+
37
+ /**
38
+ *
39
+ * @param data - The event data to generate the Google URL for.
40
+ * @returns The Google URL as a string.
41
+ */
42
+ declare function generateGoogleUrl(data: Event): string;
43
+
44
+ /**
45
+ *
46
+ * @param data - The event data to generate the Office 365 URL for.
47
+ * @returns The Office 365 URL as a string.
48
+ */
49
+ declare function generateOffice365Url(data: Event): string;
50
+
51
+ export { generateGoogleUrl, generateIcal, generateOffice365Url };
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ import U from"uuid-random";function D(t){let o=U(),n="";function e(r){n+=`${r}\r
2
+ `}if(e("BEGIN:VCALENDAR"),e("VERSION:2.0"),e("PRODID://Hadermite//ical-generator//EN"),e("CALSCALE:GREGORIAN"),e("BEGIN:VEVENT"),e(`UID:${o}`),e("SEQUENCE:0"),e(`DTSTAMP:${g(new Date)}`),e(`SUMMARY:${s(t.title)}`),t.description!==void 0){let{plain:r,html:i}=t.description;e(`DESCRIPTION:${r}`),i!==void 0&&e(`X-ALT-DESC;FMTTYPE=text/html:${s(i)}`)}if(t.isAllDay?(e(`DTSTART;VALUE=DATE:${c(u(t.startDate))}`),t.endDate?e(`DTEND;VALUE=DATE:${c(u(t.endDate))}`):e(`DTEND;VALUE=DATE:${c(u(t.startDate))}`)):(e(`DTSTART:${g(t.startDate)}`),e(`DTEND:${g(t.endDate)}`)),t.location!==void 0){let r=[t.location.title];if(t.location.address!==void 0&&r.push(s(t.location.address)),e(`LOCATION:${s(r.join("\\, "))}`),t.location.geo!==void 0){let{lat:i,lon:l}=t.location.geo;e(`GEO:${s(i.toString())};${s(l.toString())}`)}}if(t.organizer!==void 0){let{name:r,sentBy:i,mailTo:l}=t.organizer,a=`ORGANIZER;CN=${f(r)}`;i!==void 0&&(a+=`;SENT-BY="mailto:${f(i)}"`),l!==void 0&&(a+=`:mailto:${s(l)}`),e(a)}return t.url!==void 0&&e(`URL;VALUE=URI:${s(t.url)}`),e("END:VEVENT"),e("END:VCALENDAR"),n}function s(t){return t.replace(/[\\;,]/g,o=>"\\"+o).replace(/(?:\r\n|\r|\n)/g,"\\n")}function f(t){return t.replace(/[\\;,"]/g,o=>"\\"+o).replace(/(?:\r\n|\r|\n)/g,"\\n")}function c(t){let o=t.getUTCFullYear().toString(),n=`${t.getUTCMonth()+1}`.padStart(2,"0"),e=`${t.getUTCDate()}`.padStart(2,"0");return`${o}${n}${e}`}function g(t){let o=`${t.getUTCHours()}`.padStart(2,"0"),n=`${t.getUTCMinutes()}`.padStart(2,"0"),e=`${t.getUTCSeconds()}`.padStart(2,"0");return`${c(t)}T${o}${n}${e}Z`}function u(t){return new Date(Date.UTC(t.getUTCFullYear(),t.getUTCMonth(),t.getUTCDate(),0,0,0,0))}function $(t){let o=[t.description?.plain,t.url].filter(r=>r!==void 0),n=[];return t.location&&(n.push(t.location.title),t.location.address&&n.push(t.location.address),t.location.geo&&n.push(`(${t.location.geo.lat}, ${t.location.geo.lon})`)),`https://www.google.com/calendar/render?${new URLSearchParams({action:"TEMPLATE",text:t.title.replace(/[^\w\s]/g,""),details:o.join(`
3
+
4
+ `),dates:t.isAllDay?`${T(t.startDate)}/${T(t.endDate??t.startDate)}`:`${p(t.startDate)}/${p(t.endDate)}`,...n.length>0&&{location:n.join(", ")}})}`}function p(t){let o=t.getUTCFullYear().toString().padStart(4,"0"),n=(t.getUTCMonth()+1).toString().padStart(2,"0"),e=t.getUTCDate().toString().padStart(2,"0"),r=t.getUTCHours().toString().padStart(2,"0"),i=t.getUTCMinutes().toString().padStart(2,"0"),l=t.getUTCSeconds().toString().padStart(2,"0");return`${o}${n}${e}T${r}${i}${l}Z`}function T(t){let o=t.getUTCFullYear().toString().padStart(4,"0"),n=(t.getUTCMonth()+1).toString().padStart(2,"0"),e=t.getUTCDate().toString().padStart(2,"0");return`${o}${n}${e}`}function E(t){let o=[t.description?.plain,t.url].filter(r=>r!==void 0),n=[];return t.location&&(n.push(t.location.title),t.location.address&&n.push(t.location.address),t.location.geo&&n.push(`(${t.location.geo.lat}, ${t.location.geo.lon})`)),`https://outlook.office.com/calendar/0/deeplink/compose?${new URLSearchParams({rru:"addevent",subject:t.title.replace(/[^\w\s]/g,""),body:o.join(`
5
+
6
+ `),startdt:t.isAllDay?m(t.startDate):S(t.startDate),enddt:t.isAllDay?m(t.endDate??t.startDate):S(t.endDate),...n.length>0&&{location:n.join(", ")}})}`}function S(t){return t.toISOString()}function m(t){return t.toISOString().split("T")[0]}export{$ as generateGoogleUrl,D as generateIcal,E as generateOffice365Url};
package/package.json CHANGED
@@ -1,29 +1,31 @@
1
1
  {
2
- "name": "generate-ical",
3
- "version": "1.0.6",
4
- "author": {
5
- "name": "Lukas Wiklund",
6
- "email": "lukas@wiklund.se",
7
- "url": "https://github.com/lukaswiklund"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/lukaswiklund/generate-ical.git"
12
- },
13
- "files": [
14
- "./dist/**/*"
15
- ],
16
- "main": "./dist/index.js",
17
- "scripts": {
18
- "build": "tsc",
19
- "format": "prettier --write .",
20
- "deploy": "npm i && npm run format && npm run build && npm publish"
21
- },
22
- "dependencies": {
23
- "uuid-random": "^1.3.2"
24
- },
25
- "devDependencies": {
26
- "prettier": "^3.5.3",
27
- "typescript": "^5.8.2"
28
- }
29
- }
2
+ "name": "generate-ical",
3
+ "type": "module",
4
+ "version": "2.0.0",
5
+ "author": {
6
+ "name": "Lukas Wiklund",
7
+ "email": "lukas@wiklund.se",
8
+ "url": "https://github.com/lukaswiklund"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/lukaswiklund/generate-ical.git"
13
+ },
14
+ "files": [
15
+ "./dist/**/*"
16
+ ],
17
+ "main": "./dist/index.js",
18
+ "dependencies": {
19
+ "uuid-random": "^1.3.2"
20
+ },
21
+ "devDependencies": {
22
+ "prettier": "^3.7.4",
23
+ "tsup": "^8.5.1",
24
+ "typescript": "^5.9.3"
25
+ },
26
+ "scripts": {
27
+ "build": "tsup",
28
+ "format": "prettier --write .",
29
+ "deploy": "corepack pnpm i && corepack pnpm run format && corepack pnpm run build && corepack pnpm publish"
30
+ }
31
+ }