micro-utilities

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

Visit original content creator repository

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *