This is a project full of utility methods that can be used across multiple projects.
The code is:
- Thoroughly documented
- Thoroughly tested
- Comprehensively null-annotated
- Includes SourceLink so you can step into the source to debug
- Methods that are side-effect free are marked as
[Pure]
Installation
This library can be installed from Nuget or Github Packages. It targets netstandard 2.1, and will require a version of JSON.NET installed in the 11.x
range.
Nuget
You can install it from nuget by running dotnet add package SCM.SwissArmyKnife
Github
If you are developing the library or want the latest packages built from the main
branch, you can get them from Github packages.
- Add a
nuget.config
file to the root of your project. <?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="github" value="https://nuget.pkg.github.com/SCADAMINDS/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="USERNAME" />
<add key="ClearTextPassword" value="TOKEN" />
</github>
</packageSourceCredentials>
</configuration>
- Replace USERNAME with your Github username and TOKEN with a personal access token.
- Add the package:
dotnet add package SCM.SwissArmyKnife --version {VERSION} --prerelease
SwissArmyKnife.Extensions
A bunch of handy extension methods that you'll probably like to use! Examples:
// ------------- Dictionary.GetOr
myDictionary.GetOr("nonExistingKey", () => "myFallbackValue");
// ------------- Task.Select()
var enumerableTask = Task.FromResult(new int[]{1,2});
// Select to transform multiple values
// Alternative to (await enumerableTask).Select(i => i + 1)
await enumerableTask.Select(i => i + 1); // Returns [2,3]
// ------------- object.Yield
// Produce an Enumerable out of an item
int myItem = 3;
// Type: IEnumerable<int>
var myEnumerable = myItem.Yield();
// ------------- HttpClient.GetAsJsonAsync()
var client = new HttpClient();
var url = "http://www.some-url-that-produces-json.com"
// Gets URL and serializes model to MyResponseModel. On error prints http response
var await response = client.GetAsJsonAsync<MyResponseModel>(url)
And many more!
SwissArmyKnife.Files
Methods to operate on files:
DirectoryInfo solutionDir = FileLocator.TryGetSolutionDirectoryInfo();
FileInfo programFile = FileLocator.GetFileStartingAtSolutionDirectory("SourceProject", "Program.cs");
And more!
SwissArmyKnife.Compression
Methods to compress and decompress data using gzip:
byte[] myCompressedByteArray = Gzip.Compress(myByteArray);
byte[] myCompressedStringASCII = Gzip.Compress(myString, Encoding.ASCII);
byte[] myCompressedStringUTF8 = Gzip.Compress(myString);
byte[] myDecompressedByteArray = Gzip.Decompress(myCompressedByteArray);
string myDecompressedStringASCII = Gzip.DecompressToString(myCompressedStringASCII, Encoding.ASCII);
string myDecompressedStringUTF8 = Gzip.DecompressToString(myCompressedStringUTF8);
And more!
SwissArmyKnife.TestUtils
Utilities to make testing a little bit easier.
// Create a temporary file. The file is deleted when the block ends.
using var temporaryFile = TemporaryFileFixture.Create();
FileStream fileStream = temporaryFile.FileInfo.OpenWrite();
// Create a temporary directory, and delete it recursively at the end of the block.
using var temporaryDirectory = TemporaryDirectoryFixture.Create();
var directoryPath = temporaryDirectory.DirectoryInfo.FullName
// Create a dictionary that always returns the same value: "defaultValue" in this case.
var dictionary = new SameValueDictionary<string, string>("defaultValue");
// Add operations are no-op
dictionary["someKey"] = "foo";
// Returns "defaultValue"
var value = dictionary["someKey"];
And more!
Documentation
You can view the documentation for the main
branch here.
Contributing
If you'd like to contribute view the contribution docs here