Files
Libation/Source/LibationFileManager/Templates/ContributorDto.cs
Jo-Be-Co 812e0c3b60 Format details on fields specified in format. Introduce by colon.
Use dictionaries for field access and sorting.

Added sorting to series lists.
2026-03-22 13:55:25 +01:00

44 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using NameParser;
using System;
using System.Collections.Generic;
using FileManager.NamingTemplate;
namespace LibationFileManager.Templates;
public class ContributorDto(string name, string? audibleContributorId) : IFormattable
{
private HumanName HumanName { get; } = new(RemoveSuffix(name), Prefer.FirstOverPrefix);
private string? AudibleContributorId { get; } = audibleContributorId;
public static readonly Dictionary<string, Func<ContributorDto, object?>> FormatReplacements = new(StringComparer.OrdinalIgnoreCase)
{
// Single-word names parse as first names. Use it as last name.
{ "L", dto => string.IsNullOrWhiteSpace(dto.HumanName.Last) ? dto.HumanName.First : dto.HumanName.Last },
// Because of the above, if we have only a first name, then we'd double the name as "FirstName FirstName", so clear the first name in that situation.
{ "F", dto => string.IsNullOrWhiteSpace(dto.HumanName.Last) ? dto.HumanName.Last : dto.HumanName.First },
{ "T", dto => dto.HumanName.Title },
{ "M", dto => dto.HumanName.Middle },
{ "S", dto => dto.HumanName.Suffix },
{ "ID", dto => dto.AudibleContributorId },
};
public override string ToString()
=> ToString("{T} {F} {M} {L} {S}", null);
public string ToString(string? format, IFormatProvider? provider)
{
if (string.IsNullOrWhiteSpace(format))
return ToString();
return CommonFormatters.TemplateStringFormatter(this, format, provider, FormatReplacements).Trim();
}
private static string RemoveSuffix(string namesString)
{
namesString = namesString.Replace('', '\'').Replace(" - Ret.", ", Ret.");
var dashIndex = namesString.IndexOf(" - ", StringComparison.Ordinal);
return (dashIndex > 0 ? namesString[..dashIndex] : namesString).Trim();
}
}