Today, a new version of HelpMate.Core has been released.

What HelpMate.Core Is

HelpMate.Core is a simple library that provides a set of extension methods that you can use to perform common validations and conversions in C#. With HelpMate.Core, you don't need to write helper functions yourself anymore every time you implement a new project that does common things, not unless if necessary. Instead, you can just plug this package into your project and then you're good to go.

How to get it?

  1. Download and Install the latest HelpMate.Core from NuGet or via CLI:
PM> Install-Package HelpMate.Core -Version 2.1.0
  1. Declare the following namespace in your class:
using HelpMate.Core.Extensions;

Quick Examples

The following are a few quick sample usage of using the extension methods:

var date = "60/1011/2021".ToDateTime();

if (date.IsValidDateTime())
    // Date is valid

Person person = null;

if (person.IsNotNull())
    //do something if not null

List<Person> people = null;

foreach (var person in people.AsNotNull())
    //iterate when not null

var data = new Person
    Id = 1,
    Name = "Vince"

string jsonString = data.ToJson();

if (jsonString.IsValidJson())
    //do something

What's Added?

This release comes with a few new extension methods for performing, conversions, validations, and null and value handling.

Converter Extensions

DateTime ToDateTime(this string value)
DateTime? ToNullableDateTime(this string value)
short ToInt16(this string value)
short? ToNullableInt16(this string value)
int ToInt32(this string value)
int? ToNullableInt32(this string value)
long ToInt64(this string value)
long? ToNullableInt64(this string value)
byte ToByte(this string value)
byte? ToNullableByte(this string value)
bool ToBoolean(this string value)
bool? ToNullableBoolean(this string value)
float ToFloat(this string value)
float? ToNullableFloat(this string value)
decimal ToDecimal(this string value)
decimal? ToNullableDecimal(this string value)
double ToDouble(this string value)
double? ToNullableDouble(this string value)
Guid ToGuid(this string value)
Guid? ToNullableGuid(this string value)
string ToBase64Encode(this string value) 
string ToBase64Encode(this byte[] value) 
string ToBase64Decode(this string value) 
byte[] ToByteFromBase64CharArray(this string value) 
byte[] ToByteArray(this string value) 
string ToDateTimeFormat(this string date, string format) 
string ToDateTimeFormat(this string date, string format, CultureInfo cultureInfo)
T ToEnum<T>(this string value, T defaultValue, bool ignoreCase = false)
string ToCamelCase(this string value)
string ToJson<T>(this T value, JsonSerializerOptions jsonOptions = null)
int GetYearsFromDate(this DateTime date)
int GetYearsDifference(this DateTime date, DateTime dateToCompare)

Format Validation Extensions

bool IsValidEmailFormat(this string value)
bool IsValidCreditCardNumber(this string value)
bool IsValidPhoneNumber(this string value, int minLength = default, int maxLength = default)
bool IsValidJson(this string json)
bool IsValidDateTime(this DateTime? date)
bool IsValidDateTime(this DateTime date)
bool IsValidDateTimeString(this string date)
bool IsValidStandardDateString(this string value) 
bool IsFutureDate(this string date) 
bool IsNumber(this string value)
bool IsWholeNumber(this string value)
bool IsDecimalNumber(this string value)
bool IsBoolean(this string value)
bool IsHtml(this string value)
bool IsAlphaNumeric(this string value)
bool IsAlphaNumericStrict(this string value)

Null Handling Extensions

IEnumerable<T> AsNotNull<T>(this IEnumerable<T> source)
bool IsNotNull<T>(this IEnumerable<T> source)
bool IsNotNull<T>(this T source)
bool IsNull<T>(this IEnumerable<T> source)
bool IsNull<T>(this T source)

Introducing ThrowsWhen

The new version also comes with a few validator clause that you can use to validate required values in a fluent manner. For example, instead of doing the following code everytime you check for nulls:

int? value = null;

if (value is null)
    throw new ArgumentException($"The given input {nameof(value)} was null.");

You can now simplify the preceding code using the `ThrowsWhen` validation:

int? value = null;

ValueValidator.ThrowsWhen.Null(value, nameof(value));

Another example is validating against enum values. Let's say you have the following enum:

public enum AType
    Valid = 1,
    Invalid = 2

You can simply do the following to validate if the given input exists in enum:

var type = "DoesNotExist";

The following are the available validation clauses that you can use for this release:

ValueValidator.ThrowsWhen.Null(T input, string parameterName);
ValueValidator.ThrowsWhen.Null(T input, string parameterName, string message);
ValueValidator.ThrowsWhen.NullOrWhiteSpace(string input, string parameterName);
ValueValidator.ThrowsWhen.NullOrEmpty(Guid? input, string parameterName);
ValueValidator.ThrowsWhen.False(bool input, string messsage);
ValueValidator.ThrowsWhen.True(bool input, string messsage);
ValueValidator.ThrowsWhen.NotDefined(object input);

That's it. Let me know your thoughts in the comments section below. I hope you find this library useful.

Cheers! 🍻