Trivial 2.0.2

A library providing lots of useful utilities, models and services, including hit task, retry policy, PEM, JWT, stream combination, JSON HTTP web client, CSV parser, numerals, coordinates, angle, arithmetic, sets, etc.

Install-Package Trivial -Version 2.0.2
dotnet add package Trivial --version 2.0.2
<PackageReference Include="Trivial" Version="2.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Trivial --version 2.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Trivial

This library includes utilities and services for tasks, IO, security, etc.

Tasks

Just add following namespace to your code file to use.

using Trivial.Tasks;

Hit task

You can create a task controller to manage when a handler should be raised.

  • HitTask.Debounce:
    You may request to call a specific action several times in a short time but only the last one should be processed and previous ones should be ignored.
    A sample is real-time search suggestion.
  • HitTask.Throttle:
    You may want to request to call an action only once in a short time even if you request to call several times.
    A sample is the submit button in a form.
  • HitTask.Times:
    You can define an action can be only processed only when request to call in the specific times range and others will be ignored.
    A sample is double click.
  • HitTask.Multiple:
    A handler to process for the specific times and the state will be reset after a while.

Following is an example for debounce.

// An action you need to process.
Action action = () => {
    // Do something...
};

// Set up when the action can be processed.
var task = HitTask.Debounce(action, TimeSpan.FromMilliseconds(200));

// Somewhere to raise.
task.ProcessAsync();

Retry

You can create a linear retry policy or even a customized to process an action with the specific retry policy.
And you can use ObservableTask to observe the state of an action processing.

Mathematics

Just add following namespace to your code file to use.

using Trivial.Maths;

Arithmetic

There a lot of arithmetic functions.

Arithmetic.IsPrime(2147483647); // True
await Arithmetic.IsPrimeAsync(2305843009213693951); // False

Arithmetic.Gcd(192, 128); // 64
Arithmetic.Lcm(192, 128); // 384

Numerals

You can get the number symbols as you want.

You can also get the number string in English words.

EnglishNumerals.Default.ToString(12345.67);
// twelve thousand three hundred and forty-five point six seven

EnglishNumerals.Default.ToApproximationString(1234567);
// 1.2M

And ChineseNumerals for Chinese and JapaneseNumerals for Japanese.

Angle and polar point

  • Angle Angle.
  • PolarPoint The point in polar coordinates.
  • SphericalPoint The point in spherical coordinates.

Set

  • NullableValueSimpleInterval&lt;T&gt; Interval, such as [20, 100).

Rectangular coordinates

  • OneDimensionalPoint The point in 1D (line) coordinates.
  • TwoDimensionalPoint The point in 2D (flat) coordinates.
  • ThreeDimensionalPoint The point in 3D (stereoscophic) coordinates.
  • FourDimensionalPoint The point in 4D (spacetime) coordinates.

Network

Contains the helper functions and extension functions for network, such as HTTP web client and its content.

using Trivial.Net;

And you can also use JsonHttpClient to serialize the JSON format response with retry policy supports.
And HttpUri for HTTP URI fields accessing.

Security

Just add following namespace to your code file to use.

using Trivial.Security;

RSA

You can convert a PEM (OpenSSL RSA key) or an XML string to the RSAParametersConvert class.

var parameters = RSAParametersConvert.Parse(pem);

And you can convert back by using the extension method ToPrivatePEMString or ToPublicPEMString.
And also you can use the extension method ToXElement to export the XML.

Symmetric

You can encrypt and decrypt a string by symmetric algorithm.

// AES sample.
var original = "Original secret string";
var cipher = SymmetricUtilities.Encrypt(Aes.Create, original, key, iv);
var back = SymmetricUtilities.DecryptText(Aes.Create, cipher, key, iv); // back == original

Hash

For hash algorithm, you can call HashUtilities.ToHashString function to get hash from a plain string and call HashUtilities.Verify to verify.

var original = "The string to hash";

// SHA-512 (of SHA-2 family)
var sha512Str = HashUtilities.ToHashString(SHA512.Create, original);
var isVerified = HashUtilities.Verify(SHA512.Create, original, sha512Str); // --> true

