feat: Added GetParentUri method to UriExtensions

This commit is contained in:
Chris Cameron
2021-01-18 15:22:28 -05:00
parent 9b53d77d6b
commit c7e8f09eeb
3 changed files with 29 additions and 0 deletions

View File

@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased] ## [Unreleased]
### Changed
- Added GetParentUri method to UriExtensions
## [14.0.0] - 2021-01-14 ## [14.0.0] - 2021-01-14
### Added ### Added
- Added Get and Set extensions to PropertyInfo in SIMPLSHARP to mimic overloads avaliable in NETSTANDARD - Added Get and Set extensions to PropertyInfo in SIMPLSHARP to mimic overloads avaliable in NETSTANDARD

View File

@@ -18,5 +18,13 @@ namespace ICD.Common.Utils.Tests.Extensions
{ {
Assert.AreEqual(expected, new Uri(uriString).GetPassword()); 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());
}
} }
} }

View File

@@ -62,5 +62,23 @@ namespace ICD.Common.Utils.Extensions
return builder.ToString(); return builder.ToString();
} }
/// <summary>
/// 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/
/// </summary>
/// <param name="extends"></param>
/// <returns></returns>
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);
}
} }
} }