Skip to content

Commit

Permalink
Merge pull request #131 from Neuvernetzung/feat#130
Browse files Browse the repository at this point in the history
If Version is last key of Calendar Object, leftover line breaks break…
  • Loading branch information
timheerwagen authored Dec 1, 2024
2 parents 48b4455 + 9f05bb4 commit 40c711f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-bobcats-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ts-ics": patch
---

If Version is last key of Calendar Object, leftover line breaks break validation #130
18 changes: 18 additions & 0 deletions packages/ts-ics/src/lib/parse/utils/splitLines.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,21 @@ it("Correctly handle LF line breaks - when importing as LF (Linux readFile)", as

expect(splitLines(formatted).join(LF_BREAK)).toEqual(unformatted);
});

it("Correct removal of leading and trailing line breaks - if there is more than one", async () => {
const formatted = icsTestData([
"",
"",
"",
"PRODID:ID",
"VERSION:2.0",
"",
"",
"",
]);

const lines = splitLines(formatted);

expect(lines[0]).not.toEqual("\n");
expect(lines[lines.length - 1].endsWith("\n")).toEqual(false);
});
13 changes: 10 additions & 3 deletions packages/ts-ics/src/lib/parse/utils/splitLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ export const splitLines = (str: string) => {

const rawLines = str.split(BREAK_REGEX);

// remove forgotten linebreaks infront and after
if (rawLines[0] === "") rawLines.shift();
if (rawLines[rawLines.length - 1] === "") rawLines.pop();
// remove forgotten leading linebreaks
while (rawLines[0] === "") rawLines.shift();

let endIndex = rawLines.length;

// remove forgotten trailing linebreaks #130
while (endIndex > 0 && rawLines[endIndex - 1] === "") {
endIndex -= 1;
rawLines.pop();
}

for (let i = 0; i < rawLines.length; ) {
let line = rawLines[i];
Expand Down
19 changes: 19 additions & 0 deletions packages/ts-ics/tests/parse/calendar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,22 @@ it("Parse Apple ICS Calendar", async () => {

expect(() => parseIcsCalendar(calendar)).not.toThrow();
});

it("Leftover line breaks should not affect parsing - #130", async () => {
const calendar = icsTestData([
"BEGIN:VCALENDAR",
"PRODID:ID",
"VERSION:2.0",
"BEGIN:VEVENT",
"CREATED:20240112T095511Z",
"DTEND:20240112T105511Z",
"DTSTAMP:20240112T095511Z",
"DTSTART:20240112T095511Z",
"SUMMARY:Test",
"UID:d908f270-64fa-4916-9f72-b48eb7222a64",
"END:VEVENT",
"END:VCALENDAR",
]);

expect(() => parseIcsCalendar(calendar)).not.toThrow();
});

0 comments on commit 40c711f

Please sign in to comment.