maestrianet - Maestria.Extensions 1.0.4

Extension function pack to increase productivity and improve source code writing.

PM> Install-Package Maestria.Extensions -Version 1.0.4 -Source https://www.myget.org/F/maestrianet/api/v3/index.json

Copy to clipboard

> nuget.exe install Maestria.Extensions -Version 1.0.4 -Source https://www.myget.org/F/maestrianet/api/v3/index.json

Copy to clipboard

> dotnet add package Maestria.Extensions --version 1.0.4 --source https://www.myget.org/F/maestrianet/api/v3/index.json

Copy to clipboard
<PackageReference Include="Maestria.Extensions" Version="1.0.4" />
Copy to clipboard
source https://www.myget.org/F/maestrianet/api/v3/index.json

nuget Maestria.Extensions  ~> 1.0.4
Copy to clipboard

> choco install Maestria.Extensions --version 1.0.4 --source https://www.myget.org/F/maestrianet/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "maestrianet" -SourceLocation "https://www.myget.org/F/maestrianet/api/v2"
Install-Module -Name "Maestria.Extensions" -RequiredVersion "1.0.4" -Repository "maestrianet" 
Copy to clipboard

Maestria.Extensions

Build status NuGet MyGet Apimundo

Build History

donate

What is Maestria.Extensions?

Extension function pack to increase productivity and improve source code writing. By default, all extension methods are safe.

What is Maestria Project?

This library is part of Maestria Project.

Maestria is a project to provide productivity and elegance to your source code writing.

Where can I get it?

First, install NuGet. Then, install Maestria.Extensions from the package manager console:

PM> Install-Package Maestria.Extensions

or install from the dotnet cli command line:

> dotnet add package Maestria.Extensions

How do I get started?

First, import "Maestria.Extensions" reference:

using Maestria.Extensions;

Then in your application code, use fluent syntax

// AggregateExtenions
<Array>.Max();
<Array>.Min();

// Base64Extensions
<byte[]>.ToBase64(<byte[]>)
<string>.ToBase64(<string>, <Encoding>)

var decoded = <byte[]>.FromBase64(<encoded-byte[]>, <Encoding>)
var decoded = <string>.FromBase64(<encoded-string>, <Encoding>)

// CollectionExtensions
<IEnumerable>.IsNullOrEmpty()
<IEnumerable>.HasItems()
<IDictionary>.TryGetValue(<key>, <@default-value>)
<IEnumerable>.Iterate(item => <action>)
await <IEnumerable>.Iterate(async item => await <action>)
<IEnumerable>.Iterate((item, index) => <action>)
await <IEnumerable>.Iterate(async (item, index) => await <action>)

// EnumExtensions
<Enum>.GetDisplayName()
<Enum>.GetDescription()
EnumExtensions.GetValues<TEnum>()
EnumExtensions.GetValues(typeof(<TEnum>))

// ExceptionExtensions
<Exception>.GetAllMessages()
<Exception>.GetMostInner()

// HashExtensions
"value".GetHashMd5()
"value".GetHashSha1()
"value".GetHashSha256()
"value".GetHashSha384()
"value".GetHashSha512()
HashExtensions.ComputeHash(<HashAlgorithm>, "value")

// RoundExtensions
<floating-point>.Round(<digits>)
<floating-point>.Round(<digits>, <MidpointRounding>)
<floating-point>.RoundUp(<digits>)

// TruncateExtensions
<floating-point>.Truncate(<digits>)

// StringExtensions
<string>.TrimStart(<start-comparison>, <ignore-case>)
<string>.TrimEnd(<start-comparison>, <ignore-case>)
<string>.AddToBeginningIfNotStartsWith(<start-comparison>, <ignore-case>)
<string>.AddToBeginningIfHasValue(<start-comparison>, <prefix>)
<string>.AddToEndIfNotEndsWith(<start-comparison>, <ignore-case>)
<string>.AddToEndIfHasValue(<start-comparison>, <prefix>)
<string>.EscapeXml()
<string>.Format(<values-to-format>)
<string>.IsNullOrEmpty()
<string>.IsNullOrWhiteSpace()
<string>.EmptyIf(<string>)
<string>.EmptyIfNull()
<string>.EmptyIfNullOrWhiteSpace()
<string>.HasValue() // Check if text is not null and not white space
<string>.EqualsIgnoreCase(<camparison-value>, <auto-trim>)
<string>.OnlyNumbers()
<string>.RemoveAccents()
<string>.Join(<separator>)
<string>.SubstringBeforeFirstOccurrence("-")
<string>.SubstringBeforeLastOccurrence("-")
<string>.SubstringAfterFirstOccurrence("-")
<string>.SubstringAfterLastOccurrence("-")
<string>.SubstringAtOccurrenceIndex("-", 1)
<string>.SubstringSafe(<start-index>, <length>)
<string>.Truncate(<length>)
<string>.TruncateStart(<length>)
<string>.OnlyLettersOrNumbersOrUnderscoresOrHyphensTest(<string>)
<string>.ToSnakeCase()