// SHA-3-512
var sha3512Str = HashUtilities.ToHashString(new HashAlgorithmName("SHA3512"), original);
var isVerified3512 = HashUtilities.Verify(sha3512Name, original, sha3512Str); // --> true

Access token

We also provide a set of tools for OAuth including following models.

  • TokenInfo The access token and other properties.
  • AppAccessingKey The app identifier and secret key.
  • OAuthClient The token container with the ability to resolve the access token and create the JSON HTTP web client to access the resources required authentication.

And you can also implement the OAuthBasedClient base class to create your own business HTTP web client factory with OAuth supports.

JWT

You can create a JSON web token to get the string encoded by initializing a new instance of the JsonWebToken class or the JsonWebTokenParser class.

var sign = HashSignatureProvider.CreateHS512("a secret string");
var jwt = new JsonWebToken<Model>(new Model(), sign);
var jwtStr = jwt.ToEncodedString();

// Get authenticiation header value.
var header = jwt.ToAuthenticationHeaderValue();

// Parse.
var jwtSame = JsonWebToken<Model>.Parse(jwtStr, sign); // jwtSame.ToEncodedString() == jwtStr

Secure string utiltiy

You can use the extension methods in the SecureStringExtensions class to convert the secret between SecureString and String/StringBuilder/Byte[].

You can also use the class RSASecretExchange to transfer the secret with RSA encryption.

Data

Contains lots of data readers, parsers and utilities.

using Trivial.Data;

Data cache collection

You can create a collection to cache data with expiration and count limitation by following way.

var cache = new DataCacheCollection<Model>
{
    MaxCount = 1000,
    Expiration = TimeSpan.FromSeconds(100)
};

So that you can get the data from the cache if has and or add new one if necessary.

if (!cache.TryGet("abcd", out Model item)) item = new Model();

Text

Contains the text utilities.

using Trivial.Text;

CSV

You can read CSV file into a list of the specific models. For example, you have a model like following.

class Model
{
    public string A { get; set; }
    public string B { get; set; }
}

Now you can map to the CSV file.

var csv = new CsvParser("abcd,efg\nhijk,lmn");
foreach (var model in csv.ConvertTo<Model>(new[] { "A", "B" }))
{
    Console.WriteLine("{0},{1}", model.A, model.B);
}

Further

Trivial

This library includes utilities and services for tasks, IO, security, etc.

Tasks

Just add following namespace to your code file to use.

using Trivial.Tasks;

Hit task

You can create a task controller to manage when a handler should be raised.

  • HitTask.Debounce:
    You may request to call a specific action several times in a short time but only the last one should be processed and previous ones should be ignored.
    A sample is real-time search suggestion.
  • HitTask.Throttle:
    You may want to request to call an action only once in a short time even if you request to call several times.
    A sample is the submit button in a form.
  • HitTask.Times:
    You can define an action can be only processed only when request to call in the specific times range and others will be ignored.
    A sample is double click.
  • HitTask.Multiple:
    A handler to process for the specific times and the state will be reset after a while.

Following is an example for debounce.

// An action you need to process.
Action action = () => {
    // Do something...
};

// Set up when the action can be processed.
var task = HitTask.Debounce(action, TimeSpan.FromMilliseconds(200));

// Somewhere to raise.
task.ProcessAsync();

Retry

You can create a linear retry policy or even a customized to process an action with the specific retry policy.
And you can use ObservableTask to observe the state of an action processing.

Mathematics

Just add following namespace to your code file to use.

using Trivial.Maths;

Arithmetic

There a lot of arithmetic functions.

Arithmetic.IsPrime(2147483647); // True
await Arithmetic.IsPrimeAsync(2305843009213693951); // False

Arithmetic.Gcd(192, 128); // 64
Arithmetic.Lcm(192, 128); // 384

Numerals

You can get the number symbols as you want.

You can also get the number string in English words.

EnglishNumerals.Default.ToString(12345.67);
// twelve thousand three hundred and forty-five point six seven

EnglishNumerals.Default.ToApproximationString(1234567);
// 1.2M

And ChineseNumerals for Chinese and JapaneseNumerals for Japanese.

Angle and polar point

  • Angle Angle.
  • PolarPoint The point in polar coordinates.
  • SphericalPoint The point in spherical coordinates.

