Skip to content

Commit e85885a

Browse files
committed
Add Inspector#export
1 parent 02e5c59 commit e85885a

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.4.0
2+
- Added #export to Inspector for easy dumping and sharing of datasets.
3+
14
## 2.3.1
25
- Made sure to export Inspector...
36

lib/src/inspector.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:io';
22

3+
import 'package:traindown/src/formatter.dart';
34
import 'package:traindown/src/metadata.dart';
45
import 'package:traindown/src/movement.dart';
56
import 'package:traindown/src/performance.dart';
@@ -20,7 +21,9 @@ class Inspector {
2021
List<Session> sessions;
2122
List<String> _extensions = ['.traindown'];
2223

23-
Inspector(this.sessions, [this._extensions]);
24+
Inspector(this.sessions, [this._extensions]) {
25+
sessions.sort((a, b) => b.occurred.compareTo(a.occurred));
26+
}
2427

2528
/// Convenience constructor for slurping up files.
2629
factory Inspector.from_files(List<File> files,
@@ -33,6 +36,8 @@ class Inspector {
3336
}
3437
}
3538

39+
inspector.sessions.sort((a, b) => b.occurred.compareTo(a.occurred));
40+
3641
return inspector;
3742
}
3843

@@ -71,6 +76,20 @@ class Inspector {
7176
return valid;
7277
}
7378

79+
/// Export all Sessions as a single Traindown string. Good for dumping
80+
/// and sharing.
81+
String export(
82+
{String indenter = ' ',
83+
String linebreaker = '\r\n',
84+
String spacer = ' '}) {
85+
Formatter formatter =
86+
Formatter(indenter: indenter, linebreaker: linebreaker, spacer: spacer);
87+
StringBuffer buffer = StringBuffer();
88+
sessions.forEach((s) => buffer
89+
.write('${formatter.format(s.tokens)}${formatter.linebreaker * 2}'));
90+
return buffer.toString().trim();
91+
}
92+
7493
/// Map keyed by string that contains all seen values of the given key.
7594
Map<String, Set<String>> metadataByKey(
7695
[TraindownScope scope = TraindownScope.session]) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: traindown
22
description: A language to help athletes express their training. Inspired by Markdown.
33
homepage: https://www.traindown.com/
4-
version: 2.3.1
4+
version: 2.4.0
55
environment:
66
sdk: ">=2.2.0 <3.0.0"
77
dependencies:

test/inspector_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,41 @@ void main() {
225225
expect(subject.validFile(File('./example/example.traindown')), false);
226226
});
227227
});
228+
229+
group('export', () {
230+
test('It concats all the strings like a boss', () {
231+
List<Token> tokens1 = [
232+
Token(TokenType.DateTime, "2020-01-01"),
233+
Token(TokenType.MetaKey, "Your"),
234+
Token(TokenType.MetaValue, "Mom"),
235+
Token(TokenType.MetaKey, "Foo"),
236+
Token(TokenType.MetaValue, "Bar"),
237+
];
238+
Session session1 = Session(tokens1);
239+
240+
List<Token> tokens2 = [
241+
Token(TokenType.DateTime, "2021-01-01"),
242+
Token(TokenType.MetaKey, "Your"),
243+
Token(TokenType.MetaValue, "Mom"),
244+
Token(TokenType.MetaKey, "Bar"),
245+
Token(TokenType.MetaValue, "Baz"),
246+
];
247+
Session session2 = Session(tokens2);
248+
249+
Inspector subject = Inspector([session1, session2]);
250+
251+
String expected = """
252+
@ 2021-01-01
253+
254+
# Your: Mom
255+
# Bar: Baz
256+
257+
@ 2020-01-01
258+
259+
# Your: Mom
260+
# Foo: Bar""";
261+
262+
expect(subject.export(linebreaker: '\n'), expected);
263+
});
264+
});
228265
}

0 commit comments

Comments
 (0)