// Guid
<Guid>.IsEmpty()
<Guid?>.IsNullOrEmpty()
<string>.IsNullOrWhiteSpace()
<Guid>.IfEmpty(<value>)
<Guid?>.IfNullOrEmpty(<value>)

// SyntaxExtensions
<object>.IsNull()
<object>.HasValue() or <object>.IsNotNull() // Same result
<object>.In(<array-of-values>)
<IComparable>.Between(<starting-value>, <ending-value>)
<IComparable>.LimitMaxAt(<max-value>)
<IComparable>.LimitMinAt(<min-value>)

// Pipelines
var value = <string>
    .OnlyNumbers()
    .DetachedCall(x => Console.WriteLine(x)) // <<< Call a method with current value and continue execution pipeline
    .Format("mask"); // value is only the number of string formatted and only numbers are written on console

<string>
    .OnlyNumbers()
    .OutVar(out var variableToExternalFromScopeAccess) // <<< Create variable with current value on external scope and continua execution pipeline
    .Format("mask"); // value is only the number of string formatted and only numbers are written on console

If fluent expressions

It's possible to execute fluent comparisons expression with the syntax: <value>.IfGreater(<value-to-compare>).Then(<result-if-compare-is-true>).

The methods for comparison operations are: IfGreater,IfGreaterOrEqual, IfLest. IfLessOrEqual,If and IfNot.

Rules:

  • When condition it's false, result the pipeline is <value>.
  • When <value> or <value-to-compare> is null:
    • Result only true if both are null and comparison is equality operation If.
    • When an only value is null, the result is true if the operation is not equality comparison IfNot.
    • When <value> is Nullable<>, <result-if-compare-is-true> always if Nullable<> data type.
    • But when <value> is not Nullable<>, <result-if-compare-is-true> allow use Nullable<> and not Nullable<> data type.
    • All other operations comparisons result in false.
  • It's possible return null value at <result-if-compare-is-true>, but then indicated syntax is <value>.NullIf(<value-to-compare>).

Examples:

<IComparable>.IfGreater(10).Then(5)
<IComparable>.IfGreaterOrEqual(10).Then(5)
<IComparable>.IfLest(10).Then(5)
<IComparable>.IfLessOrEqual(10).Then(5)
<IComparable>.If(10).Then(5)
<IComparable>.IfNot(10).Then(5)

Delegates expression only executes when the condition it's true.

<IComparable>.IfGreater(10).Then(() => 5)
<IComparable>.IfGreaterOrEqual(10).Then(() => 5)
<IComparable>.IfLest(10).Then(() => 5)
<IComparable>.IfLessOrEqual(10).Then(() => 5)
<IComparable>.If(10).Then(() => 5)
<IComparable>.IfNot(10).Then(() => 5)

Other fluent comparison operations:

// IfNullExtensions
<object>.IfNull(<output-value-when-null>)
<string>.IfNullOrEmpty(<output-value-when-null-or-empty-string>)
<string>.IfNullOrWhiteSpace(<output-value-when-null-or-empty-white-space>)

// NullIfExtensions
<object>.NullIf(<comparison-value>)
<floating-point>.NullIf(<comparison-value>, <tolerance-to-comparasion>)

<string>.NullIf(<comparison-value>, <ignore-case>)
<string>.NullIfEmpty(<comparison-value>)
<string>.NullIfWhiteSpace(<comparison-value>)

<IComparable>.NullIfIn(<comparison-value>)
<IComparable>.NullIfBetween(<comparison-value>)

Data Types

ISimpleResult, SimpleResult and SimpleResult< TValue>

This structure has success and message for simple method result, extensible with generic TValue on "Value" property.

SimpleResult ok = SimpleResult.Ok(<optional-message>);
SimpleResult fail = SimpleResult.Fail("Fail message");

// Implict conversions
SimpleResult ok = true;
SimpleResult fail = "Fail message"
SimpleResult fail = new Exception("Fail message")


// Initializer
var result = new SimpleResult
{
    Success = true,
    Message = "Successfully processed"
}

To improve then development experience, there are implicit conversions to assign data from:

  • bool: To set or verify property Success.
  • string: To set fail message and Success to false.
  • Exception: To set fail message, Exception and Success to false.
  • TValue: To set value and Success to true (Even if null).

And implicit conversions to assingn data to:

  • bool:
    • SimpleResult: Get data from property Success.
    • SimpleResult<TValue>: Get data from property Success
  • Exception: Get data from property Exception
  • TValue: Get data from property Value

The property SuccessAndHasValue check if Success == true and Value != null in SimpleResult<TValue>.

Caution on SimpleResult<TValue>: Implicit comparison if (mySimpleResultVariable) is equivalent to if (mySimpleResultVariable.Success).
Use explicit if (mySimpleResultVariable.SuccessAndHasValue) when result value can be null with success is true

Use cases:

public SimpleResult Execute(Args args)
{
    if (args == null)
        return "Argument cannot be null";  // <===== Implicit cast to failure result
    try
    {
        // Execute actions
        return true; // <===== Implicit cast success result
    }
    catch (Exception e)
    {
        return e; // <===== Implicit cast to failure result
    }
}

public SimpleResult<int> Execute2(Args args)
{
    if (args == null)
        return "Argument cannot be null";  // <===== Implicit cast to failure result
    try
    {
        // Execute actions
        return 10; // <===== Implicit cast success result
    }
    catch (Exception e)
    {
        return e; // <===== Implicit cast to failure result
    }
}

// ...

var result = Execute(...);
var result2 = Execute2(...);

if (result && result2) // <===== Implicit cast to boolean
{
    // ...
}


Try<TSuccess, TFailure>

Auxiliary data type to increment the expressibility of method results when success and failure need different structures.
To facilitate development there is support for implicit conversion.

public void Try<PersonCreated, CustomError> Save(Person value) 
{
    ...
    if (condition)
        return new CustomError() { Code = 999, Message = "Failure message" }
    return new PersonCreated { Id = 123 };
}

...

var result = Save(person);
if (result)
    Console.WriteLine(result.Value.Id);
else
    Console.WriteLine($"Error {result.Failure.Code}: {result.Failure.Message}");

Settings

It's possible set default settings for library:

Extensions.GlobalSettings.Configure(cfg => cfg
    .FloatAndDoubleTolerance(default-float-and-double-comparasion-tolerance) // Default is 0.00001f

Development Guidelines

  • Create file with extensions method name
  • this argument name must be value for single object or values to IEnumerable inheritances
  • Put new file into folder /src/Extensions/<extended-data-type>
    • Expressions methods like If, NullIf, Is, In, EmptyIf, Between, must be located in src/Extensions/Comparable folder
    • Numbers extensions methods must bet located in src/Extensions/Number
  • The I prefix for interfaces must be omitted from the folder name.
  • src\Settings\MaestriaExtensionSettings: File to configure global defaults behaviors
  • String extensions methods should use nullable input and non-nullable output.

Breaking Changes

Breaking-Changes.md


buy-me-a-coffee smile.png

If my contributions helped you, please help me buy a coffee :D

donate

  • .NETStandard 2.0
    • JetBrains.Annotations (>= 11.1.0)
    • System.ComponentModel.Annotations (>= 4.5.0)
  • .NETStandard 2.0: 2.0.0.0

Owners

Fábio Naspolini

Authors

Fábio Monteiro Naspolini

Project URL

https://github.com/MaestriaNet/Extensions

License

Unknown

Tags

Extension Extensions Fluent Functional Elegance Productivity Maestria Mastery IEnumerable Enumerable ICollection Collection IList List Array Max Min Highest Smallest Largest String Text IfNull NullIf Null If In Between Truncate Round Hash MD5 SHA1 Enum Aggreagate Exception StackTrace Reflection

Info

0 total downloads
0 downloads for version 1.0.4
Download (12.82 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
3.4.2 52.84 KB Wed, 09 Nov 2022 02:14:02 GMT 0
3.4.1 52.67 KB Wed, 09 Nov 2022 01:07:38 GMT 0
3.4.0 52.51 KB Wed, 09 Nov 2022 00:39:54 GMT 0
3.3.2 29.68 KB Fri, 07 Oct 2022 02:45:46 GMT 0
3.3.1 29.64 KB Mon, 12 Sep 2022 22:13:58 GMT 0
3.3.0 29.63 KB Mon, 12 Sep 2022 04:08:47 GMT 0
3.2.3 29.36 KB Sat, 27 Aug 2022 04:46:00 GMT 0
3.2.2 27.13 KB Sat, 27 Aug 2022 03:54:13 GMT 0
3.2.1 27.13 KB Fri, 26 Aug 2022 16:06:23 GMT 0
3.2.0 22.23 KB Tue, 31 May 2022 03:44:42 GMT 0
3.1.3 22.23 KB Tue, 31 May 2022 03:43:55 GMT 0
3.1.2 22.11 KB Wed, 03 Nov 2021 00:13:53 GMT 0
3.1.1 22.11 KB Wed, 03 Nov 2021 00:13:16 GMT 0
3.1.0 22.01 KB Sun, 12 Sep 2021 23:02:36 GMT 0
3.0.2 21.89 KB Wed, 08 Sep 2021 22:06:29 GMT 0
3.0.1 22.25 KB Sat, 07 Aug 2021 03:58:59 GMT 0
3.0.0 22.26 KB Wed, 04 Aug 2021 03:24:40 GMT 0
2.1.4 22.19 KB Mon, 05 Jul 2021 04:09:39 GMT 0
2.1.3 22.2 KB Tue, 22 Jun 2021 01:11:26 GMT 0
2.1.2 21.97 KB Mon, 21 Jun 2021 03:39:03 GMT 0
2.1.1 21.54 KB Mon, 07 Jun 2021 00:31:25 GMT 0
2.1.0 17.69 KB Fri, 04 Jun 2021 01:21:40 GMT 0
2.0.13 17.4 KB Thu, 03 Jun 2021 23:42:27 GMT 0
2.0.12 15.73 KB Tue, 25 May 2021 20:18:10 GMT 0
2.0.11 15.7 KB Fri, 21 May 2021 19:27:28 GMT 0
2.0.10 15.71 KB Sun, 16 May 2021 18:13:02 GMT 0
2.0.9 15.19 KB Fri, 14 May 2021 03:04:13 GMT 0
2.0.8 15.13 KB Sun, 04 Apr 2021 22:17:20 GMT 0
2.0.7 15.08 KB Sat, 03 Apr 2021 19:30:12 GMT 0
2.0.6 15.01 KB Fri, 02 Apr 2021 23:07:54 GMT 0
2.0.5 14.94 KB Sun, 28 Mar 2021 23:37:19 GMT 0
2.0.4 14.79 KB Sun, 28 Mar 2021 20:25:09 GMT 0
2.0.3 14.79 KB Sat, 27 Mar 2021 22:28:58 GMT 0
2.0.2 14.77 KB Sat, 27 Mar 2021 20:42:54 GMT 0
2.0.1 14.32 KB Fri, 19 Mar 2021 22:02:32 GMT 0
2.0.0 14.3 KB Fri, 19 Mar 2021 03:53:07 GMT 0
1.2.5 14.57 KB Sun, 14 Mar 2021 03:25:47 GMT 0
1.2.4 14.55 KB Sun, 10 Jan 2021 22:43:29 GMT 0
1.2.3 14.47 KB Sun, 10 Jan 2021 22:23:26 GMT 0
1.2.2 14.33 KB Tue, 01 Sep 2020 21:02:26 GMT 0
1.2.1 14.28 KB Tue, 18 Aug 2020 00:42:25 GMT 0
1.2.0 14.28 KB Sun, 09 Aug 2020 04:20:35 GMT 0
1.1.1 13.06 KB Wed, 05 Aug 2020 13:50:00 GMT 0
1.1.0 12.8 KB Sun, 12 Jul 2020 20:27:08 GMT 0
1.0.10 12.62 KB Sun, 19 Apr 2020 01:29:34 GMT 0
1.0.9 12.57 KB Tue, 14 Apr 2020 02:20:28 GMT 0
1.0.8 12.5 KB Tue, 14 Apr 2020 02:04:15 GMT 0
1.0.7 12.42 KB Sat, 11 Apr 2020 22:08:50 GMT 0
1.0.6 12.42 KB Sat, 11 Apr 2020 22:07:15 GMT 0
1.0.5 13.78 KB Fri, 20 Dec 2019 01:02:18 GMT 0
1.0.4 12.82 KB Mon, 16 Dec 2019 00:34:21 GMT 0
1.0.3 13.39 KB Sun, 15 Dec 2019 06:08:32 GMT 0
1.0.2 13.16 KB Mon, 25 Nov 2019 04:32:25 GMT 0
1.0.1 13.2 KB Mon, 18 Nov 2019 03:29:44 GMT 0
1.0.0 12.76 KB Thu, 10 Oct 2019 16:30:25 GMT 0