Skip to content

Commit

Permalink
Added a new ToSnakeCase string extension method.
Browse files Browse the repository at this point in the history
Updated version to 2.4.0
  • Loading branch information
ricardoeduardo committed Mar 10, 2019
1 parent 3b7fc25 commit b59d9f1
Show file tree
Hide file tree
Showing 42 changed files with 55 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.AspNetCore.DataAnnotations</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.AspNetCore.DynamicImage</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.AspNetCore.MultiTenant</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.AspNetCore.WebUtilities</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.ActiveDirectory</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.DataAnnotations</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<PropertyGroup>
<PackageId>Umbrella.Utilities.Integration.NewtonsoftJson</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
38 changes: 13 additions & 25 deletions Core/src/Umbrella.Utilities/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,37 +194,23 @@ public static string Clean(this string value, bool convertBrTagsToNl = false, bo
return cleanedValue;
}

private static string ToSnakeCase(this string value, bool lowerCase = true, bool removeWhiteSpace = true)
=> ToSnakeCaseInternal(value, lowerCase, removeWhiteSpace, false);
public static string ToSnakeCase(this string value, bool lowerCase = true, bool removeWhiteSpace = true, CultureInfo cultureInfo = null)
=> ToSnakeCaseInternal(value, lowerCase, removeWhiteSpace, cultureInfo ?? CultureInfo.CurrentCulture);

private static string ToSnakeCaseInvariant(this string value, bool lowerCase = true, bool removeWhiteSpace = true)
=> ToSnakeCaseInternal(value, lowerCase, removeWhiteSpace, true);
public static string ToSnakeCaseInvariant(this string value, bool lowerCase = true, bool removeWhiteSpace = true)
=> ToSnakeCaseInternal(value, lowerCase, removeWhiteSpace, CultureInfo.InvariantCulture);

private static string ToSnakeCaseInternal(string value, bool lowerCase, bool removeWhiteSpace, bool useInvariantCulture)
private static string ToSnakeCaseInternal(string value, bool lowerCase, bool removeWhiteSpace, CultureInfo cultureInfo)
{
Func<string, string> stringLower = null;
Func<char, char> charLower = null;

if (useInvariantCulture)
{
stringLower = x => x.ToLowerInvariant();
charLower = char.ToLowerInvariant;
}
else
{
stringLower = x => x.ToLower();
charLower = char.ToLower;
}

if (string.IsNullOrWhiteSpace(value))
return value;

if (value.Length == 1)
return lowerCase ? stringLower(value) : value;
return lowerCase ? value.ToLower(cultureInfo) : value;

List<char> buffer = new List<char>(value.Length)
var buffer = new List<char>(value.Length)
{
lowerCase ? charLower(value[0]) : value[0]
lowerCase ? char.ToLower(value[0], cultureInfo) : value[0]
};

for (int i = 1; i < value.Length; i++)
Expand All @@ -237,15 +223,17 @@ private static string ToSnakeCaseInternal(string value, bool lowerCase, bool rem
if (char.IsUpper(value[i]))
{
if (lowerCase)
current = charLower(current);
current = char.ToLower(current, cultureInfo);

buffer.Add('_');
}

buffer.Add(current);
}

//TODO: Can this be made more efficient somehow by avoiding the intermediate array??

// TODO: We can make this more efficient by resizing the buffer and returning it instead of creating a new copy.
// Will require the list to be an array to start with though instead of a List<char>
// Could also do it as a stack operation using Span<T>
return new string(buffer.ToArray());
}

Expand Down
2 changes: 1 addition & 1 deletion Core/src/Umbrella.Utilities/Umbrella.Utilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<PropertyGroup>
<PackageId>Umbrella.Utilities</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.WebUtilities.ModelState</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.WebUtilities</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.DataAccess.Abstractions</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.DataAccess.EF6</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.DataAccess.EntityFrameworkCore</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.DataAccess.Mapping</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.DataAccess.MultiTenant.EF6</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.DataAccess.MultiTenant</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.FileSystem.Abstractions</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.FileSystem.AzureStorage</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.FileSystem.Disk</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.Legacy.Utilities</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
2 changes: 1 addition & 1 deletion Legacy/src/Umbrella.Legacy.WCF/Umbrella.Legacy.WCF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.Legacy.WCF</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.Legacy.WebUtilities.Bundling</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.Legacy.WebUtilities.DataAnnotations</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.Legacy.WebUtilities.DynamicImage</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.Legacy.WebUtilities</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<PropertyGroup>
<PackageId>Umbrella.Extensions.Logging.ApplicationInsights</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.Extensions.Logging.Azure</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.Extensions.Logging.Log4Net.Azure</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.Extensions.Logging.Log4Net</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>Umbrella.TypeScript.Annotations</PackageId>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PropertyGroup>
<PackageId>Umbrella.TypeScript.Aurelia.Tools</PackageId>
<PackageType>DotnetCliTool</PackageType>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<Authors>Richard Edwards</Authors>
<Company>Zinofi Digital Ltd</Company>
<Copyright>Zinofi Digital Ltd 2019</Copyright>
Expand Down
Loading

0 comments on commit b59d9f1

Please sign in to comment.