JamSoft.Helpers 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package JamSoft.Helpers --version 1.0.1                
NuGet\Install-Package JamSoft.Helpers -Version 1.0.1                
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="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                
#r "nuget: JamSoft.Helpers, 1.0.1"                
#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.
// 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                

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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on JamSoft.Helpers:

Package Downloads
JamSoft.Helpers.AvaloniaUI

A collection of general helpers for AvaloniaUI applications.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.2 164 6/23/2024
1.3.1 147 6/23/2024 1.3.1 is deprecated because it has critical bugs.
1.3.0 503 5/1/2023
1.2.4 365 3/4/2023
1.2.3 394 2/22/2023
1.2.2 276 2/21/2023
1.2.1 278 2/20/2023
1.2.0 290 2/19/2023
1.1.0 275 2/18/2023
1.0.2 423 8/27/2022
1.0.1 452 6/4/2022
0.4.2 1,351 4/18/2021 0.4.2 is deprecated because it is no longer maintained.
0.1.0 942 5/17/2020 0.1.0 is deprecated because it is no longer maintained.