Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1 fix nonstandard property parsing #2

Merged
merged 6 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Parser/Properties/Property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ export default abstract class Property<T = string, P extends {[key: string]: str
public abstract readonly key: string;
public readonly value: T;
public readonly parameters: P;
public readonly isNonStandard: boolean;

constructor (value: T, parameters: P) {
constructor (value: T, parameters: P, isNonStandard: boolean = false) {
Copy link

@passionate-bram passionate-bram Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a short-hand notation for constructors when the argument name matches the target field name.

https://www.typescriptlang.org/docs/handbook/2/classes.html#parameter-properties

This constructor would be written as:

constructor (public readonly value: T, public readonly parameters: P, public readonly isNonStandard: boolean = false) {}

// Same but multiline for readability
constructor(
  public readonly value: T,
  public readonly parameters: P,
  public readonly isNonStandard: boolean = false
) {}

Some linters complain about the empty constructor body which you can solve with an /* empty on purpose */ comment or similar.

Also note that in this case if there is additional logic besides the fields you can still have regular code in the constructor, the body executes after field assignments.

Edit1: Extended example
Edit2: Linked to correct section in the handbook, same page but different heading

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@passionate-bram Nice, I didn't know about constructor parameter promotion in TypeScript yet. I'll update this soon. Thanks for your help!

this.value = value;
this.parameters = parameters;
this.isNonStandard = isNonStandard;
}

toString () : string {
Expand Down
8 changes: 8 additions & 0 deletions tests/__samples__/ics/non-standard-properties.ics
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BEGIN:VCALENDAR
VERSION:2.0
PRODID:blank_line_mid
COMMENT:This blank line is invalid
X-PREFIXED:Hello
IANA-PREFIXED:World
UNPREFIXED:This should work as well
END:VCALENDAR
4 changes: 3 additions & 1 deletion tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('Parse ICS to JSON', () => {
'forced_types.ics',
'google_birthday.ics',
'minimal.ics',
'non-standard-properties.ics',
'only_dtstart_date.ics',
'only_dtstart_time.ics',
'parserv2.ics',
Expand All @@ -29,11 +30,12 @@ describe('Parse ICS to JSON', () => {
const json = JSON.stringify(ics, (key, value) => {
if (value instanceof Property) {
// If we have no parameters we also don't export them to JSON
if (Object.keys(value.parameters).length > 0) {
if (Object.keys(value.parameters).length > 0 || value.isNonStandard) {
value = {
key: value.key,
__value__: value.value,
...value.parameters,
...value.isNonStandard && {__nonStandard__: true},
};
} else {
value = value.toString();
Expand Down