JamSoft.Helpers 1.0.1

.NET Standard 2.0 .NET Framework 4.8
Install-Package JamSoft.Helpers -Version 1.0.1
dotnet add package JamSoft.Helpers --version 1.0.1
<PackageReference Include="JamSoft.Helpers" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JamSoft.Helpers --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: JamSoft.Helpers, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install JamSoft.Helpers as a Cake Addin
#addin nuget:?package=JamSoft.Helpers&version=1.0.1

// Install JamSoft.Helpers as a Cake Tool
#tool nuget:?package=JamSoft.Helpers&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

JamSoft.Helpers

A collection of general helpers for applications and libraries. The goal is to provide convienience methods and core building blocks. All in a cross-platform .NET Standard 2.0 library.

.NET Core Coverage Status Nuget GitHub

GitHub Pages Site

https://jamsoft.github.io/JamSoft.Helpers/

Install Via Nuget

Install-Package JamSoft.Helpers

xUnit Tests

There is a high level of test coverage as shown in the badge above (around 99%), however, at the moment the pipeline executes on Linux with limited permissions which means some tests cannot be run in this environment. The library has been fully tested on Windows 10, OSX Catalina and Fedora 31.

The following test classes also show basic example implementations and uses of the provided pattern classes.

  • ObserverTests
  • MementoTests
  • MyTestViewModel

Configuration & Settings

Rather than getting embroiled in the convoluted and sometimes awkward user settings infrastructure provided by .NET (*.settings), sometimes you just want to store some values in a file, yes?

The issues and additional complications around user.config and strong names can sometimes get in the way. Using the SettingsBase<T> class can bypass this.

Set Up

Create a POCO class containing your settings properties, default values and inherit SettingsBase<T>, such as:

public sealed class MySettings : SettingsBase<MySettings>
{
    public string ASetting { get; set; } = "A Default Value";
}

Loading & Access

You can now you can manage your settings at rumetime like:

string myDefaultSettingsPath = "C:\Some\location\on\disk";
MySettings.Load(myDefaultSettingsPath);

Will load either load the settings from a file called mysettings.json, or if no file exists, the defaults are loaded.

You can access the values using the instance:

var theValue = MySettings.Instance.ASetting

Or

MySettings.Instance.ASetting = theValue;

Saving

Saving the settings is a call to the Save() method, like:

MySettings.Save();

This will always save back to the same file the settings were originally loaded from, or if there was no file, the file will be created and the settings saved.

Reset

You can easily return back to the defaults by calling the ResetToDefaults() method, like:

MySettings.ResetToDefaults();

This will reset all settings to their default values and immediately write them to disk. If you do not want to write them to disk, simply pass a false to the method.

MySettings.ResetToDefaults(saveToDisk:false);

Environment Variables

There are a handful of helper methods and classes for access environment variables on various platforms.

Common Variables

var path = EnvEx.GetVariable(EnvExVariableNames.Path);

Windows Variables

var appData = EnvEx.GetVariable(EnvExWinVariableNames.AppData); // C:\Users\username\AppData\Roaming

OSX Variables

var shell = EnvEx.GetVariable(EnvExOsxVariableNames.Shell); // "/bin/bash"

Linux Variables

var manPath = EnvEx.GetVariable(EnvExLinuxVariableNames.ManPath); // ":"

More variables names are included in the library than are shown above. You can make use of these via helper constants in the following classes:

  • EnvExVariableNames (Common)
  • EnvExWinVariableNames
  • EnvExOsxVariableNames
  • EnvExLinuxVariableNames

Since the EnvEx.GetVariable method just takes a string, any value can be passed, such as:

var envValue = EnvEx.GetVariable("MYVARIABLENAME");

On Window you can also pass a target parameter of type EnvironmentVariableTarget. The default for this is Process as Linux and OSX do not support this parameter. If anything other than Process is passed on a non-Windows platform it will be defaulted to Process to prevent exceptions being raised.

RSA Cryptography

There is a new little class to help digitally sign data with RSA Cryptography. The main class is created via a factory which can be registered in your DI container of choice.

public interface IRsaCryptoFactory
{
}

container.Register<IRsaCryptoFactory, RsaCryptoFactory>();

You can then use this factory to obtain instances of the service.

var crypto = cryptoFactory.Create();

There are many overloads of the create method to control how the service is built and how you want it configued.

You should also wrap each use of this within a using statement such as:

using(var crypto = cryptoFactory.Create(_privateKey, _publicKey, HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1))
{
   // do your stuff.
}

Once you have created an instance if you do not provide either of the keys, you can obtain the used keys for storage via the two properties, such as:

var crypto = cryptoFactory.Create();
var publicKey = sut.PublicKey
var privateKey = sut.PrivateKey

Graphics

Convert to HEX

int red = 121;
int green = 155;
int blue = 56;

var hex = Graphics.Colors.ToHex(red, green, blue);

Or you can also use an alpha value.

