Set of basic extensions.
- ToEnumerable: Converts any thing to IEnumerable (yield)
Check the following
"Hello World".ToCamelCase(); // helloWorld
Sample
[Theory] [InlineData("BnayaEshet", "bnayaEshet")] [InlineData("bnayaEshet", "bnayaEshet")] [InlineData("bnaya Eshet", "bnayaEshet")] [InlineData("bnaya-Eshet", "bnayaEshet")] [InlineData("bnaya_Eshet", "bnayaEshet")] [InlineData("bnaya_eshet", "bnayaEshet")] [InlineData("b1n2aya Eshet", "b1n2ayaEshet")] [InlineData("", "")] [InlineData(null, "")] public void String_ToCamelCase_Test_Succeed(string input, string expected) { string result = input.ToCamelCase(); _outputHelper.WriteLine($"'{input}' = '{result}'"); Assert.Equal(expected, result); }
"hello World".ToCamelCase(); // HelloWorld
Sample
[Theory] [InlineData("BnayaEshet", "BnayaEshet")] [InlineData("bnayaEshet", "BnayaEshet")] [InlineData("bnaya Eshet", "BnayaEshet")] [InlineData("bnaya-Eshet", "BnayaEshet")] [InlineData("bnaya-eshet", "BnayaEshet")] [InlineData("bnaya_Eshet", "BnayaEshet")] [InlineData("Bnaya_eshet", "BnayaEshet")] [InlineData("bnaya_eshet", "BnayaEshet")] [InlineData("b1n2aya_eshet", "B1n2ayaEshet")] [InlineData("", "")] [InlineData(null, "")] public void String_ToPascalCase_Test_Succeed(string input, string expected) { string result = input.ToPascalCase(); _outputHelper.WriteLine($"'{input}' = '{result}'"); Assert.Equal(expected, result); }
"helloWorld".ToCamelCase(); // HELLO_WORLD
Sample
[Theory] [InlineData("BnayaEshet", "BNAYA_ESHET")] [InlineData("Bnaya_Eshet", "BNAYA_ESHET")] [InlineData("Bnaya_ESHET", "BNAYA_ESHET")] [InlineData("Bnaya1234Eshet", "BNAYA1234_ESHET")] [InlineData("Bnaya Eshet", "BNAYA_ESHET")] [InlineData(" Bnaya Eshet", "BNAYA_ESHET")] [InlineData("Bnaya Eshet ", "BNAYA_ESHET")] [InlineData("Bnaya Eshet", "BNAYA_ESHET")] [InlineData("Bnay$a Eshet", "BNAY$A_ESHET")] [InlineData("Bnaya$ Eshet", "BNAYA$_ESHET")] [InlineData("Bnaya$Eshet", "BNAYA$_ESHET")] [InlineData("Bnaya__Eshet", "BNAYA_ESHET")] [InlineData("Bnaya_ _Eshet", "BNAYA_ESHET")] [InlineData("Bnaya _ _Eshet", "BNAYA_ESHET")] [InlineData("Bnaya_ _ Eshet", "BNAYA_ESHET")] [InlineData("", "")] [InlineData(null, "")] public void String_ToSCREAMING_Test_Succeed(string input, string expected) { string result = input.ToSCREAMING(); _outputHelper.WriteLine($"'{input}' = '{result}'"); Assert.Equal(expected, result); }
"Hello World".ToCamelCase(); // hello-world
Sample
[Theory] [InlineData("BnayaEshet", "bnaya-eshet")] [InlineData("Bnaya_Eshet", "bnaya-eshet")] [InlineData("Bnaya_ESHET", "bnaya-eshet")] [InlineData("Bnaya1234Eshet", "bnaya1234-eshet")] [InlineData("Bnaya Eshet", "bnaya-eshet")] [InlineData(" Bnaya Eshet", "bnaya-eshet")] [InlineData("Bnaya Eshet ", "bnaya-eshet")] [InlineData("Bnaya Eshet", "bnaya-eshet")] [InlineData("Bnay$a Eshet", "bnay-a-eshet")] [InlineData("Bnaya$ Eshet", "bnaya-eshet")] [InlineData("Bnaya$Eshet", "bnaya-eshet")] [InlineData("Bnaya--Eshet", "bnaya-eshet")] [InlineData("Bnaya- -Eshet", "bnaya-eshet")] [InlineData("Bnaya__Eshet", "bnaya-eshet")] [InlineData("Bnaya_ _Eshet", "bnaya-eshet")] [InlineData("Bnaya _ _Eshet", "bnaya-eshet")] [InlineData("Bnaya_ _ Eshet", "bnaya-eshet")] [InlineData("", "")] [InlineData(null, "")] public void String_ToDash_Test_Succeed(string input, string expected) { string result = input.ToDash(); _outputHelper.WriteLine($"'{input}' = '{result}'"); Assert.Equal(expected, result); }
Sample
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var r1 = 1.ToEnumerable(2, arr[2..]); Assert.Equal(arr, r1); var r2 = 1.ToEnumerable(2, 3, arr[3..]); Assert.Equal(arr, r2); var r3 = 1.ToEnumerable(2, 3, 4, arr[4..]); Assert.Equal(arr, r3); var r4 = arr[..2].ToEnumerable(3, 4, 5, 6, 7, 8, 9); Assert.Equal(arr, r4);
Disposable pattern (based on Reactive Extensions)
Disposable which do noting on disposal.
Useful when the needs for disposable is conditional (under the using pattern).
Sample
using var dsp = factory?.GetDisposable() ?? isposable.Empty;
Creates a disposable object that invokes the specified action when disposed.
using var d = Disposable.Create(() => logger.Log("disposed"));
Sample
ILog logger = A.Fake<ILog>(); using (var d = Disposable.Create(() => logger.Log("disposed"))) { A.CallTo(() => logger.Log(A<string>.Ignored)) .MustNotHaveHappened(); } A.CallTo(() => logger.Log(A<string?>.Ignored)) .MustHaveHappenedOnceExactly();
Maintain a scoped state (which can affect parent state on exit by a custom logic).
Can be useful to maintain state with in a Visitor pattern (or functional programming).
Sample
StackCancelable<int> d1; using (d1 = Disposable.CreateStack<int>(10)) { Assert.Equal(10, d1.State); using (var state = d1.Push(50)) { Assert.Equal(50, d1.State); Assert.Equal(50, state.State); int i = state; Assert.Equal(50, i); } using (var state = d1.Push(2, (prv, inScope) => inScope * 2 + prv)) { Assert.Equal(2, state); Assert.Equal(2, d1.State); } Assert.Equal(14, d1.State); // the state which was calculate when the scope ends using (var state = d1.Push(m => m * 2)) // calculate from current state { Assert.Equal(28, state); } Assert.Equal(14, d1.State); using (var state = d1.Push(m => m * 2)) { Assert.Equal(28, d1.State); using (var state1 = d1.Push(m => m * 2, (prv, inScope) => inScope + 1)) { Assert.Equal(56, state1); } Assert.Equal(57, d1.State); } Assert.Equal(14, d1.State); Assert.False(d1.IsDisposed); } Assert.True(d1.IsDisposed);
Creates a collection builder, which aware of the stack context.
It'll remove items from the collection on stack frame disposal
Sample
CollectionDisposable<int> stackCollection; using (stackCollection = Disposable.CreateCollection<int>()) using (var root = stackCollection.Add(10)) { _outputHelper.WriteLine(stackCollection.ToString()); _outputHelper.WriteLine(root.ToString()); Assert.True(10.ToEnumerable().SequenceEqual(stackCollection.State)); using (var state = stackCollection.Add(50)) { Assert.True(state.SequenceEqual(10.ToEnumerable(50))); } using (var state = stackCollection.Add(2)) { Assert.True(state.SequenceEqual(10.ToEnumerable(2))); } Assert.True(stackCollection.SequenceEqual(10.ToEnumerable())); using (var state = stackCollection.Add(30)) { Assert.True(state.SequenceEqual(10.ToEnumerable(30))); using (var state1 = stackCollection.Add(5,6, 7)) { Assert.True(state1.SequenceEqual(10.ToEnumerable(30, 5, 6, 7))); } Assert.True(stackCollection.SequenceEqual(10.ToEnumerable(30))); } Assert.True(stackCollection.SequenceEqual(10.ToEnumerable())); Assert.False(stackCollection.IsDisposed); } Assert.True(stackCollection.IsDisposed);