Set

  • NullableValueSimpleInterval&lt;T&gt; Interval, such as [20, 100).

Rectangular coordinates

  • OneDimensionalPoint The point in 1D (line) coordinates.
  • TwoDimensionalPoint The point in 2D (flat) coordinates.
  • ThreeDimensionalPoint The point in 3D (stereoscophic) coordinates.
  • FourDimensionalPoint The point in 4D (spacetime) coordinates.

Network

Contains the helper functions and extension functions for network, such as HTTP web client and its content.

using Trivial.Net;

And you can also use JsonHttpClient to serialize the JSON format response with retry policy supports.
And HttpUri for HTTP URI fields accessing.

Security

Just add following namespace to your code file to use.

using Trivial.Security;

RSA

You can convert a PEM (OpenSSL RSA key) or an XML string to the RSAParametersConvert class.

var parameters = RSAParametersConvert.Parse(pem);

And you can convert back by using the extension method ToPrivatePEMString or ToPublicPEMString.
And also you can use the extension method ToXElement to export the XML.

Symmetric

You can encrypt and decrypt a string by symmetric algorithm.

// AES sample.
var original = "Original secret string";
var cipher = SymmetricUtilities.Encrypt(Aes.Create, original, key, iv);
var back = SymmetricUtilities.DecryptText(Aes.Create, cipher, key, iv); // back == original

Hash

For hash algorithm, you can call HashUtilities.ToHashString function to get hash from a plain string and call HashUtilities.Verify to verify.

var original = "The string to hash";

// SHA-512 (of SHA-2 family)
var sha512Str = HashUtilities.ToHashString(SHA512.Create, original);
var isVerified = HashUtilities.Verify(SHA512.Create, original, sha512Str); // --> true

// SHA-3-512
var sha3512Str = HashUtilities.ToHashString(new HashAlgorithmName("SHA3512"), original);
var isVerified3512 = HashUtilities.Verify(sha3512Name, original, sha3512Str); // --> true

Access token

We also provide a set of tools for OAuth including following models.

  • TokenInfo The access token and other properties.
  • AppAccessingKey The app identifier and secret key.
  • OAuthClient The token container with the ability to resolve the access token and create the JSON HTTP web client to access the resources required authentication.

And you can also implement the OAuthBasedClient base class to create your own business HTTP web client factory with OAuth supports.

JWT

You can create a JSON web token to get the string encoded by initializing a new instance of the JsonWebToken class or the JsonWebTokenParser class.

var sign = HashSignatureProvider.CreateHS512("a secret string");
var jwt = new JsonWebToken<Model>(new Model(), sign);
var jwtStr = jwt.ToEncodedString();

// Get authenticiation header value.
var header = jwt.ToAuthenticationHeaderValue();

// Parse.
var jwtSame = JsonWebToken<Model>.Parse(jwtStr, sign); // jwtSame.ToEncodedString() == jwtStr

Secure string utiltiy

You can use the extension methods in the SecureStringExtensions class to convert the secret between SecureString and String/StringBuilder/Byte[].

You can also use the class RSASecretExchange to transfer the secret with RSA encryption.

Data

Contains lots of data readers, parsers and utilities.

using Trivial.Data;

Data cache collection

You can create a collection to cache data with expiration and count limitation by following way.

var cache = new DataCacheCollection<Model>
{
    MaxCount = 1000,
    Expiration = TimeSpan.FromSeconds(100)
};

So that you can get the data from the cache if has and or add new one if necessary.

if (!cache.TryGet("abcd", out Model item)) item = new Model();

Text

Contains the text utilities.

using Trivial.Text;

CSV

You can read CSV file into a list of the specific models. For example, you have a model like following.

class Model
{
    public string A { get; set; }
    public string B { get; set; }
}

Now you can map to the CSV file.

var csv = new CsvParser("abcd,efg\nhijk,lmn");
foreach (var model in csv.ConvertTo<Model>(new[] { "A", "B" }))
{
    Console.WriteLine("{0},{1}", model.A, model.B);
}

Further

  • .NETStandard 2.0

    • No dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.0.2 41 8/31/2019
1.2.0 419 5/20/2019
1.1.0 161 5/8/2019
1.0.0 140 5/1/2019