Trivial
A library providing lots of useful utilities, models and services, including interceptor, retry policy, writable JSON DOM, PEM, JWT, stream combination, JSON HTTP web client, JSON converters, CSV parser, numerals, coordinates, angle, arithmetic, sets, colors, etc.
Install
| Product | Versions |
|---|---|
| .NET | |
| Other |
README
Trivial
Includes utilities and services for tasks, security, JSON, etc.
Tasks
using Trivial.Tasks;
Interceptor
Set an action with a specific interceptor to control its execution.
Interceptor.Debounce: Request to invoke a specific action several times in a short time but only the last one should be executed and previous ones should be ignored. A sample is real-time search suggestion.Interceptor.Throttle: Raise actually only once in a short time even if invoke several times. A sample is the submit button in a form.Interceptor.Times: The action can be only executed only when invoke in the specific times range and others will be ignored. A sample is double click.Interceptor.Multiple: A handler to execute for the specific times and the state will be reset after a while.
// Set an action with interceptor.
var action = Interceptor.Debounce(() =>
{
// Do something...
}, TimeSpan.FromMilliseconds(200));
// Invoke somewhere.
action();
Retry
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;
Use JsonHttpClient to serialize the JSON format response with additional retry policy.
And HttpUri for HTTP URI fields accessing.
Security
using Trivial.Security;
Access token
Provide a set of tools for OAuth including following models.
TokenInfoThe access token and other properties.AppAccessingKeyThe app identifier and secret key.OAuthClientThe token container with the ability to resolve the access token and create the JSON HTTP web client to access the resources required authentication.
Optional to implement the OAuthBasedClient base class to create your own business HTTP web client factory with OAuth supports.
JWT
Create a JSON web token to get the string encoded by initializing a new instance of the JsonWebToken class or the JsonWebTokenParser class.
var jwt = new JsonWebTokenPayload
{
Id = Guid.NewGuid().ToString("n"),
Issuer = "example"
} + HashSignatureProvider.CreateHS512("a secret string");
// Get authenticiation header value.
var header = jwt.ToAuthenticationHeaderValue();
// Parse.
var jwtSame = JsonWebToken<Model>.Parse(jwtStr, sign); // jwtSame.ToEncodedString() == header.Parameter
RSA
Convert a PEM (OpenSSL RSA key) or an XML string to the RSAParameters struct.
var parameters = RSAParametersConvert.Parse(pem);
And convert back by extension method ToPrivatePEMString or ToPublicPEMString.
Or to XML by extension method ToXElement.
Symmetric & Hash
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.
Secure string
Convert the secret between SecureString and String/StringBuilder/Byte[] by the SecureStringExtensions class.
And class RSASecretExchange is used to transfer the secret by RSA encryption.
Text
using Trivial.Text;
JSON
Includes writable JSON DOM JsonObjectNode and JsonArrayNode.
And includes lots of useful converter like following.
JsonJavaScriptTicksConverterand its fallback converters to convertDateTimefrom/to JavaScript ticks number in JSON.JsonUnixTimestampConverterand its fallback converters to convertDateTimefrom/to Unix timestamp number in JSON.JsonNumberConverterandJsonNumberConverter.NumberStringConverter, to read number string in JSON.JsonStringListConverterand its character separated converters (such asJsonStringListConverter.WhiteSpaceSeparatedConverter) to convert a string list from/to a string in JSON.
CSV
Read CSV or TSV 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
Save a number of model in memory cache by generic class DataCacheCollection.
Drawing
using Trivial.Drawing;
Color calculator
Color adjustment, converter, parser and mixer.
var color = ColorCalculator.Parse("rgb(226, 37, 0xA8)");
color = ColorCalculator.Opacity(color, 0.9);
color = ColorCalculator.Saturate(color, RelativeSaturationLevels.High);
color = ColorCalculator.Mix(ColorMixTypes.Lighten, color, Color.FromArgb(0, 240, 0));
Mathematics
using Trivial.Maths;
Arithmetic
There are a lot of arithmetic functions.
Arithmetic.IsPrime(2147483647); // True
Arithmetic.Gcd(192, 128); // 64
Numerals
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
AngleAngle.PolarPointThe point in polar coordinates.SphericalPointThe point in spherical coordinates.
Set
NullableValueSimpleInterval<T>Interval, such as[20, 100).
Rectangular coordinates
IntPoint1DDoublePoint1DThe point in 1D (line) coordinates.IntPoint2DDoublePoint2DThe point in 2D (flat) coordinates.IntPoint3DDoublePoint3DThe point in 3D (stereoscophic) coordinates.SpacetimePointThe point in 4D (spacetime) coordinates.
Further
Changes from Previous Version
Adoption Assessment
Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.