Skip to content

Commit

Permalink
forward customParser argument
Browse files Browse the repository at this point in the history
This fixes #4
  • Loading branch information
robert-virkus committed Sep 6, 2021
1 parent 21b011a commit 1dd1669
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/src/components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ abstract class VComponent {
/// The [text] can either contain `\r\n` (`CRLF`) or `\n` linebreaks, when both linebreak types are present in the [text], `CRLF` linebreaks are assumed.
/// Folded lines are unfolded automatically.
/// When you have a custom line delimiter, use [parseLines] instead.
/// Define the [customParser] if you want to support specific properties. Note that unknown properties will be made available with [getProperty], e.g. `final value = component.getProperty('X-NAME')?.textValue;`
static VComponent parse(String text,
{Property? Function(String name, String definition)? customParser}) {
final containsStandardCompliantLineBreaks = text.contains('\r\n');
Expand All @@ -173,13 +174,14 @@ abstract class VComponent {
if (lines.isEmpty) {
throw FormatException('Invalid input: [$text]');
}
return parseLines(lines);
return parseLines(lines, customParser: customParser);
}

/// Parses the component from the specified text [lines].
///
/// Compare [parse] for details.
static VComponent parseLines(List<String> lines) {
static VComponent parseLines(List<String> lines,
{Property? Function(String name, String definition)? customParser}) {
VComponent root = _createComponent(lines.first);
VComponent current = root;
for (var i = 1; i < lines.length; i++) {
Expand All @@ -199,7 +201,8 @@ abstract class VComponent {
current = parent;
}
} else {
final property = Property.parseProperty(line);
final property =
Property.parseProperty(line, customParser: customParser);
current.properties.add(property);
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/extension_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:enough_icalendar/enough_icalendar.dart';

void main() {
test('Calendar simple', () {
final text = '''BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
X-XXX-NUMBER:123
BEGIN:VEVENT
UID:[email protected]
DTSTAMP:19970610T172345Z
DTSTART:19970714T170000Z
DTEND:19970715T040000Z
SUMMARY:Bastille Day Party
END:VEVENT
END:VCALENDAR''';
// final calendar = VComponent.parse(text);
final calendar = VComponent.parse(text, customParser: (name, definition) {
if (name == 'X-XXX-NUMBER') {
return IntegerProperty(definition);
}
});
expect(calendar, isInstanceOf<VCalendar>());
expect(calendar.children, isNotEmpty);
expect(
calendar.getProperty('X-XXX-NUMBER'), isInstanceOf<IntegerProperty>());
expect(
calendar.getProperty<IntegerProperty>('X-XXX-NUMBER')?.intValue, 123);
});
}

0 comments on commit 1dd1669

Please sign in to comment.