var hex = Graphics.Colors.ToHex(alpha, red, green, blue);

You can also pass values as an array

var hex = Graphics.Colors.ToHex(new[] { alpha, red, green, blue });

Convert to RGB

int red = 255;
int green = 169;
int blue = 104;

var color = Graphics.Colors.ToRgb("#FFA968");

Convert to ARGB

This can be useful for WPF and XAML which supports an alpha value in the HEX.

int alpha = 255;
int red = 146;
int green = 145;
int blue = 145;

var c = Graphics.Colors.ToArgb("#FF929191");

Math Things

Even or Odd

Even number detection

int value = 2;
var isMyNumberEven = value.IsEvenNumber();

Percentages

Basic percentage calculations

int value = 500;
int total = 2000;

var percent = value.IsWhatPercentageOf(total) // 25

String Distances

Hamming Distance

Calculates the number of edits required to go from one string to another must be equal lengths to start

var inputOne = "InputString1";
var inputTwo = "InputString2";

var distance = Distance.GetHammingDistance(inputOne, inputTwo);
inputOne.HammingDistanceTo(inputTwo)

Levenshtein Distance

Calculates the number of edits required to go from one string to another

var inputOne = "InputString1";
var inputTwo = "InputString2";

var distance = Distance.GetLevenshteinDistance(inputOne, inputTwo);
inputOne.LevenshteinDistanceTo(inputTwo)

Shorten Strings

This method allows you to shorten strings to a predetermined length and pad with ... or any pattern you provide.

string input = "Thisismylongstringthatneedsshortening";
var result = input.DotShortenString(10, 20); // "Thisism...shortening"

string input = "Thisismylongstringthatneedsshortening";
var result = input.DotShortenString(10, 20, ";;;"); // "Thisism;;;shortening"

Remove All Multispaces

var input = "  This  has    too  many  spaces   ";
input.RemoveAllMultiSpace(); // " This has too many spaces "
var input = "  This  has    too  many  spaces   ";
input.RemoveAllMultiSpace(trim:true); // "This has too many spaces"
var input = "  This  has    too  many  spaces   ";
input.RemoveAllMultiSpace("--"); // "--This--has--too--many--spaces--"
var input = "  This  has    too  many  spaces   ";
input.RemoveAllMultiSpace(pattern:"--", trim:true) // "This--has--too--many--spaces"

String Compare

string input = "string1";
string pattern = "strinG1";

input.IsExactlySameAs(pattern); // false

Secure Strings Compare

// #dontdothisinproduction
var input1 = new SecureString();
foreach (char c in "QqNK8f#X4t7lZYomTC#c1rFz9^Xl8rAO!".ToCharArray())
{
    input1.AppendChar(c);
}

// #dontdothisinproduction
var input2 = new SecureString();
foreach (char c in "QqNK8f#X4t7lZYomTC#c1rFz9^Xl8rAO!".ToCharArray())
{
    input2.AppendChar(c);
}

input1.IsExactlySameAs(input2); // true

Serialization

XML Encoding Formatting

Adds a strict uppercased UTF-8 to XML declarations

using (var sw = new UppercaseUtf8StringWriter())
{
    xsSubmit.Serialize(sw, new TestObject { SomeProperty = "SomeValue" });
    xml = sw.ToString();
}

Patterns

Mvvm - ViewModelBase

A very bare bones view model with property changed updates

public abstract class ViewModelBase : INotifyPropertyChanged
{
    ...
    public bool IsEditable ...
    ...
    public bool IsBusy ...
    ...
    protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = "")
    {
        ...
    }
}

Mvvm - SuperObservableCollection<T>

An observable collection that mutes change notifications when adding a range of objects and allows sorting

public class SuperObservableCollection<T> : ObservableCollection<T>
{
    public SuperObservableCollection(IEnumerable<T> coll)
    {
    }

    public void AddRange(IEnumerable<T> list, bool suppressNotifications = true, bool notifiyOnceAllAdded = true)
    {
        ...
    }

    public void Sort(bool suppressNotifications = false)
    {
        ...
    }
	
    public void Sort(IComparer<T> comparer, bool suppressNotifications = false)
    {
        ...
    }
}

Observer

A very basic implementation of the core bits of the observer pattern

public interface IObservable
{
    void Attach(IObserver observer);

    void Detach(IObserver observer);

    void Notify();
}
public interface IObserver
{
    void Update(IObservable observable);
}
public abstract class ObservableBase : IObservable
{
}

Memento

public interface IMemento
{
    object GetState();
}
public interface IMementoOwner
{
    IMemento Save();

    void Restore(IMemento memento);
}
public class MementoManager
{
    ...
}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

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.1 69 6/4/2022
0.4.2 815 4/18/2021
0.4.1 181 4/18/2021
0.4.0 532 6/9/2020
0.3.0 346 5/26/2020
0.2.0 321 5/23/2020
0.1.0 358 5/17/2020