Unit Tests & New Natural Sort (#941)
* Added a lot of tests * More tests! Added a Parser.NormalizePath to normalize all paths within Kavita. * Fixed a bug where MarkChaptersAsUnread implementation wasn't consistent between different files and lead to extra row generation for no reason. * Added more unit tests * Found a better implementation for Natural Sorting. Added tests and validate it works. Next commit will swap out natural Sort for new Extension. * Replaced NaturalSortComparer with OrderByNatural. * Drastically simplified and sped up FindFirstEntry for finding cover images in archives * Initial fix for a epub bug where metadata defines key as absolute path but document uses a relative path. We now have a hack to correct for the epub.
This commit is contained in:
parent
71d42b1c8b
commit
591b574706
36 changed files with 1533 additions and 314 deletions
|
@ -1,8 +1,30 @@
|
|||
namespace API.Extensions
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace API.Extensions
|
||||
{
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
private static readonly Regex Regex = new Regex(@"\d+", RegexOptions.Compiled, TimeSpan.FromMilliseconds(500));
|
||||
|
||||
/// <summary>
|
||||
/// A natural sort implementation
|
||||
/// </summary>
|
||||
/// <param name="items">IEnumerable to process</param>
|
||||
/// <param name="selector">Function that produces a string. Does not support null values</param>
|
||||
/// <param name="stringComparer">Defaults to CurrentCulture</param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns>Sorted Enumerable</returns>
|
||||
public static IEnumerable<T> OrderByNatural<T>(this IEnumerable<T> items, Func<T, string> selector, StringComparer stringComparer = null)
|
||||
{
|
||||
var maxDigits = items
|
||||
.SelectMany(i => Regex.Matches(selector(i))
|
||||
.Select(digitChunk => (int?)digitChunk.Value.Length))
|
||||
.Max() ?? 0;
|
||||
|
||||
return items.OrderBy(i => Regex.Replace(selector(i), match => match.Value.PadLeft(maxDigits, '0')), stringComparer ?? StringComparer.CurrentCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,15 +31,15 @@ namespace API.Extensions
|
|||
: infos.Any(v => v.Chapters == chapter.Range);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the MangaFormat that is common to all the files. Unknown if files are mixed (should never happen) or no infos
|
||||
/// </summary>
|
||||
/// <param name="infos"></param>
|
||||
/// <returns></returns>
|
||||
public static MangaFormat GetFormat(this IList<ParserInfo> infos)
|
||||
{
|
||||
if (infos.Count == 0) return MangaFormat.Unknown;
|
||||
return infos.DistinctBy(x => x.Format).First().Format;
|
||||
}
|
||||
// /// <summary>
|
||||
// /// Returns the MangaFormat that is common to all the files. Unknown if files are mixed (should never happen) or no infos
|
||||
// /// </summary>
|
||||
// /// <param name="infos"></param>
|
||||
// /// <returns></returns>
|
||||
// public static MangaFormat GetFormat(this IList<ParserInfo> infos)
|
||||
// {
|
||||
// if (infos.Count == 0) return MangaFormat.Unknown;
|
||||
// return infos.DistinctBy(x => x.Format).First().Format;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ public static class PathExtensions
|
|||
{
|
||||
if (string.IsNullOrEmpty(filepath)) return filepath;
|
||||
var extension = Path.GetExtension(filepath);
|
||||
if (string.IsNullOrEmpty(extension)) return filepath;
|
||||
return Path.GetFullPath(filepath.Replace(extension, string.Empty));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ namespace API.Extensions
|
|||
public static class SeriesExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks against all the name variables of the Series if it matches anything in the list.
|
||||
/// Checks against all the name variables of the Series if it matches anything in the list. This does not check against format.
|
||||
/// </summary>
|
||||
/// <param name="series"></param>
|
||||
/// <param name="list"></param>
|
||||
|
|
|
@ -12,7 +12,8 @@ namespace API.Extensions
|
|||
{
|
||||
return inBookSeries
|
||||
? volumes.FirstOrDefault(v => v.Chapters.Any())
|
||||
: volumes.OrderBy(v => v.Number, new ChapterSortComparer()).FirstOrDefault(v => v.Chapters.Any());
|
||||
: volumes.OrderBy(v => v.Number, new ChapterSortComparer())
|
||||
.FirstOrDefault(v => v.Chapters.Any());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue