Skip to content

Commit

Permalink
feat!: use digit as delimiters; use capital letters as one word; remo…
Browse files Browse the repository at this point in the history
…ve IsUpperCase char extention (use char.IsUpper instead).
  • Loading branch information
Basim108 committed May 21, 2024
1 parent e6d0b70 commit e6247fc
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 53 deletions.
13 changes: 8 additions & 5 deletions Hrimsoft.StringCases.Tests/ToCamelCaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public void given_white_space_returns_empty_string()
[Fact]
public void given_null_returns_empty_string()
{
string test = null;
Assert.Equal("", test.ToCamelCase());
Assert.Equal("", ((string)null).ToCamelCase());
}

[Fact]
Expand Down Expand Up @@ -73,9 +72,13 @@ public void given_two_word_underscored_camel_returns_correct()
}

[Fact]
public void given_two_capital_letters_into_2_words()
{
Assert.Equal("aA", "AA".ToCamelCase());
public void given_two_capital_letters_into_one_word() {
Assert.Equal("countryNz", "countryNZ".ToCamelCase());
Assert.Equal("nz", "NZ".ToCamelCase());
Assert.Equal("gdprIsNotCcpa", "GDPRisNotCCPA".ToCamelCase());
Assert.Equal("gdpr1IsNotCcpa", "GDPR1isNotCCPA".ToCamelCase());
Assert.Equal("1IsNotNz", "1isNotNZ".ToCamelCase());
Assert.Equal("1IsNot2", "1isNot2".ToCamelCase());
}

[Fact]
Expand Down
10 changes: 7 additions & 3 deletions Hrimsoft.StringCases.Tests/ToKebabCaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ public void given_two_word_underscored_camel_returns_correct()
}

[Fact]
public void given_two_capital_letters_into_2_words()
{
Assert.Equal("a-a", "AA".ToKebabCase());
public void given_two_capital_letters_into_one_word() {
Assert.Equal("country-nz", "countryNZ".ToKebabCase());
Assert.Equal("nz", "NZ".ToKebabCase());
Assert.Equal("gdpr-is-not-ccpa", "GDPRisNotCCPA".ToKebabCase());
Assert.Equal("gdpr-1-is-not-ccpa", "GDPR1isNotCCPA".ToKebabCase());
Assert.Equal("1-is-not-nz", "1isNotNZ".ToKebabCase());
Assert.Equal("1-is-not-2", "1isNot2".ToKebabCase());
}

[Fact]
Expand Down
15 changes: 9 additions & 6 deletions Hrimsoft.StringCases.Tests/ToPascalCaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public void given_white_spaced_string_returns_empty_string()
[Fact]
public void given_null_returns_empty_string()
{
string test = null;
Assert.Equal("", test.ToPascalCase());
Assert.Equal("", ((string)null).ToPascalCase());
}

[Fact]
Expand Down Expand Up @@ -71,11 +70,15 @@ public void given_two_word_underscored_camel_returns_correct()
{
Assert.Equal("FirstSecond", "_firstSecond".ToPascalCase());
}

[Fact]
public void given_two_capital_letters_into_2_words()
{
Assert.Equal("AA", "AA".ToPascalCase());
public void given_two_capital_letters_into_one_word() {
Assert.Equal("CountryNz", "countryNZ".ToPascalCase());
Assert.Equal("Nz", "NZ".ToPascalCase());
Assert.Equal("GdprIsNotCcpa", "GDPRisNotCCPA".ToPascalCase());
Assert.Equal("Gdpr1IsNotCcpa", "GDPR1isNotCCPA".ToPascalCase());
Assert.Equal("1IsNotNz", "1isNotNZ".ToPascalCase());
Assert.Equal("1IsNot2", "1isNot2".ToPascalCase());
}

[Fact]
Expand Down
12 changes: 8 additions & 4 deletions Hrimsoft.StringCases.Tests/ToSnakeCaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public void given_white_spaced_string_returns_empty_string()
[Fact]
public void given_null_returns_empty_string()
{
string test = null;
Assert.Equal("", test.ToSnakeCase());
Assert.Equal("", ((string)null).ToSnakeCase());
}

[Fact]
Expand Down Expand Up @@ -73,9 +72,14 @@ public void given_two_word_underscored_camel_returns_correct()
}

[Fact]
public void given_two_capital_letters_into_2_words()
public void given_two_capital_letters_into_one_word()
{
Assert.Equal("a_a", "AA".ToSnakeCase());
Assert.Equal("country_nz", "countryNZ".ToSnakeCase());
Assert.Equal("nz", "NZ".ToSnakeCase());
Assert.Equal("gdpr_is_not_ccpa", "GDPRisNotCCPA".ToSnakeCase());
Assert.Equal("gdpr_1_is_not_ccpa", "GDPR1isNotCCPA".ToSnakeCase());
Assert.Equal("1_is_not_nz", "1isNotNZ".ToSnakeCase());
Assert.Equal("1_is_not_2", "1isNot2".ToSnakeCase());
}

[Fact]
Expand Down
13 changes: 8 additions & 5 deletions Hrimsoft.StringCases.Tests/ToTrainCaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public void given_white_space_returns_empty_string()
[Fact]
public void given_null_returns_empty_string()
{
string test = null;
Assert.Equal("", test.ToTrainCase());
Assert.Equal("", ((string)null).ToTrainCase());
}

[Fact]
Expand Down Expand Up @@ -73,9 +72,13 @@ public void given_two_word_underscored_camel_returns_correct()
}

[Fact]
public void given_two_capital_letters_into_2_words()
{
Assert.Equal("A-A", "AA".ToTrainCase());
public void given_two_capital_letters_into_one_word() {
Assert.Equal("Country-Nz", "countryNZ".ToTrainCase());
Assert.Equal("Nz", "NZ".ToTrainCase());
Assert.Equal("Gdpr-Is-Not-Ccpa", "GDPRisNotCCPA".ToTrainCase());
Assert.Equal("Gdpr-1-Is-Not-Ccpa", "GDPR1isNotCCPA".ToTrainCase());
Assert.Equal("1-Is-Not-Nz", "1isNotNZ".ToTrainCase());
Assert.Equal("1-Is-Not-2", "1isNot2".ToTrainCase());
}

[Fact]
Expand Down
13 changes: 8 additions & 5 deletions Hrimsoft.StringCases.Tests/ToUnderscoredCamelCaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public void given_white_space_returns_empty_string()
[Fact]
public void given_null_returns_empty_string()
{
string test = null;
Assert.Equal("", test.ToUnderscoredCamelCase());
Assert.Equal("", ((string)null).ToUnderscoredCamelCase());
}

[Fact]
Expand Down Expand Up @@ -73,9 +72,13 @@ public void given_two_word_underscored_camel_returns_correct()
}

[Fact]
public void given_two_capital_letters_into_2_words()
{
Assert.Equal("_aA", "AA".ToUnderscoredCamelCase());
public void given_two_capital_letters_into_one_word() {
Assert.Equal("_countryNz", "countryNZ".ToUnderscoredCamelCase());
Assert.Equal("_nz", "NZ".ToUnderscoredCamelCase());
Assert.Equal("_gdprIsNotCcpa", "GDPRisNotCCPA".ToUnderscoredCamelCase());
Assert.Equal("_gdpr1IsNotCcpa", "GDPR1isNotCCPA".ToUnderscoredCamelCase());
Assert.Equal("_1IsNotNz", "1isNotNZ".ToUnderscoredCamelCase());
Assert.Equal("_1IsNot2", "1isNot2".ToUnderscoredCamelCase());
}

[Fact]
Expand Down
4 changes: 4 additions & 0 deletions Hrimsoft.StringCases.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=236f7aa5_002D7b06_002D43ca_002Dbf2a_002D9b31bfcff09a/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Private" Description="Constant fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="CONSTANT_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=669e5282_002Dfb4b_002D4e90_002D91e7_002D07d269d04b60/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Protected, ProtectedInternal, Internal, Public, PrivateProtected" Description="Constant fields (not private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="CONSTANT_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /&gt;&lt;/Policy&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=a4f433b8_002Dabcd_002D4e55_002Da08f_002D82e78cef0f0c/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Any" AccessRightKinds="Any" Description="Local constants"&gt;&lt;ElementKinds&gt;&lt;Kind Name="LOCAL_CONSTANT" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /&gt;&lt;/Policy&gt;</s:String></wpf:ResourceDictionary>
17 changes: 1 addition & 16 deletions Hrimsoft.StringCases/CharExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
namespace Hrimsoft.StringCases
{
/// <summary>
/// Extensions for char primitives
/// </summary>
internal static class CharExtensions
{
/// <summary>
/// Tests a symbol for being upper case
/// Tests a symbol for being a delimiter: space, _ , . , -
/// </summary>
/// <param name="symbol">tested symbol</param>
/// <returns></returns>
public static bool IsUpperCase(this char symbol)
{
var result = symbol >= 'A' && symbol <= 'Z';
return result;
}

/// <summary>
/// Tests a symbol for being a delimiter
/// </summary>
/// <param name="symbol">tested symbol</param>
/// <returns></returns>
public static bool IsDelimiter(this char symbol)
{
var result = symbol == ' ' || symbol == '_' || symbol == '.' || symbol == '-';
Expand Down
6 changes: 3 additions & 3 deletions Hrimsoft.StringCases/Hrimsoft.StringCases.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<Authors>Basim Al-Jawahery</Authors>
<PackageProjectUrl>https://github.com/Basim108/string-cases</PackageProjectUrl>
<RepositoryUrl>https://github.com/Basim108/string-cases</RepositoryUrl>
<PackageVersion>1.0.0</PackageVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
<PackageVersion>2.0.0</PackageVersion>
<AssemblyVersion>2.0.0</AssemblyVersion>
<FileVersion>2.0.0</FileVersion>
<Company>Hrimsoft</Company>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Description>String extensions for converting into different cases e.g. snake_case, camelCase, kebab-case, _underscoredCamelCase, Train-Case</Description>
Expand Down
12 changes: 6 additions & 6 deletions Hrimsoft.StringCases/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Hrimsoft.StringCases
{
/// <summary>
/// Extension methods for String type
/// </summary>
public static class StringExtensions
{
/// <summary>
Expand All @@ -17,9 +14,12 @@ public static IList<string> SplitOnToParts(this string source)
var parts = new List<string>();
var lexeme = new List<string>();

for (var i = 0; i < source.Length; i++)
{
if (source[i].IsDelimiter() || source[i].IsUpperCase())
for (var i = 0; i < source.Length; i++) {
var isUpperDelimiter = i > 0 && char.IsUpper(source[i]) && !char.IsUpper(source[i - 1]);
var isLowerDelimiter = i > 1 && char.IsLower(source[i]) && char.IsUpper(source[i - 1]) && char.IsUpper(source[i - 2])
|| i > 0 && char.IsLower(source[i]) && char.IsDigit(source[i - 1]);

if (source[i].IsDelimiter() || isUpperDelimiter || isLowerDelimiter || char.IsDigit(source[i]))
{
if (lexeme.Count > 0)
{
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ PM> Install-Package Hrimsoft.StringCases
var pascalCase = "Some text that could be any-kind--ofText".ToPascalCase();
// pascalCase = "SomeTextThatCouldBeAnyKindOfText"
var twoCapitalLetters = "countryNZ".ToSnakeCase(); // country_nz
twoCapitalLetters = "NZ".ToSnakeCase(); // nz
twoCapitalLetters = "GDPRisNotCCPA".ToSnakeCase(); // gdpr_is_not_ccpa
var digitIsDilimiter = "GDPR1isNotCCPA".ToSnakeCase(); // gdpr_1_is_not_ccpa
digitIsDilimiter = "1isNotCCPA".ToSnakeCase(); // 1_is_not_ccpa
digitIsDilimiter = "1isNot2".ToSnakeCase(); // 1_is_not_2
```

## License ##
Expand Down

0 comments on commit e6247fc

Please sign in to comment.