diff --git a/Maybe.Documentation/Maybe.Documentation.csproj b/Maybe.Documentation/Maybe.Documentation.csproj new file mode 100644 index 0000000..0d83803 --- /dev/null +++ b/Maybe.Documentation/Maybe.Documentation.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/Maybe.Documentation/MaybeExtensions.cs b/Maybe.Documentation/MaybeExtensions.cs new file mode 100644 index 0000000..169d7c0 --- /dev/null +++ b/Maybe.Documentation/MaybeExtensions.cs @@ -0,0 +1,145 @@ +using ZBRA.Maybe; + +namespace Maybe.Documentation +{ + public class MaybeExtensions + { + public static void OrEmptyExample_WithValue() + { + Console.WriteLine("OrEmpty example with values"); + var maybe = "some value".ToMaybe(); + var v = maybe.OrEmpty(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: some value + } + + public static void OrEmptyExample_WithoutValue() + { + Console.WriteLine("OrEmpty example without values"); + var maybe = Maybe.Nothing; + var v = maybe.OrEmpty(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: + } + + public static void OrNullExample_WithValue() + { + Console.WriteLine("OrNull example with values"); + var maybe = "some value".ToMaybe(); + var v = maybe.OrNull(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: some value + } + + public static void OrNullExample_WithoutValue() + { + Console.WriteLine("OrNull example without values"); + var maybe = Maybe.Nothing; + var v = maybe.OrNull(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: + } + + + public static void OrTrueExample_WithValue() + { + Console.WriteLine("OrTrue example with values"); + var maybe = false.ToMaybe(); + var v = maybe.OrTrue(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: false + } + + public static void OrTrueExample_WithoutValue() + { + Console.WriteLine("OrTrue example without values"); + var maybe = Maybe.Nothing; + var v = maybe.OrTrue(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: true + } + + public static void OrFalseExample_WithValue() + { + Console.WriteLine("OrFalse example with values"); + var maybe = false.ToMaybe(); + var v = maybe.OrFalse(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: False + } + + public static void OrFalseExample_WithoutValue() + { + Console.WriteLine("OrFalse example without values"); + var maybe = Maybe.Nothing; + var v = maybe.OrFalse(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: False + } + + internal static void ToNullableExample_WithValue() + { + Console.WriteLine("ToNullable example with value"); + var maybe = 1234.ToMaybe(); + var v = maybe.ToNullable(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: 1234 + } + + internal static void ToNullableExample_WithoutValue() + { + Console.WriteLine("ToNullable example without value"); + var maybe = Maybe.Nothing; + var v = maybe.ToNullable(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: + } + + internal static void ToMaybeExample_WithObjectValue() + { + Console.WriteLine("ToMaybe example with object"); + var myObject = new { PropertyA = "Value Of Property A", PropertyB = 66 }; + var maybe = myObject.ToMaybe(); + Console.WriteLine($"Print Value: {maybe}"); + // Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } + Console.WriteLine($"Maybe PropertyA value: {maybe.Select(o => o.PropertyA)}"); + // Maybe PropertyA value: Value Of Property A + Console.WriteLine($"Maybe PropertyB value: {maybe.Select(o => o.PropertyB)}"); + // Maybe PropertyB value: 66 + } + + internal static void ToMaybeExample_WithStructValue() + { + Console.WriteLine("ToMaybe example with struct"); + var myInt = 66; + var maybe = myInt.ToMaybe(); + Console.WriteLine($"Print Value: {maybe}"); + // Print Value: 66 + Console.WriteLine($"Print Maybe value: {maybe.Select(i => i)}"); + //Print Maybe value: 66 + } + + internal static void ToMaybeExample_WithMaybeValue() + { + Console.WriteLine("ToMaybe example with a Maybe of an object"); + var myObject = new { PropertyA = "Value Of Property A", PropertyB = 66 }; + var maybe = myObject.ToMaybe(); + var secondMaybe = maybe.ToMaybe(); + Console.WriteLine($"First Maybe Print Value: {maybe}"); + // First Maybe Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } + Console.WriteLine($"Second Maybe Print Value: {secondMaybe}"); + // Second Maybe Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } + } + + internal static void ToMaybeExample_WithMaybeNothingValue() + { + Console.WriteLine("ToMaybe example with a Maybe of an object"); + var maybe = Maybe.Nothing; + var secondMaybe = maybe.ToMaybe(); + Console.WriteLine($"First Maybe Print Value: {maybe}"); + // First Maybe Print Value: + Console.WriteLine($"Second Maybe Print Value: {secondMaybe}"); + // Second Maybe Print Value: + } + } +} diff --git a/Maybe.Documentation/Program.cs b/Maybe.Documentation/Program.cs new file mode 100644 index 0000000..febdf1e --- /dev/null +++ b/Maybe.Documentation/Program.cs @@ -0,0 +1,27 @@ +using Maybe.Documentation; + +//OrEmpty +MaybeExtensions.OrEmptyExample_WithValue(); +MaybeExtensions.OrEmptyExample_WithoutValue(); +//OrNull +MaybeExtensions.OrNullExample_WithValue(); +MaybeExtensions.OrNullExample_WithoutValue(); +//OrTrue +MaybeExtensions.OrTrueExample_WithValue(); +MaybeExtensions.OrTrueExample_WithoutValue(); +//OrFalse +MaybeExtensions.OrFalseExample_WithValue(); +MaybeExtensions.OrFalseExample_WithoutValue(); + + +//ToNullable +MaybeExtensions.ToNullableExample_WithValue(); +MaybeExtensions.ToNullableExample_WithoutValue(); + +//ToMaybe +MaybeExtensions.ToMaybeExample_WithObjectValue(); +MaybeExtensions.ToMaybeExample_WithStructValue(); +MaybeExtensions.ToMaybeExample_WithMaybeValue(); +MaybeExtensions.ToMaybeExample_WithMaybeNothingValue(); + +Console.ReadLine(); \ No newline at end of file diff --git a/Maybe.sln b/Maybe.sln index 3a39b32..e4e7cf2 100644 --- a/Maybe.sln +++ b/Maybe.sln @@ -1,11 +1,13 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30717.126 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33213.308 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Maybe.Test", "Maybe.Test\Maybe.Test.csproj", "{F5DD5CA7-C20A-4E6A-A06A-1C4118D10945}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Maybe", "Maybe\Maybe.csproj", "{15B03988-B848-41CC-9B68-1BA4AB16A78F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Maybe", "Maybe\Maybe.csproj", "{15B03988-B848-41CC-9B68-1BA4AB16A78F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Maybe.Documentation", "Maybe.Documentation\Maybe.Documentation.csproj", "{AAB025E0-8341-4A7B-9B65-115430DED230}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {15B03988-B848-41CC-9B68-1BA4AB16A78F}.Debug|Any CPU.Build.0 = Debug|Any CPU {15B03988-B848-41CC-9B68-1BA4AB16A78F}.Release|Any CPU.ActiveCfg = Release|Any CPU {15B03988-B848-41CC-9B68-1BA4AB16A78F}.Release|Any CPU.Build.0 = Release|Any CPU + {AAB025E0-8341-4A7B-9B65-115430DED230}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AAB025E0-8341-4A7B-9B65-115430DED230}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AAB025E0-8341-4A7B-9B65-115430DED230}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AAB025E0-8341-4A7B-9B65-115430DED230}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/README.md b/README.md index a2864ee..8717c16 100644 --- a/README.md +++ b/README.md @@ -73,4 +73,8 @@ if (a < b) { // Nothing will always be less than any int value } -``` \ No newline at end of file +``` + +# See more details + +[Maybe Extension](docs/maybe-extension.md) \ No newline at end of file diff --git a/docs/maybe-extension.md b/docs/maybe-extension.md new file mode 100644 index 0000000..9c9033c --- /dev/null +++ b/docs/maybe-extension.md @@ -0,0 +1,188 @@ +# Maybe Extension Documentation + +## MaybeExtensions.ToMaybe +### Remarks +Converts the value to a Maybe. + +If the value is null, so it returns a Maybe.Nothing for that specific type. +For structs it always returns the Maybe. + +### Method definition +``` +public static Maybe ToMaybe(this T value) +``` +### Overloads +Overload to convert structs. +``` +public static Maybe ToMaybe(this S? value) where S : struct +``` + +This overload will always return the original Maybe, preveting the user to create a Maybe of a Maybe. +``` +public static Maybe ToMaybe(this Maybe value) +``` + +### Examples +Transform an object to a maybe and access its properties +``` +var myObject = new { PropertyA = "Value Of Property A", PropertyB = 66 }; +var maybe = myObject.ToMaybe(); +Console.WriteLine($"Print Value: {maybe}"); +// Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } +Console.WriteLine($"Maybe PropertyA value: {maybe.Select(o => o.PropertyA)}"); +// Maybe PropertyA value: Value Of Property A +Console.WriteLine($"Maybe PropertyB value: {maybe.Select(o => o.PropertyB)}"); +// Maybe PropertyB value: 66 +``` +Transform an int to a maybe and access it value. +``` +var myInt = 66; +var maybe = myInt.ToMaybe(); +Console.WriteLine($"Print Value: {maybe}"); +// Print Value: 66 +Console.WriteLine($"Print Maybe value: {maybe.Select(i => i)}"); +//Print Maybe value: 66 +``` +Try to transform a Maybe object to another Maybe. +``` +var myObject = new { PropertyA = "Value Of Property A", PropertyB = 66 }; +var maybe = myObject.ToMaybe(); +var secondMaybe = maybe.ToMaybe(); +Console.WriteLine($"First Maybe Print Value: {maybe}"); +// First Maybe Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } +Console.WriteLine($"Second Maybe Print Value: {secondMaybe}"); +// Second Maybe Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } + +``` +Try to transform a empty Maybe to another Maybe +``` +var maybe = Maybe.Nothing; +var secondMaybe = maybe.ToMaybe(); +Console.WriteLine($"First Maybe Print Value: {maybe}"); +// First Maybe Print Value: +Console.WriteLine($"Second Maybe Print Value: {secondMaybe}"); +// Second Maybe Print Value: +``` + +## MaybeExtensions.OrEmpty + +### Remarks +Allow to get the encapsulated value or an empty string. + +Returns the maybe value if HasValue is true, otherwise `string.Empty`. + +### Method definition +``` +public static string OrEmpty(this Maybe subject); +``` + +### Examples +``` +var maybe = "some value".ToMaybe(); +var v = maybe.OrEmpty(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: some value +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.OrEmpty(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: +``` + +## MaybeExtensions.OrNull +### Remarks +Allow to get the encapsulated value or null. + +Returns the maybe value if HasValue is true, otherwise `null`. + +### Method definition +``` +public static T OrNull(this Maybe subject) where T : class; +``` + +### Examples +``` +var maybe = "some value".ToMaybe(); +var v = maybe.OrNull(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: some value +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.OrNull(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: +``` + +## MaybeExtensions.OrTrue +### Remarks +Allow to get the boolean maybe encapsulated value or True. + +Returns the maybe value if HasValue is true, otherwise `true`. +### Method definition +``` +public static bool OrTrue(this Maybe subject); +``` +### Examples +``` +var maybe = false.ToMaybe(); +var v = maybe.OrTrue(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: False +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.OrTrue(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: True +``` + +## MaybeExtensions.OrFalse +### Remarks +Allow to get the boolean maybe encapsulated value or False. + +Returns the maybe value if HasValue is true, otherwise `false`. + +### Method defintion +``` +public static bool OrFalse(this Maybe subject); +``` +### Examples +``` +var maybe = false.ToMaybe(); +var v = maybe.OrFalse(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: False +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.OrFalse(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: False +``` + + +## MaybeExtensions.ToNullable +### Remarks +Convert a Maybe to Nullable. + +If the Maybe.HasValue is true, return its `value` otherwise a `null`. + +### Method definition +``` +public static T? ToNullable(this Maybe value) where T : struct +``` +### Examples +``` +var maybe = 1234.ToMaybe(); +var v = maybe.ToNullable(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: 1234 +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.ToNullable(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: +``` \ No newline at end of file