FriendToNetWebDevelopers.MicroUtilities 1.0.7

dotnet add package FriendToNetWebDevelopers.MicroUtilities --version 1.0.7
                    
NuGet\Install-Package FriendToNetWebDevelopers.MicroUtilities -Version 1.0.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FriendToNetWebDevelopers.MicroUtilities" Version="1.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FriendToNetWebDevelopers.MicroUtilities" Version="1.0.7" />
                    
Directory.Packages.props
<PackageReference Include="FriendToNetWebDevelopers.MicroUtilities" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FriendToNetWebDevelopers.MicroUtilities --version 1.0.7
                    
#r "nuget: FriendToNetWebDevelopers.MicroUtilities, 1.0.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=FriendToNetWebDevelopers.MicroUtilities&version=1.0.7
                    
Install as a Cake Addin
#tool nuget:?package=FriendToNetWebDevelopers.MicroUtilities&version=1.0.7
                    
Install as a Cake Tool

Micro Utilities by Friend to .NET Web Developers

Summary

A set of tiny utilities to help on web projects

Installation

Install-Package FriendToNetWebDevelopers.HtmlAttributeDictionary

Usage & Available Utilities

For each of these, you'll need to include this.

using FriendToNetWebDeveloper.MicroUtilities;

Email validation

This utility attempts to validate email emails by checking for formatting and also checking a list of valid top-level domains as provided by icann.org.

Testing was based on this gist. However, it makes no attempt to accept the Strange Valid email addresses category.

var okay = Utilities.Email.IsValidEmail("none@none.com");
//return true

okay = Utilities.Email.IsValidEmail("foo@bar");
//returns false

Uri Utilities

This utility has to do with validation and generation of urls.

Build Absolute URL

Specifically for taking the correct portions of a URI and making them build based on whether or not a developer is running using localhost with a port.

var urlString = Utilities.Url.BuildAbsoluteUrl(uri);
//                 No port included ↓
//On the server: https://example.com/some-file.jpg
//                 Has a port                ↓
//Debug on local machine: https://localhost:44328/some-file.jpg
Uri Slug Generation & Validation

Slugs are used to safely build out a url segment based on, for instance, the title of a document. Generating them can be somewhat tricky. These functions serve to simplify that for the developer.

Regex for valid slug: ^[a-z0-9]+(?:-[a-z0-9]+)*$

Validation

Utilities.Url.IsValidUriSlug("foo-bar");
//returns true

Utilities.Url.IsValidUriSlug("Foo Bar");
//returns false

Generation

var okay = Utilities.Url.TryToConvertToSlug("Foo Bar", out var slug);
// okay = true
// slug = "foo-bar"

var okay = Utilities.Url.TryToConvertToSlug("-", out var slug);
// okay = false
// slug = ""

var okay = Utilities.Url.TryToConvertToSlug(null, out var slug);
// okay = false
// slug = ""
Url Building Based On A Query Object

Use this to take a known base url (as a string) and dynamically append a query string to it based on either IDictionary<string, string> or IEnumerable<KeyValuePair<string, string>>.

The dictionary is simple in that it avoid repetition. However, the list of key value pairs can allow for multiple of one key. For instance, allowing something[]=1 and something[]=2 which would come in at the server level as a list on the receiving server.

//          This is the dictionary or enumerable object for the query ↓
var finalUrl = Utilities.Url.BuildUrl("https://api.foobar.com", queryObject);
Top-level Domain Validation

Checks if the Top-level domain within the host of the given URI is a valid domain. Queries against the text file provided by ICANN / IANA for the final check.

Utilities.Url.HasValidTopLevelDomain(new Uri("https://foobar.com"));
//Returns true
Utilities.Url.HasValidTopLevelDomain(new Uri("https://foobar.web"));
//Returns false

ID Utilities

The ID utilities are meant to quickly get, validate, and return valid id attributes.

Generate IDs

This is used when creating elements which need to refer to each other by id but there can be many on the page at the same time (accordion elements, sliders, etc).

A prefix is required and included by default. You can change what the prefix is by adding it in. As suffix may also be included.

//Generates a valid id attribute value based on a guid with a prefix of "id"
//example: Returns id02bfd4e04f0b43f9bf407d3162db9289 (generated from new Guid)
Utilities.Id.GetValidHtmlId();

//Formats various types of data into a valid id value
// types include Guid, int, uint, long, ulong
// also includes:                |    prefix   suffix
//                               ↓       ↓        ↓                 
Utilities.Id.GetValidHtmlId(4444, "foo_", "_bar");
// ↑ Returns "foo_4444_bar"
Validate IDs

This utility can also be used to validate IDs which can be specified in an unsafe way (user input).

string? nullId = null;
Utilities.Id.IsValidId(nullId);
// ↑ Returns false

Utilities.Id.IsValidId("bob dole");
// ↑ Returns false

Utilities.ID.IsValidId("bob_dole");
// ↑ Returns true

It can also parse an unsafe, nullable proposed id value and ensure a non-nullable string is output.

Utilities.Id.TryGetAsValidId("bob dole", TryGetValidIdDefaultStrategyEnum.EmptyOnInvalid, var out thisWillBeEmpty);
// ↑ Returns false | thisWilBeEmpty will return string.Empty - this is so that other transformations can be handled

Utilities.Id.TryGetAsValidId("foo bar", TryGetValidIdDefaultStrategyEnum.GenerateOnInvalid, var out thisWillBeAGeneratedId);
// ↑ Returns false | thisWillBeAGeneratedId will return default value from GetValidHtmlId()

Utilities.Id.TryGetAsValidId("foo_bar_baz", TryGetValidIdDefaultStrategyEnum.GenerateOnInvalid, var out thisWillBeFooBarBaz);
// ↑ Returns true | thisWillBeFooBarBaz will be "foo_bar_baz"

Youtube Utilities

ID Validation

Checks if the given ID is valid based on matching the regex pattern: [a-zA-Z0-9_-]{11}

Utilities.Youtube.IsValidYoutubeId("SrN4A9rVXj0");
// ↑ Returns true
Utilities.Youtube.IsValidYoutubeId("foo-bar");
// ↑ Returns false
Thumbnail

Retrieves the thumbnail for the given youtube id.

var thumbnailUrl = Utilities.Youtube.GetYoutubeThumbnail("SrN4A9rVXj0");
//Returns "https://i.ytimg.com/vi/SrN4A9rVXj0/hqdefault.jpg"
thumbnailUrl = Utilities.Youtube.GetYoutubeThumbnail("SrN4A9rVXj0", YoutubeThumbnailEnum.MaxResDefault);
//Returns "https://i.ytimg.com/vi/SrN4A9rVXj0/maxresdefault.jpg"
thumbnailUrl = Utilities.Youtube.GetYoutubeThumbnail("foo-bar", YoutubeThumbnailEnum.MaxResDefault);
//Throws BadYoutubeIdException
Embed

Retrieves the url for embedding youtube on a page.

Utilities.Youtube.GetYoutubeIframeUrl("SrN4A9rVXj0");
//Returns https://www.youtube.com/embed/SrN4A9rVXj0
Utilities.Youtube.GetYoutubeIframeUrl("foo-bar");
//Throws BadYoutubeIdException
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.7 71 6/7/2025
1.0.6 173 1/14/2025
1.0.4 139 8/2/2024
1.0.3 130 6/9/2024
1.0.1 120 6/6/2024
1.0.0 118 6/6/2024