Skip to content

Commit

Permalink
Add case normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Bardin08 committed Jun 2, 2024
1 parent 74ffef7 commit 8c2a74b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Globalization;
using System.Text;

namespace Core.Extensions;

public static class StringExtensions
{
public static string ToPascalCase(this string text)
{
if (text.Length < 1)
return text;

var words = text.Split(new[] { ' ', '-', '_', '.' }, StringSplitOptions.RemoveEmptyEntries);

var sb = new StringBuilder();

foreach (var word in words)
{
if (word.Length > 0)
{
sb.Append(CultureInfo.InvariantCulture.TextInfo.ToTitleCase(word.ToLowerInvariant()));
}
}

return sb.ToString();
}
}
20 changes: 17 additions & 3 deletions src/Core/Mappers/UserPreferencesMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Core.Models.UserPreferences;
using Core.Extensions;
using Core.Models.UserPreferences;
using Infrastructure.Persistence.Mongo.Entities.Preferences;
using MongoDB.Bson;

Expand Down Expand Up @@ -26,15 +27,28 @@ public static UserPreferences UpdateEntity(UserPreferences e, UserPreferencesDto
var channels = e.Channels;
foreach (var ch in dto.Channels)
{
if (channels.TryGetValue(ch.Key, out var channel))
for (var i = 0; i < ch.Value.Metadata?.Keys.Count; i++)
{
var key = ch.Value.Metadata.Keys.ElementAt(i);
var normalized = ch.Key.ToPascalCase();

if (!key.Equals(normalized))
{
ch.Value.Metadata[normalized] = ch.Value.Metadata[key];
ch.Value.Metadata.Remove(key);
}
}

var normalizedKey = ch.Key.ToPascalCase();
if (channels.TryGetValue(normalizedKey, out var channel))
{
channel.Enabled = ch.Value.Enabled;
channel.Description = ch.Value.Description;
channel.Metadata = ch.Value.Metadata;
}
else
{
channels.Add(ch.Key, new ChannelDescriptorBase
channels.Add(normalizedKey, new ChannelDescriptorBase
{
Enabled = ch.Value.Enabled,
Description = ch.Value.Description,
Expand Down

0 comments on commit 8c2a74b

Please sign in to comment.