maestrianet - Maestria.Extensions 2.0.5

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

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

Copy to clipboard

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

Copy to clipboard

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

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

nuget Maestria.Extensions  ~> 2.0.5
Copy to clipboard

> choco install Maestria.Extensions --version 2.0.5 --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 "2.0.5" -Repository "maestrianet" 
Copy to clipboard

Maestria.Extensions

NuGet Version NuGet Downloads Apimundo

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

Documentations

See additional documentations at docs folder.


buy-me-a-coffee smile.png

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

donate

  • .NETStandard 2.0
    • JetBrains.Annotations (>= 2020.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

Info

3943 total downloads
72 downloads for version 2.0.5
Download (14.94 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
3.7.2 141.46 KB Wed, 12 Nov 2025 04:26:21 GMT 2
3.7.2-preview-18 141.47 KB Wed, 12 Nov 2025 04:20:41 GMT 3
3.7.1 141.45 KB Wed, 12 Nov 2025 04:15:05 GMT 1
3.7.1-preview-16 141.47 KB Wed, 12 Nov 2025 04:12:58 GMT 5
3.7.0 141.43 KB Wed, 12 Nov 2025 04:06:54 GMT 3
3.7.0-preview-14 141.45 KB Wed, 12 Nov 2025 04:03:17 GMT 1
3.6.7 53.59 KB Tue, 11 Nov 2025 04:15:08 GMT 1
3.6.7-preview-10 53.61 KB Tue, 11 Nov 2025 04:12:50 GMT 4
3.6.6 53.59 KB Tue, 11 Nov 2025 03:55:05 GMT 2
3.6.6-preview-6 53.6 KB Tue, 11 Nov 2025 03:52:20 GMT 2
3.6.6-preview-5 53.61 KB Tue, 11 Nov 2025 03:25:05 GMT 3
3.6.6-preview-4 53.6 KB Tue, 11 Nov 2025 03:23:40 GMT 1
3.6.6-preview-3 53.61 KB Tue, 11 Nov 2025 03:18:56 GMT 0
3.6.5 53.59 KB Tue, 11 Nov 2025 03:15:40 GMT 1
3.6.5-preview-1 53.61 KB Tue, 11 Nov 2025 03:14:39 GMT 5
3.6.4 53.59 KB Mon, 10 Nov 2025 05:51:29 GMT 1
3.6.4-preview-55 53.61 KB Mon, 10 Nov 2025 05:49:20 GMT 4
3.6.3 53.62 KB Mon, 10 Nov 2025 05:27:43 GMT 2
3.6.3-preview-52 53.64 KB Mon, 10 Nov 2025 05:26:32 GMT 1
3.6.3-preview-50 53.62 KB Mon, 10 Nov 2025 05:24:32 GMT 3
3.6.3-preview-48 53.62 KB Mon, 10 Nov 2025 05:21:48 GMT 1
3.6.3-preview-46 53.62 KB Mon, 10 Nov 2025 05:04:19 GMT 6
3.6.2 53.6 KB Mon, 10 Nov 2025 04:40:53 GMT 3
3.6.2-preview-44 53.62 KB Mon, 10 Nov 2025 04:39:55 GMT 2
3.6.1 53.59 KB Mon, 10 Nov 2025 04:16:43 GMT 2
3.6.1-preview-41 53.6 KB Mon, 10 Nov 2025 04:14:34 GMT 4
3.6.1-preview-40 53.61 KB Mon, 10 Nov 2025 04:12:56 GMT 4
3.6.0 53.59 KB Mon, 10 Nov 2025 02:30:34 GMT 4
3.6.0-preview-9 53.6 KB Mon, 10 Nov 2025 02:08:16 GMT 6
3.6.0-preview-5 53.6 KB Mon, 10 Nov 2025 01:27:40 GMT 5
3.6.0-preview-37 53.61 KB Mon, 10 Nov 2025 03:51:54 GMT 4
3.6.0-preview-13 53.61 KB Mon, 10 Nov 2025 02:27:14 GMT 3
3.6.0-preview-10 53.61 KB Mon, 10 Nov 2025 02:11:26 GMT 3
3.5.0 53.63 KB Mon, 28 Jul 2025 04:08:52 GMT 72
3.4.2 52.84 KB Wed, 09 Nov 2022 02:14:02 GMT 76
3.4.1 52.67 KB Wed, 09 Nov 2022 01:07:38 GMT 72
3.4.0 52.51 KB Wed, 09 Nov 2022 00:39:54 GMT 78
3.3.2 29.68 KB Fri, 07 Oct 2022 02:45:46 GMT 67
3.3.1 29.64 KB Mon, 12 Sep 2022 22:13:58 GMT 64
3.3.0 29.63 KB Mon, 12 Sep 2022 04:08:47 GMT 74
3.2.3 29.36 KB Sat, 27 Aug 2022 04:46:00 GMT 69
3.2.2 27.13 KB Sat, 27 Aug 2022 03:54:13 GMT 57
3.2.1 27.13 KB Fri, 26 Aug 2022 16:06:23 GMT 68
3.2.0 22.23 KB Tue, 31 May 2022 03:44:42 GMT 79
3.1.3 22.23 KB Tue, 31 May 2022 03:43:55 GMT 62
3.1.2 22.11 KB Wed, 03 Nov 2021 00:13:53 GMT 67
3.1.1 22.11 KB Wed, 03 Nov 2021 00:13:16 GMT 87
3.1.0 22.01 KB Sun, 12 Sep 2021 23:02:36 GMT 57
3.0.2 21.89 KB Wed, 08 Sep 2021 22:06:29 GMT 63
3.0.1 22.25 KB Sat, 07 Aug 2021 03:58:59 GMT 68
3.0.0 22.26 KB Wed, 04 Aug 2021 03:24:40 GMT 63
2.1.4 22.19 KB Mon, 05 Jul 2021 04:09:39 GMT 50
2.1.3 22.2 KB Tue, 22 Jun 2021 01:11:26 GMT 74
2.1.2 21.97 KB Mon, 21 Jun 2021 03:39:03 GMT 60
2.1.1 21.54 KB Mon, 07 Jun 2021 00:31:25 GMT 65
2.1.0 17.69 KB Fri, 04 Jun 2021 01:21:40 GMT 77
2.0.13 17.4 KB Thu, 03 Jun 2021 23:42:27 GMT 68
2.0.12 15.73 KB Tue, 25 May 2021 20:18:10 GMT 62
2.0.11 15.7 KB Fri, 21 May 2021 19:27:28 GMT 78
2.0.10 15.71 KB Sun, 16 May 2021 18:13:02 GMT 79
2.0.9 15.19 KB Fri, 14 May 2021 03:04:13 GMT 71
2.0.8 15.13 KB Sun, 04 Apr 2021 22:17:20 GMT 65
2.0.7 15.08 KB Sat, 03 Apr 2021 19:30:12 GMT 65
2.0.6 15.01 KB Fri, 02 Apr 2021 23:07:54 GMT 71
2.0.5 14.94 KB Sun, 28 Mar 2021 23:37:19 GMT 72
2.0.4 14.79 KB Sun, 28 Mar 2021 20:25:09 GMT 70
2.0.3 14.79 KB Sat, 27 Mar 2021 22:28:58 GMT 68
2.0.2 14.77 KB Sat, 27 Mar 2021 20:42:54 GMT 68
2.0.1 14.32 KB Fri, 19 Mar 2021 22:02:32 GMT 81
2.0.0 14.3 KB Fri, 19 Mar 2021 03:53:07 GMT 65
1.2.5 14.57 KB Sun, 14 Mar 2021 03:25:47 GMT 70
1.2.4 14.55 KB Sun, 10 Jan 2021 22:43:29 GMT 70
1.2.3 14.47 KB Sun, 10 Jan 2021 22:23:26 GMT 74
1.2.2 14.33 KB Tue, 01 Sep 2020 21:02:26 GMT 69
1.2.1 14.28 KB Tue, 18 Aug 2020 00:42:25 GMT 79
1.2.0 14.28 KB Sun, 09 Aug 2020 04:20:35 GMT 67
1.1.1 13.06 KB Wed, 05 Aug 2020 13:50:00 GMT 64
1.1.0 12.8 KB Sun, 12 Jul 2020 20:27:08 GMT 75
1.0.10 12.62 KB Sun, 19 Apr 2020 01:29:34 GMT 75
1.0.9 12.57 KB Tue, 14 Apr 2020 02:20:28 GMT 68
1.0.8 12.5 KB Tue, 14 Apr 2020 02:04:15 GMT 55
1.0.7 12.42 KB Sat, 11 Apr 2020 22:08:50 GMT 77
1.0.6 12.42 KB Sat, 11 Apr 2020 22:07:15 GMT 73
1.0.5 13.78 KB Fri, 20 Dec 2019 01:02:18 GMT 65
1.0.4 12.82 KB Mon, 16 Dec 2019 00:34:21 GMT 52
1.0.3 13.39 KB Sun, 15 Dec 2019 06:08:32 GMT 70
1.0.2 13.16 KB Mon, 25 Nov 2019 04:32:25 GMT 69
1.0.1 13.2 KB Mon, 18 Nov 2019 03:29:44 GMT 66
1.0.0 12.76 KB Thu, 10 Oct 2019 16:30:25 GMT 61