Skip to content

Instantly share code, notes, and snippets.

@hallojoe
Created July 15, 2024 14:54
Show Gist options
  • Save hallojoe/79811a5f59f9f38cb1ece60cd9d5be24 to your computer and use it in GitHub Desktop.
Save hallojoe/79811a5f59f9f38cb1ece60cd9d5be24 to your computer and use it in GitHub Desktop.
public static class UriStringExtensions
{
public static Uri? ToUri(this string? uriString, UriKind uriKind = UriKind.RelativeOrAbsolute)
{
if (string.IsNullOrWhiteSpace(uriString)) return null;
return Uri.TryCreate(uriString, uriKind, out var uri) ? uri : null;
}
public static IEnumerable<Uri> ToUriCollection(this string? uriCollectionString, char separator = ',', UriKind uriKind = UriKind.RelativeOrAbsolute)
{
if (string.IsNullOrWhiteSpace(uriCollectionString)) return [];
var uriStringCollection = uriCollectionString.Split(separator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
return uriStringCollection.Select(uriString => uriString
.ToUri(uriKind))
.Where(uri => uri is not null)
.Select(uri => uri!);
}
public static GuidUdi? ToGuidUdi(this string? guidUdiUriString)
{
return guidUdiUriString
.ToUri()
.ToGuidUdi();
}
public static IEnumerable<GuidUdi> ToGuidUdiCollection(this string? guidUdiUriStringCollection, char separator = ',')
{
return guidUdiUriStringCollection
.ToUriCollection(separator)
.Select(guidUdiUri => guidUdiUri.ToGuidUdi())
.Where(guidUdi => guidUdi is not null)
.Select(guidUdi => guidUdi!);
}
public static IEnumerable<Guid> ToGuidCollection(this string? guidUdiUriStringCollection, char separator = ',')
{
return guidUdiUriStringCollection.ToGuidUdiCollection(separator).Select(guidUdi => guidUdi.Guid);
}
}
public static class GuidUdiUriExtensions
{
public static GuidUdi? ToGuidUdi(this Uri? guidUdiUri)
{
return guidUdiUri is not { Scheme: "umb"} ? null : new GuidUdi(guidUdiUri);
}
}
public static class GuidUdiExtensions
{
public static IEnumerable<Guid> ToGuidCollection(this IEnumerable<GuidUdi?> guidUdiCollection)
{
return guidUdiCollection
.Where(guidUdi => guidUdi is not null)
.Select(guidUdi => guidUdi!.Guid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment