Skip to content

Commit 8c5b80a

Browse files
committed
refactoring string extensions
1 parent 1217bd9 commit 8c5b80a

File tree

6 files changed

+32
-76
lines changed

6 files changed

+32
-76
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
## Extensions
22

33
An extension methods collection
4+

src/Extensions/CharExtensions.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Extensions
2+
{
3+
public static class CharExtensions
4+
{
5+
public static char ToLower(this char c) =>
6+
char.ToLower(c);
7+
}
8+
}

src/Extensions/StringExtensions.cs

+2-26
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,6 @@ public static string Absolutize(this string path)
3535
return IsPathRooted(path) ? path : Combine(Directory.GetCurrentDirectory(), path);
3636
}
3737

38-
public static string Slice(this string source, int start) => source.Slice(start, source.Length);
39-
40-
public static string Slice(this string source, int start, int end)
41-
{
42-
if (source == default)
43-
throw new ArgumentException("Cannot slice a null string");
44-
if (start < 0)
45-
throw new ArgumentException("Slice cannot have a negative start");
46-
if (start >= source.Length)
47-
return "";
48-
49-
if (end < 0) // Keep this for negative end support
50-
end = source.Length + end;
51-
var len = end - start; // Calculate length
52-
return source.Substring(start, len); // Return Substring of length
53-
}
54-
5538
public static string FromCamelCase(this string source)
5639
{
5740
if (source == default)
@@ -182,7 +165,7 @@ public static string LowerFirstLetter(this string text)
182165
if (text == default)
183166
return text;
184167

185-
return text.Slice(0, 1).ToLower() + text.Slice(1);
168+
return text[0].ToLower() + text[1..];
186169
}
187170

188171
public static int ToInt32(this string text)
@@ -191,17 +174,10 @@ public static int ToInt32(this string text)
191174
return 0;
192175
return Convert.ToInt32(text);
193176
}
177+
194178
public static string Except(this string text1, string text2)
195179
{
196180
return text1.ToCharArray().Where(x => !text2.Contains(x)).Join("");
197181
}
198-
199-
public static string FirstCharToLowerCase(this string str)
200-
{
201-
if (string.IsNullOrEmpty(str) || char.IsLower(str[0]))
202-
return str;
203-
204-
return char.ToLower(str[0]) + str[1..];
205-
}
206182
}
207183
}

src/Infrastructure/Infrastructure.csproj

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="6.0.1" />
11-
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="6.0.1" />
12-
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="6.0.2" />
13-
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="6.0.2" />
10+
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="6.0.2" />
11+
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="6.0.2" />
12+
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="6.0.4" />
13+
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="6.0.4" />
1414
<PackageReference Include="AutoMapper" Version="11.0.1" />
1515
<PackageReference Include="MassTransit.AspNetCore" Version="7.3.1" />
1616
<PackageReference Include="MassTransit.RabbitMQ" Version="7.3.1" />
1717
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.2" />
1818
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.2" />
1919
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
20-
<PackageReference Include="CSharpFunctionalExtensions" Version="2.28.0" />
20+
<PackageReference Include="CSharpFunctionalExtensions" Version="2.28.2" />
2121
<PackageReference Include="Microsoft.Windows.Compatibility" Version="6.0.0" />
2222
<PackageReference Include="Novell.Directory.Ldap.NETStandard" Version="3.6.0" />
2323
<PackageReference Include="Polly" Version="7.2.3" />
24-
<PackageReference Include="RestSharp" Version="107.2.1" />
24+
<PackageReference Include="RestSharp" Version="107.3.0" />
2525
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
2626
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
2727
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />

tests/Extensions.Tests/Extensions.Tests.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
1111
<PackageReference Include="xunit" Version="2.4.1" />
1212
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
1313
<PrivateAssets>all</PrivateAssets>
@@ -20,7 +20,7 @@
2020
</ItemGroup>
2121

2222
<ItemGroup>
23-
<ProjectReference Include="..\..\common\Extensions\Extensions.csproj" />
23+
<ProjectReference Include="..\..\src\Extensions\Extensions.csproj" />
2424
</ItemGroup>
2525

2626
</Project>

tests/Extensions.Tests/StringExtensionsTests.cs

+12-41
Original file line numberDiff line numberDiff line change
@@ -78,37 +78,6 @@ public void AbsolutizeShouldThrowExceptionIfStringIsNull()
7878
Assert.Equal("Cannot absolutize a null path", message);
7979
}
8080

81-
[Theory]
82-
[InlineData("test string", 0, "test string")]
83-
[InlineData("test string", 2, "st string")]
84-
public void ShouldSliceStringWithStart(string value, int start, string expected)
85-
{
86-
Assert.Equal(expected, value.Slice(start));
87-
}
88-
89-
[Theory]
90-
[InlineData("test string", 2, 7, "st st")]
91-
[InlineData("test string", 2, -2, "st stri")]
92-
public void ShouldSliceString(string value, int start, int end, string expected)
93-
{
94-
Assert.Equal(expected, value.Slice(start, end));
95-
}
96-
97-
[Fact]
98-
public void SliceShouldThrowExceptionIfStringIsNull()
99-
{
100-
string test = default;
101-
var message = Assert.Throws<ArgumentException>(() => test.Slice(2, 5)).Message;
102-
Assert.Equal("Cannot slice a null string", message);
103-
}
104-
105-
[Fact]
106-
public void SliceShouldThrowExceptionIfStartIsNegative()
107-
{
108-
var message = Assert.Throws<ArgumentException>(() => "test string".Slice(-1, 5)).Message;
109-
Assert.Equal("Slice cannot have a negative start", message);
110-
}
111-
11281
[Theory]
11382
[InlineData("ThisIsATestString", "This Is A Test String")]
11483
[InlineData("_ThisIsATestString", "This Is A Test String")]
@@ -249,16 +218,6 @@ public void ShouldRemoveDiacritics(string input, string expected)
249218
Assert.Equal(expected, input.RemoveDiacritics());
250219
}
251220

252-
[Theory]
253-
[InlineData(default, default)]
254-
[InlineData("test", "test")]
255-
[InlineData("TEST", "tEST")]
256-
[InlineData("Test", "test")]
257-
public void ShouldLowerFirstLetter(string input, string expected)
258-
{
259-
Assert.Equal(expected, input.LowerFirstLetter());
260-
}
261-
262221
[Theory]
263222
[InlineData(default, 0)]
264223
[InlineData("42", 42)]
@@ -275,5 +234,17 @@ public void ShouldRemoveCharFromString(string text1, string text2, string expect
275234
{
276235
Assert.Equal(expected, text1.Except(text2));
277236
}
237+
238+
[Theory]
239+
[InlineData("Test","test")]
240+
[InlineData("TEST","tEST")]
241+
[InlineData("T","t")]
242+
[InlineData("t","t")]
243+
[InlineData("","")]
244+
[InlineData(default,default)]
245+
public void ShouldLowerFirstChar(string str, string expected)
246+
{
247+
Assert.Equal(expected,str.LowerFirstLetter());
248+
}
278249
}
279250
}

0 commit comments

Comments
 (0)