Utils: Add SplitAddress and SplitName
This commit is contained in:
@ -25,6 +25,7 @@ namespace Elwig.Helpers {
|
|||||||
public static readonly Regex PartialDateRegex = GeneratedPartialDateRegex();
|
public static readonly Regex PartialDateRegex = GeneratedPartialDateRegex();
|
||||||
public static readonly Regex FromToRegex = GeneratedFromToRegex();
|
public static readonly Regex FromToRegex = GeneratedFromToRegex();
|
||||||
public static readonly Regex FromToTimeRegex = GeneratedFromToTimeRegex();
|
public static readonly Regex FromToTimeRegex = GeneratedFromToTimeRegex();
|
||||||
|
public static readonly Regex AddressRegex = GeneratedAddressRegex();
|
||||||
|
|
||||||
[GeneratedRegex("^serial://([A-Za-z0-9]+):([0-9]+)(,([5-9]),([NOEMSnoems]),(0|1|1\\.5|2|))?$", RegexOptions.Compiled)]
|
[GeneratedRegex("^serial://([A-Za-z0-9]+):([0-9]+)(,([5-9]),([NOEMSnoems]),(0|1|1\\.5|2|))?$", RegexOptions.Compiled)]
|
||||||
private static partial Regex GeneratedSerialRegex();
|
private static partial Regex GeneratedSerialRegex();
|
||||||
@ -41,6 +42,9 @@ namespace Elwig.Helpers {
|
|||||||
[GeneratedRegex(@"^([0-9]{1,2}:[0-9]{2})?-([0-9]{1,2}:[0-9]{2})?$", RegexOptions.Compiled)]
|
[GeneratedRegex(@"^([0-9]{1,2}:[0-9]{2})?-([0-9]{1,2}:[0-9]{2})?$", RegexOptions.Compiled)]
|
||||||
private static partial Regex GeneratedFromToTimeRegex();
|
private static partial Regex GeneratedFromToTimeRegex();
|
||||||
|
|
||||||
|
[GeneratedRegex(@"^(.*?) +([0-9].*)$", RegexOptions.Compiled)]
|
||||||
|
private static partial Regex GeneratedAddressRegex();
|
||||||
|
|
||||||
private static readonly ushort[] Crc16ModbusTable = {
|
private static readonly ushort[] Crc16ModbusTable = {
|
||||||
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
|
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
|
||||||
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
|
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
|
||||||
@ -292,5 +296,30 @@ namespace Elwig.Helpers {
|
|||||||
public static string GenerateLsNr(Delivery d) => GenerateLsNr(d.Date, d.ZwstId, d.LNr);
|
public static string GenerateLsNr(Delivery d) => GenerateLsNr(d.Date, d.ZwstId, d.LNr);
|
||||||
|
|
||||||
public static string GenerateLsNr(DateOnly date, string zwstid, int lnr) => $"{date:yyyyMMdd}{zwstid}{lnr:000}";
|
public static string GenerateLsNr(DateOnly date, string zwstid, int lnr) => $"{date:yyyyMMdd}{zwstid}{lnr:000}";
|
||||||
|
|
||||||
|
public static (string, string?) SplitAddress(string address) {
|
||||||
|
var m = AddressRegex.Match(address);
|
||||||
|
return (m.Groups[1].Value, m.Groups[2].Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static (string, string?) SplitName(string fullName, string? familyName) {
|
||||||
|
if (familyName == null) return (fullName, null);
|
||||||
|
var p0 = fullName.ToLower().IndexOf(familyName.ToLower());
|
||||||
|
if (p0 == -1) return (fullName, null);
|
||||||
|
var p1 = fullName.IndexOf(" und ");
|
||||||
|
var p2 = fullName.ToLower().LastIndexOf(" und ");
|
||||||
|
if (p1 != p2) {
|
||||||
|
if (p0 > p1) {
|
||||||
|
// A und B familyName [und ...]
|
||||||
|
return (fullName[p0..^0], fullName[0..(p0 - 1)]);
|
||||||
|
} else {
|
||||||
|
// familyName und ... A und B
|
||||||
|
var p3 = fullName.LastIndexOf(' ', p2 - 1);
|
||||||
|
return (fullName[0..p3], fullName[(p3 + 1)..^0]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return (familyName, fullName.Replace(familyName, "").Replace(" ", " ").Trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,5 +68,37 @@ namespace Tests {
|
|||||||
Assert.Throws<ArgumentException>(() => Utils.Modulo("789", -1));
|
Assert.Throws<ArgumentException>(() => Utils.Modulo("789", -1));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Test_SplitAddress() {
|
||||||
|
Assert.Multiple(() => {
|
||||||
|
Assert.That(Utils.SplitAddress("Winzerstra<72>e 1"), Is.EqualTo(("Winzerstra<72>e", "1")));
|
||||||
|
Assert.That(Utils.SplitAddress("Auf dem Feld 12"), Is.EqualTo(("Auf dem Feld", "12")));
|
||||||
|
Assert.That(Utils.SplitAddress("Winzerstra<72>e 5a"), Is.EqualTo(("Winzerstra<72>e", "5a")));
|
||||||
|
Assert.That(Utils.SplitAddress("Winzerstra<72>e 1-3/2"), Is.EqualTo(("Winzerstra<72>e", "1-3/2")));
|
||||||
|
Assert.That(Utils.SplitAddress("Winzerstra<72>e 3/4/5"), Is.EqualTo(("Winzerstra<72>e", "3/4/5")));
|
||||||
|
Assert.That(Utils.SplitAddress("Winzerstra<72>e 7/2/4/77"), Is.EqualTo(("Winzerstra<72>e", "7/2/4/77")));
|
||||||
|
Assert.That(Utils.SplitAddress("Winzerstra<72>e 95b"), Is.EqualTo(("Winzerstra<72>e", "95b")));
|
||||||
|
Assert.That(Utils.SplitAddress("Winzerstra<72>e 1, TOP 3"), Is.EqualTo(("Winzerstra<72>e", "1, TOP 3")));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Test_SplitName() {
|
||||||
|
Assert.Multiple(() => {
|
||||||
|
Assert.That(Utils.SplitName("Max Bauer", "Bauer"), Is.EqualTo(("Bauer", "Max")));
|
||||||
|
Assert.That(Utils.SplitName("Bauer Max", "Bauer"), Is.EqualTo(("Bauer", "Max")));
|
||||||
|
Assert.That(Utils.SplitName("Max und Moritz Bauer", "Bauer"), Is.EqualTo(("Bauer", "Max und Moritz")));
|
||||||
|
Assert.That(Utils.SplitName("Bauer Max und Moritz", "Bauer"), Is.EqualTo(("Bauer", "Max und Moritz")));
|
||||||
|
Assert.That(Utils.SplitName("Bauer GesbR", "Bauer"), Is.EqualTo(("Bauer", "GesbR")));
|
||||||
|
Assert.That(Utils.SplitName("Max und Moritz Bauer GesbR", "Bauer"), Is.EqualTo(("Bauer", "Max und Moritz GesbR")));
|
||||||
|
Assert.That(Utils.SplitName("Bauer Max und Moritz GesbR", "Bauer"), Is.EqualTo(("Bauer", "Max und Moritz GesbR")));
|
||||||
|
Assert.That(Utils.SplitName("Weingut Bauer", "Bauer"), Is.EqualTo(("Bauer", "Weingut")));
|
||||||
|
Assert.That(Utils.SplitName("Bauer Weingut", "Bauer"), Is.EqualTo(("Bauer", "Weingut")));
|
||||||
|
Assert.That(Utils.SplitName("Max und Moritz Bauer und Mustermann", "Bauer"), Is.EqualTo(("Bauer und Mustermann", "Max und Moritz")));
|
||||||
|
Assert.That(Utils.SplitName("Bauer und Mustermann Max und Moritz", "Bauer"), Is.EqualTo(("Bauer und Mustermann", "Max und Moritz")));
|
||||||
|
Assert.That(Utils.SplitName("ABC GesbR", "Bauer"), Is.EqualTo(((string, string?))("ABC GesbR", null)));
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user