Trivial 3.4.2

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

Suggested Alternatives

Trivial 3.5.2

Additional Details

Please update to the latest version.

There is a newer version of this package available.
See the version list below for details.
Install-Package Trivial -Version 3.4.2
dotnet add package Trivial --version 3.4.2
<PackageReference Include="Trivial" Version="3.4.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Trivial --version 3.4.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, security, JSON, etc.

Tasks

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.

// Create a task.
var task = HitTask.Debounce(() =>
{
    // Do something...
}, TimeSpan.FromMilliseconds(200));

// Raise somewhere.
task.ProcessAsync();

Retry

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

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

using Trivial.Security;

RSA

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

var parameters = RSAParametersConvert.Parse(pem);

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

Symmetric & Hash

You can use a symmetric algorithm to encrypt and decrypt a string by calling SymmetricUtilities.Encrypt and SymmetricUtilities.DecryptText functions.

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

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<JsonWebTokenPayload>(new JsonWebTokenPayload
{
    Id = Guid.NewGuid().ToString("n"),
    Issuer = "example"
}, sign);

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

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

Secure string

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.

Text

using Trivial.Text;

JSON

Includes writable JSON DOM JsonObject and JsonArray.
And includes lots of useful converter like following.

  • JsonJavaScriptTicksConverter, and its nullable value conveters and fallback converters, to convert DateTime or DateTime? from/to JavaScript ticks number in JSON.
  • JsonUnixTimestampConverter, and its nullable value conveters and fallback converters, to convert DateTime or DateTime? from/to Unix timestamp number in JSON.
  • JsonNumberConverter and JsonNumberConverter.NumberStringConverter, to read number string in JSON.
  • JsonStringListConverter and its character separated converters (such as JsonStringListConverter.WhiteSpaceSeparatedConverter), to convert a string list from/to a string in JSON.
  • JsonObjectConverter, to convert JsonObject and JsonArray.

CSV

You can read CSV file into a list of the specific models.
For example, you have a model class CsvModel with string properties A and B, now you can map to the CSV file.

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

Data

using Trivial.Data;

Cache

You can save a number of model in memory cache by generic class DataCacheCollection.

Mathematics

using Trivial.Maths;

Arithmetic

There a lot of arithmetic functions.

Arithmetic.IsPrime(2147483647); // True
Arithmetic.Gcd(192, 128); // 64

Numerals

You can get the number symbols as you want. And get the numerals in English.

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.

Further

Trivial

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

Tasks

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.

// Create a task.
var task = HitTask.Debounce(() =>
{
    // Do something...
}, TimeSpan.FromMilliseconds(200));

// Raise somewhere.
task.ProcessAsync();

Retry

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

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

using Trivial.Security;

RSA

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

var parameters = RSAParametersConvert.Parse(pem);

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

Symmetric & Hash

You can use a symmetric algorithm to encrypt and decrypt a string by calling SymmetricUtilities.Encrypt and SymmetricUtilities.DecryptText functions.

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

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<JsonWebTokenPayload>(new JsonWebTokenPayload
{
    Id = Guid.NewGuid().ToString("n"),
    Issuer = "example"
}, sign);

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

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

Secure string

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.

Text

using Trivial.Text;

JSON

Includes writable JSON DOM JsonObject and JsonArray.
And includes lots of useful converter like following.

  • JsonJavaScriptTicksConverter, and its nullable value conveters and fallback converters, to convert DateTime or DateTime? from/to JavaScript ticks number in JSON.
  • JsonUnixTimestampConverter, and its nullable value conveters and fallback converters, to convert DateTime or DateTime? from/to Unix timestamp number in JSON.
  • JsonNumberConverter and JsonNumberConverter.NumberStringConverter, to read number string in JSON.
  • JsonStringListConverter and its character separated converters (such as JsonStringListConverter.WhiteSpaceSeparatedConverter), to convert a string list from/to a string in JSON.
  • JsonObjectConverter, to convert JsonObject and JsonArray.

CSV

You can read CSV file into a list of the specific models.
For example, you have a model class CsvModel with string properties A and B, now you can map to the CSV file.

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

Data

using Trivial.Data;

Cache

You can save a number of model in memory cache by generic class DataCacheCollection.

Mathematics

using Trivial.Maths;

Arithmetic

There a lot of arithmetic functions.

Arithmetic.IsPrime(2147483647); // True
Arithmetic.Gcd(192, 128); // 64

Numerals

You can get the number symbols as you want. And get the numerals in English.

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.

Further

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.6.0 45 11/12/2020
3.6.0-preview6 72 9/29/2020
3.6.0-preview5 117 9/1/2020
3.6.0-preview4 127 8/30/2020
3.6.0-preview3 44 8/27/2020
3.6.0-preview2 74 8/26/2020
3.6.0-preview1 102 5/12/2020
3.5.2 1,638 4/13/2020
3.5.1 231 4/12/2020
3.5.0 216 4/1/2020
3.4.3 228 3/20/2020
3.4.2 209 3/12/2020
3.4.1 237 2/25/2020
3.4.0 317 2/20/2020
3.3.0 328 2/14/2020
3.2.0 568 1/22/2020
3.1.0 604 12/30/2019
3.0.1 457 12/26/2019
2.2.0 216 2/19/2020
2.1.0 304 12/24/2019
2.0.3 337 12/3/2019
1.2.0 878 5/20/2019
1.1.0 565 5/8/2019
1.0.0 373 5/1/2019
Show less