namespace WPFluent.Extensions; /// /// Extensions for class. /// public static class UriExtensions { /// /// Append provided segments to the . /// public static Uri Append(this Uri uri, params string[] segments) { if(!uri.IsAbsoluteUri) { return uri; // or throw? } return new Uri( segments.Aggregate( uri.AbsoluteUri, (current, path) => $"{current.TrimEnd('/').TrimEnd('\\')}/{path.TrimStart('/').TrimStart('\\')}")); } /// /// Append new to the . /// public static Uri Append(this Uri uri, Uri value) { return new Uri( $"{uri.ToString().TrimEnd('/').TrimEnd('\\')}/{value.ToString().TrimStart('/').TrimStart('\\')}", UriKind.RelativeOrAbsolute); } /// /// Determines whether the end of is equal to provided value. /// public static bool EndsWith(this Uri uri, string value) { return uri.ToString().EndsWith(value); } /// /// Removes last segment of the . /// public static Uri TrimLastSegment(this Uri uri) { if(uri.Segments.Length < 2) { return uri; } var uriLastSegmentLength = uri.Segments[^1].Length; var uriOriginalString = uri.ToString(); return new Uri(uriOriginalString[..^uriLastSegmentLength], UriKind.RelativeOrAbsolute); } }