Skip to content

Commit

Permalink
Wordify accepts preserved words
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsmith1968 committed Apr 19, 2020
1 parent 7901bf8 commit 2a904fa
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
1 change: 1 addition & 0 deletions DNX.Helpers/DNX.Helpers.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Description>.NET Extensions and helpers for Core and Common .NET types</Description>
<Authors>Martin Smith</Authors>
Expand Down
33 changes: 31 additions & 2 deletions DNX.Helpers/Strings/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -655,18 +655,47 @@ public static bool IsValidNumber(this string text, CultureInfo cultureInfo)
/// i.e. insert a space before each uppercase letter that is
/// either preceded by a lowercase letter or followed by a
/// lowercase letter (but not for the first char in string).
/// This keeps groups of uppercase letters - e.g. acronyms - together.
/// This keeps groups of uppercase letters - e.g. preservedWords - together.
/// </summary>
/// <param name="titleCaseString">A string in PascalCase</param>
/// <returns></returns>
public static string Wordify(this string titleCaseString)
{
return titleCaseString.Wordify(null);
}

/// <summary>
/// Add spaces to separate the capitalized words in the string,
/// i.e. insert a space before each uppercase letter that is
/// either preceded by a lowercase letter or followed by a
/// lowercase letter (but not for the first char in string).
/// This keeps groups of uppercase letters - e.g. acronyms - together.
/// </summary>
/// <param name="titleCaseString">A string in PascalCase</param>
/// <param name="preservedWords">The preservedWords - beyond acronyms</param>
/// <returns>System.String.</returns>
public static string Wordify(this string titleCaseString, IList<string> preservedWords)
{
if (string.IsNullOrWhiteSpace(titleCaseString))
return titleCaseString;

return WordifyRegex
var text = WordifyRegex
.Replace(titleCaseString, " ${x}")
.Replace(" ", " ");

if (preservedWords.HasAny())
{
var acronymDict = preservedWords
.Distinct()
.ToDictionary(x => x.Wordify(), x => x)
;

text = acronymDict.Aggregate(text,
(current, dict) => current.Replace(dict.Key, dict.Value)
);
}

return text;
}
}
}
29 changes: 20 additions & 9 deletions Test.DNX.Helpers/Strings/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using DNX.Helpers.Strings;
using NUnit.Framework;
using Shouldly;
Expand Down Expand Up @@ -545,17 +546,27 @@ public string Test_CoalesceNullOrWhitespace(string a, string b, string c)
return result;
}

[TestCase(null, null)]
[TestCase("", "")]
[TestCase("TwoWords", "Two Words")]
[TestCase("ThreeWordsTogether", "Three Words Together")]
[TestCase("pascalCase", "pascal Case")]
[TestCase("Already Spaced", "Already Spaced")]
[TestCase("AnEntireSentenceSquashedTogetherIntoOneSingleWord", "An Entire Sentence Squashed Together Into One Single Word")]
public void Wordify_Tests(string text, string expected)
[TestCase(null, null, null)]
[TestCase("", null, "")]
[TestCase("TwoWords", null, "Two Words")]
[TestCase("ThreeWordsTogether", null, "Three Words Together")]
[TestCase("pascalCase", null, "pascal Case")]
[TestCase("Already Spaced", null, "Already Spaced")]
[TestCase("AnEntireSentenceSquashedTogetherIntoOneSingleWord", null, "An Entire Sentence Squashed Together Into One Single Word")]
[TestCase("IsITVDramaBetterThanBBCDrama", null, "Is ITV Drama Better Than BBC Drama")]
[TestCase("IsTheHoLAnOutdatedInstitution", null, "Is The Ho L An Outdated Institution")]
[TestCase("IsTheHoLAnOutdatedInstitution", "HoL", "Is The HoL An Outdated Institution")]
[TestCase("VirusesLikeH1N1AndH5N1WereWarningsForThePanDemicToCome", null, "Viruses Like H1N1 And H5N1 Were Warnings For The Pan Demic To Come")]
[TestCase("VirusesLikeH1N1AndH5N1WereWarningsForThePanDemicToCome", "H1N1|H5N1|PanDemic", "Viruses Like H1N1 And H5N1 Were Warnings For The PanDemic To Come")]
public void Wordify_Tests(string text, string preservedWordsText, string expected)
{
// Arrange
var preservedWords = preservedWordsText?
.Split("|")
.ToArray();

// Act
var result = text.Wordify();
var result = text.Wordify(preservedWords);

// Assert
result.ShouldBe(expected);
Expand Down
1 change: 1 addition & 0 deletions Test.DNX.Helpers/Test.DNX.Helpers.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>latest</LangVersion>
<Authors>Martin Smith</Authors>
<Description>Tests for DNX.Helpers</Description>
<Copyright>Copyright © 2016-2019 DNX Solutions Ltd</Copyright>
Expand Down

0 comments on commit 2a904fa

Please sign in to comment.