From 5280917e7a913113a36a0f681965f9c54ab55126 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 9 Feb 2021 12:00:52 +0100 Subject: [PATCH] Minor refactor --- lib/core/md_yaml_doc.dart | 12 +++++++----- lib/core/md_yaml_doc_codec.dart | 14 +++++++------- test/md_yaml_codec_test.dart | 4 +++- test/md_yaml_doc_test.dart | 4 ++-- test/note_serializer_test.dart | 16 +++++++++------- test/note_storage_test.dart | 2 +- 6 files changed, 29 insertions(+), 23 deletions(-) diff --git a/lib/core/md_yaml_doc.dart b/lib/core/md_yaml_doc.dart index 495841f0..c6939570 100644 --- a/lib/core/md_yaml_doc.dart +++ b/lib/core/md_yaml_doc.dart @@ -5,17 +5,19 @@ import 'package:collection/collection.dart'; Function _deepEq = const DeepCollectionEquality().equals; class MdYamlDoc { - String body = ""; - LinkedHashMap props = LinkedHashMap(); + String body; + LinkedHashMap props; - MdYamlDoc([this.body, this.props]) { - body = body ?? ""; + MdYamlDoc({ + this.body = "", + this.props, + }) { // ignore: prefer_collection_literals props = props ?? LinkedHashMap(); } MdYamlDoc.from(MdYamlDoc other) { - body = String.fromCharCodes(other.body.codeUnits); + body = other.body; props = LinkedHashMap.from(other.props); } diff --git a/lib/core/md_yaml_doc_codec.dart b/lib/core/md_yaml_doc_codec.dart index 05a3549e..74e1576a 100644 --- a/lib/core/md_yaml_doc_codec.dart +++ b/lib/core/md_yaml_doc_codec.dart @@ -25,7 +25,7 @@ class MarkdownYAMLCodec { bodyBeginingPos += 1; } var body = str.substring(bodyBeginingPos); - return MdYamlDoc(body); + return MdYamlDoc(body: body); } if (str.startsWith(startYamlStr)) { @@ -37,10 +37,10 @@ class MarkdownYAMLCodec { var yamlText = str.substring(4, str.length - endYamlStrWithoutLineEding.length); var map = parseYamlText(yamlText); - return MdYamlDoc("", map); + return MdYamlDoc(props: map); } - return MdYamlDoc(str); + return MdYamlDoc(body: str); } var yamlText = str.substring(4, endYamlPos); @@ -57,14 +57,14 @@ class MarkdownYAMLCodec { } } - return MdYamlDoc(body, map); + return MdYamlDoc(body: body, props: map); } if (str.endsWith(endYamlStr)) { var endYamlPos = str.length - endYamlStr.length; var startYamlPos = str.lastIndexOf(startYamlStr, endYamlPos); if (startYamlPos == -1) { - return MdYamlDoc(str); + return MdYamlDoc(body: str); } // FIXME: What if there is nothing afterwards? @@ -74,10 +74,10 @@ class MarkdownYAMLCodec { var body = str.substring(0, startYamlPos); reverse = true; - return MdYamlDoc(body, map); + return MdYamlDoc(body: body, props: map); } - return MdYamlDoc(str, LinkedHashMap()); + return MdYamlDoc(body: str); } static LinkedHashMap parseYamlText(String yamlText) { diff --git a/test/md_yaml_codec_test.dart b/test/md_yaml_codec_test.dart index 1b750094..a52916d5 100644 --- a/test/md_yaml_codec_test.dart +++ b/test/md_yaml_codec_test.dart @@ -17,7 +17,9 @@ void main() { test('Markdown Serializer', () { var created = toIso8601WithTimezone(nowWithoutMicro()); var note = MdYamlDoc( - "This is the body", LinkedHashMap.from({"created": created})); + body: "This is the body", + props: LinkedHashMap.from({"created": created}), + ); var serializer = MarkdownYAMLCodec(); var str = serializer.encode(note); diff --git a/test/md_yaml_doc_test.dart b/test/md_yaml_doc_test.dart index 09206ce7..e3419a8e 100644 --- a/test/md_yaml_doc_test.dart +++ b/test/md_yaml_doc_test.dart @@ -18,8 +18,8 @@ void main() { bProps['title'] = "Foo"; bProps['list'] = ["Foo", "Bar", 1]; - var a = MdYamlDoc("a", aProps); - var b = MdYamlDoc("a", bProps); + var a = MdYamlDoc(body: "a", props: aProps); + var b = MdYamlDoc(body: "a", props: bProps); expect(a, b); }); } diff --git a/test/note_serializer_test.dart b/test/note_serializer_test.dart index 284ee1df..675cdfb0 100644 --- a/test/note_serializer_test.dart +++ b/test/note_serializer_test.dart @@ -15,7 +15,7 @@ void main() { test('Test emojis', () { var props = LinkedHashMap.from( {"title": "Why not :coffee:?"}); - var doc = MdYamlDoc("I :heart: you", props); + var doc = MdYamlDoc(body: "I :heart: you", props: props); var serializer = NoteSerializer.raw(); serializer.settings.saveTitleAsH1 = false; @@ -36,7 +36,8 @@ void main() { test('Test Title Serialization', () { var props = {}; - var doc = MdYamlDoc("# Why not :coffee:?\n\nI :heart: you", props); + var doc = + MdYamlDoc(body: "# Why not :coffee:?\n\nI :heart: you", props: props); var serializer = NoteSerializer.raw(); serializer.settings.saveTitleAsH1 = true; @@ -57,7 +58,8 @@ void main() { test('Test Title Reading with blank lines', () { var props = {}; - var doc = MdYamlDoc("\n# Why not :coffee:?\n\nI :heart: you", props); + var doc = MdYamlDoc( + body: "\n# Why not :coffee:?\n\nI :heart: you", props: props); var serializer = NoteSerializer.raw(); @@ -70,7 +72,7 @@ void main() { test('Test Title Reading with blank lines and no body', () { var props = {}; - var doc = MdYamlDoc("\n# Why not :coffee:?", props); + var doc = MdYamlDoc(body: "\n# Why not :coffee:?", props: props); var serializer = NoteSerializer.raw(); @@ -84,7 +86,7 @@ void main() { test('Test Old Title Serialization', () { var props = LinkedHashMap.from( {"title": "Why not :coffee:?"}); - var doc = MdYamlDoc("I :heart: you", props); + var doc = MdYamlDoc(body: "I :heart: you", props: props); var serializer = NoteSerializer.raw(); serializer.settings.saveTitleAsH1 = true; @@ -106,7 +108,7 @@ void main() { "title": "Why not?", "draft": true, }); - var doc = MdYamlDoc("body", props); + var doc = MdYamlDoc(body: "body", props: props); var serializer = NoteSerializer.raw(); serializer.settings.saveTitleAsH1 = false; @@ -131,7 +133,7 @@ void main() { "draft": true, "tags": "#foo #bar-do", }); - var doc = MdYamlDoc("body", props); + var doc = MdYamlDoc(body: "body", props: props); var serializer = NoteSerializer.raw(); serializer.settings.saveTitleAsH1 = false; diff --git a/test/note_storage_test.dart b/test/note_storage_test.dart index d97bd77b..e3d3a98a 100644 --- a/test/note_storage_test.dart +++ b/test/note_storage_test.dart @@ -34,7 +34,7 @@ void main() { n1.created = dt; var n2 = Note(parent, n2Path); - n2.data = MdYamlDoc("test2", props); + n2.data = MdYamlDoc(body: "test2", props: props); notes = [n1, n2]; });