diff --git a/CHANGELOG.md b/CHANGELOG.md index 87d981b..cd096c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Changed + - Added GetParentUri method to UriExtensions + ## [14.0.0] - 2021-01-14 ### Added - Added Get and Set extensions to PropertyInfo in SIMPLSHARP to mimic overloads avaliable in NETSTANDARD diff --git a/ICD.Common.Utils.Tests/Extensions/UriExtensionsTest.cs b/ICD.Common.Utils.Tests/Extensions/UriExtensionsTest.cs index 04514f1..2a760dd 100644 --- a/ICD.Common.Utils.Tests/Extensions/UriExtensionsTest.cs +++ b/ICD.Common.Utils.Tests/Extensions/UriExtensionsTest.cs @@ -18,5 +18,13 @@ namespace ICD.Common.Utils.Tests.Extensions { Assert.AreEqual(expected, new Uri(uriString).GetPassword()); } + + [TestCase("http://www.test.com/a/b/c", "http://www.test.com/a/b/")] + [TestCase("http://www.test.com/a/b/", "http://www.test.com/a/")] + [TestCase("http://www.test.com/", "http://www.test.com/")] + public void GetParentUri(string uriString, string expected) + { + Assert.AreEqual(expected, new Uri(uriString).GetParentUri().ToString()); + } } } diff --git a/ICD.Common.Utils/Extensions/UriExtensions.cs b/ICD.Common.Utils/Extensions/UriExtensions.cs index 503f295..0e77c58 100644 --- a/ICD.Common.Utils/Extensions/UriExtensions.cs +++ b/ICD.Common.Utils/Extensions/UriExtensions.cs @@ -62,5 +62,23 @@ namespace ICD.Common.Utils.Extensions return builder.ToString(); } + + /// + /// Returns a new Uri representing the Uri for the parent path. + /// E.g. + /// www.test.com/A/B/C + /// Becomes + /// www.test.com/A/B/ + /// + /// + /// + public static Uri GetParentUri([NotNull] this Uri extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + string parentUriString = extends.AbsoluteUri.Remove(extends.AbsoluteUri.Length - extends.Segments.Last().Length); + return new Uri(parentUriString, UriKind.Absolute); + } } }