From 5980b64caa91d5f80ada71e00064df54e47e5adb Mon Sep 17 00:00:00 2001 From: Glax Date: Wed, 11 Sep 2024 14:40:03 +0200 Subject: [PATCH] Readable Chapter comparison --- Tranga/Chapter.cs | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/Tranga/Chapter.cs b/Tranga/Chapter.cs index 369b8fc..2dbf346 100644 --- a/Tranga/Chapter.cs +++ b/Tranga/Chapter.cs @@ -58,29 +58,24 @@ public readonly struct Chapter : IComparable public int CompareTo(object? obj) { - if (obj is Chapter otherChapter) + if(obj is not Chapter otherChapter) + throw new ArgumentException($"{obj} can not be compared to {this}"); + + if (float.TryParse(volumeNumber, GlobalBase.numberFormatDecimalPoint, out float volumeNumberFloat) && + float.TryParse(chapterNumber, GlobalBase.numberFormatDecimalPoint, out float chapterNumberFloat) && + float.TryParse(otherChapter.volumeNumber, GlobalBase.numberFormatDecimalPoint, + out float otherVolumeNumberFloat) && + float.TryParse(otherChapter.chapterNumber, GlobalBase.numberFormatDecimalPoint, + out float otherChapterNumberFloat)) { - if (float.TryParse(volumeNumber, GlobalBase.numberFormatDecimalPoint, out float volumeNumberFloat) && - float.TryParse(chapterNumber, GlobalBase.numberFormatDecimalPoint, out float chapterNumberFloat) && - float.TryParse(otherChapter.volumeNumber, GlobalBase.numberFormatDecimalPoint, - out float otherVolumeNumberFloat) && - float.TryParse(otherChapter.chapterNumber, GlobalBase.numberFormatDecimalPoint, - out float otherChapterNumberFloat)) + return volumeNumberFloat.CompareTo(otherVolumeNumberFloat) switch { - - switch (volumeNumberFloat.CompareTo(otherVolumeNumberFloat)) - { - case < 0: - return -1; - case > 0: - return 1; - default: - return chapterNumberFloat.CompareTo(otherChapterNumberFloat); - } - } - else throw new FormatException($"Value could not be parsed"); + <0 => -1, + >0 => 1, + _ => chapterNumberFloat.CompareTo(otherChapterNumberFloat) + }; } - throw new ArgumentException($"{obj} can not be compared to {this}"); + else throw new FormatException($"Value could not be parsed"); } ///