A set of tiny utilities to help on web projects
Install-Package FriendToNetWebDevelopers.HtmlAttributeDictionary
For each of these, you'll need to include this.
using FriendToNetWebDeveloper.MicroUtilities;
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.
//Use this when you don't have any information to go off of or don't want that to be public
var id = Utilities.GetValidHtmlId();
//Generates something like this: "id12810faad82640c09a9025c9f4909345"
//Use this to pass in an existing GUID
id = Utilities.GetValidHtmlId(new Guid("aeda0af0-dbc7-4d83-a568-557d27074781"));
//Generates: "idaeda0af0dbc74d83a568557d27074781"
// Prefix ↓ Suffix ↓
id = Utilities.GetValidHtmlId(44, "id__", "__suffix");
//Generates: "id__44__suffix"
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
This utility has to do with validation and generation of urls.
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
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 = ""
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);
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
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
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
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