Skip to content

Commit

Permalink
Merge pull request #20 from ottorinobruni/add-base64
Browse files Browse the repository at this point in the history
Add base64
  • Loading branch information
ottorinobruni authored May 24, 2024
2 parents d08e50b + 4623218 commit 1c96962
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 14 deletions.
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
TextCase is a powerful .NET library designed to simplify text manipulation by providing a variety of case conversion methods.

Key Features
- Wide Range of Conversions: Transform text into 17 different cases including UpperCase, LowerCase, TitleCase, CamelCase, PascalCase, and more.
- Wide Range of Conversions: Transform text into 19 different cases including UpperCase, LowerCase, TitleCase, CamelCase, PascalCase, Base64 encoding/decoding, and more.
- Text Analysis: Easily count characters, words, letters, and sentences within your text.
- Easy Integration: Simple and intuitive API that integrates seamlessly with your .NET applications.

Expand All @@ -27,14 +27,17 @@ Available Conversions
- CapitaliseWordsCase: Capitalizes the first letter of each word.
- ReverseCase: Reverses the text.
- AlternateCase: Alternates case starting with uppercase.
- Base64EncodeCase: Encodes text to Base64.
- Base64DecodeCase: Decodes Base64 text.

It's possible to count in the text:
Text Analysis Examples:
- number of characters
- number of words
- number of letters
- number of sentences
- number of paragraphs

## Usage:
## Usage Examples:

```cscharp
// YOU TALKING TO ME?
Expand Down Expand Up @@ -101,6 +104,17 @@ TextCase.Convert("You talking to me?", Case.InverseCase);
TextCase.Convert("You talking to me?", Case.TrainCase);
"You talking to me?".ToTrainCase();
// Encode text to Base64
TextCase.Convert("You talking to me?", Case.Base64EncodeCase);
"You talking to me?".ToBase64EncodeCase();
// Decode Base64 text
TextCase.Convert("WW91IHRhbGtpbmcgdG8gbWU/", Case.Base64DecodeCase);
"WW91IHRhbGtpbmcgdG8gbWU?".ToBase64DecodeCase();
```
Text Analysis Functions

```cscharp
// Text Count
TextCase.GetTextCount("You talking to me?");
"You talking to me?".GetTextCount();
Expand All @@ -116,6 +130,11 @@ TextCase.GetLettersCount("You talking to me?");
// Sentences Count
TextCase.GetSentencesCount("You talking to me?");
"You talking to me?".GetSentencesCount();
// Paragraphs Count
TextCase.GetParagraphsCount("You talking to me?");
"You talking to me?".GetParagraphsCount();
```

## Get it on NuGet:
Expand Down
48 changes: 48 additions & 0 deletions TextCase.UnitTests/Base64DecodeCaseConverterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Buffers.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TextCase.Converters;
using TextCase.Extensions;
using Xunit;

namespace TextCase.UnitTests
{
public class Base64DecodeCaseConverterTest
{
[Theory]
[InlineData("SGVsbG8gV29ybGQ=", "Hello World")]
[InlineData("SEVMTE8gV09STEQ=", "HELLO WORLD")]
[InlineData("SUNIIEJJTiBHTMOcQ0tMSUNI", "ICH BIN GLÜCKLICH")]
[InlineData("ICBDaGUgT3JlIFNvbm8/IA==", " Che Ore Sono? ")]
public void ToBase64DecodeCase_WhenValidInput_ShouldReturnDecodedString(string input, string expected)
{
// Setup
var service = new Base64DecodeCaseConverter();

// Execute
var actual = service.Convert(input);

// Assert
Assert.Equal(expected, actual);
}

[Theory]
[InlineData("SGVsbG8gV29ybGQ=", "Hello World")]
[InlineData("SEVMTE8gV09STEQ=", "HELLO WORLD")]
[InlineData("SUNIIEJJTiBHTMOcQ0tMSUNI", "ICH BIN GLÜCKLICH")]
[InlineData("ICBDaGUgT3JlIFNvbm8/IA==", " Che Ore Sono? ")]
public void ToBase64DecodeCase_WhenBase64DecodeCase_TextShouldBeBase64DecodeCase(string input, string output)
{
// Execute
var convertedText = input.ToBase64DecodeCase();

// Assert
var expected = output;
var actual = convertedText;
Assert.Equal(expected, actual);
}
}
}
48 changes: 48 additions & 0 deletions TextCase.UnitTests/Base64EncodeCaseConverterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Buffers.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TextCase.Converters;
using TextCase.Extensions;
using Xunit;

namespace TextCase.UnitTests
{
public class Base64EncodeCaseConverterTest
{
[Theory]
[InlineData("Hello World", "SGVsbG8gV29ybGQ=")]
[InlineData("HELLO WORLD", "SEVMTE8gV09STEQ=")]
[InlineData("ICH BIN GLÜCKLICH", "SUNIIEJJTiBHTMOcQ0tMSUNI")]
[InlineData(" Che Ore Sono? ", "ICBDaGUgT3JlIFNvbm8/IA==")]
public void ToBase64EncodeCase_WhenValidInput_ShouldReturnBase64EncodedString(string input, string expected)
{
// Setup
var service = new Base64EncodeCaseConverter();

// Execute
var actual = service.Convert(input);

// Assert
Assert.Equal(expected, actual);
}

[Theory]
[InlineData("Hello World", "SGVsbG8gV29ybGQ=")]
[InlineData("HELLO WORLD", "SEVMTE8gV09STEQ=")]
[InlineData("ICH BIN GLÜCKLICH", "SUNIIEJJTiBHTMOcQ0tMSUNI")]
[InlineData(" Che Ore Sono? ", "ICBDaGUgT3JlIFNvbm8/IA==")]
public void ToBase64EncodeCase_WhenBase64EncodeCase_TextShouldBeBase64EncodeCase(string input, string output)
{
// Execute
var convertedText = input.ToBase64EncodeCase();

// Assert
var expected = output;
var actual = convertedText;
Assert.Equal(expected, actual);
}
}
}
27 changes: 25 additions & 2 deletions TextCase.UnitTests/StringExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ public void GetLettersCount_WhenInput_LettersShouldBeCount(string input, int cou

[Theory]
[InlineData("", 0)]
[InlineData("hello world. icH bIn glückLICH", 2)]
[InlineData(" che ore sono? ", 1)]
[InlineData(null, 0)]
[InlineData("This is a single sentence.", 1)]
[InlineData("This is the first sentence. This is the second one! Is this the third? Yes, it is.", 4)]
[InlineData("First sentence.\nSecond sentence!\nThird sentence?", 3)]
[InlineData("First sentence. Second sentence! Third sentence? ", 3)]
[InlineData("First sentence.Second sentence!Third sentence?", 3)]
[InlineData("This is a sentence with a Mr Smith. And here's another one! Finally, this is the last one?", 3)]
public void GetSentencesCount_WhenInput_SentencesShouldBeCount(string input, int count)
{
// Execute
Expand All @@ -67,5 +72,23 @@ public void GetSentencesCount_WhenInput_SentencesShouldBeCount(string input, int
var actual = result;
Assert.Equal(expected, actual);
}

[Theory]
[InlineData("", 0)]
[InlineData(null, 0)]
[InlineData("This is a single paragraph.", 1)]
[InlineData("This is the first paragraph.\n\nThis is the second paragraph.\n\nThis is the third paragraph.", 3)]
[InlineData("First paragraph.\r\n\r\nSecond paragraph.\r\n\r\nThird paragraph.", 3)]
[InlineData("First paragraph. \n\n Second paragraph! \n\n Third paragraph? ", 3)]
[InlineData("First paragraph.\nSecond paragraph! \n\nThird paragraph?", 2)]
[InlineData("First paragraph.\n\n\nSecond paragraph!\n\n\n\nThird paragraph?", 3)]
public void GetParagraphsCount_VariousInputs_ReturnsExpectedResult(string input, int expected)
{
// Act
int result = TextCase.GetParagraphsCount(input);

// Assert
Assert.Equal(expected, result);
}
}
}
34 changes: 34 additions & 0 deletions TextCase/Converters/Base64DecodeCaseConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Text;

namespace TextCase.Converters
{
/// <summary>
/// Represents a base64 decode converter.
/// </summary>
public class Base64DecodeCaseConverter : ICaseConverter
{
/// <summary>
/// Converts the specified text to base64 decode.
/// </summary>
/// <param name="text">The string to convert to base64 decode.</param>
/// <returns>The specified text converted to base64 decode.</returns>
public string Convert(string text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}

try
{
return Encoding.UTF8.GetString(System.Convert.FromBase64String(text));
}
catch (FormatException)
{
return text;
}
}
}

}
23 changes: 23 additions & 0 deletions TextCase/Converters/Base64EncodeCaseConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Text;

namespace TextCase.Converters
{
/// <summary>
/// Represents a base64 encode converter.
/// </summary>
public class Base64EncodeCaseConverter : ICaseConverter
{
/// <summary>
/// Converts the specified text to base64 encode.
/// </summary>
/// <param name="text">The string to convert to base64 encode.</param>
/// <returns>The specified text converted to base64 encode.</returns>
public string Convert(string text)
{
return string.IsNullOrWhiteSpace(text) ?
string.Empty :
System.Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
}
}

}
58 changes: 50 additions & 8 deletions TextCase/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ public static int GetTextCount(this string value)
/// <returns>The number of words in the current string.</returns>
public static int GetWordsCount(this string value)
{
return string.IsNullOrEmpty(value) ? 0 : value.Split(new[] { ' ', '\t', '\n', '\r', '.', ',', ';', '!', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
if (string.IsNullOrWhiteSpace(value))
{
return 0;
}
var words = Regex.Matches(value, @"\b\w+\b");
return words.Count;
}

/// <summary>
Expand Down Expand Up @@ -63,15 +68,32 @@ public static int GetSentencesCount(this string value)
return 0;
}

// Split sentences based on common sentence-ending punctuation marks.
var sentenceSeparators = new char[] { '.', '!', '?' };
var sentences = value.Split(sentenceSeparators, StringSplitOptions.RemoveEmptyEntries)
.Where(sentence => !string.IsNullOrWhiteSpace(sentence))
.ToArray();
var sentenceSeparators = new string[] { ".", "!", "?" };
var sentenceExtraSeparators = new string[] { ". " };
var demo = value.Split(sentenceSeparators, StringSplitOptions.RemoveEmptyEntries);
var demo2 = value.Split(sentenceExtraSeparators, StringSplitOptions.RemoveEmptyEntries);

var result = demo.Count(sentence => !string.IsNullOrWhiteSpace(sentence));

return sentences.Length;
return value.Split(sentenceSeparators, StringSplitOptions.RemoveEmptyEntries)
.Count(sentence => !string.IsNullOrWhiteSpace(sentence));
}


/// <summary>
/// Gets the number of paragraphs in the current String value.
/// </summary>
/// <param name="value">The string to count.</param>
/// <returns>The number of paragraphs in the current string.</returns>
public static int GetParagraphsCount(this string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return 0;
}
return value.Split(new string[] { "\r\n\r\n", "\n\n" }, StringSplitOptions.RemoveEmptyEntries)
.Count(paragraph => !string.IsNullOrWhiteSpace(paragraph));
}

/// <summary>
/// Converts the specified string to uppercase using the casing rules of the invariant culture.
/// </summary>
Expand Down Expand Up @@ -231,5 +253,25 @@ public static string ToTrainCase(this string value)
{
return TextCase.Convert(value, Case.TrainCase);
}

/// <summary>
/// Converts the specified text to base64 decode case.
/// </summary>
/// <param name="value">The string to convert to base64 decode case.</param>
/// <returns>The specified text converted to base64 decode case.</returns>
public static string ToBase64DecodeCase(this string value)
{
return TextCase.Convert(value, Case.Base64DecodeCase);
}

/// <summary>
/// Converts the specified text to base64 encode case.
/// </summary>
/// <param name="value">The string to convert to base64 encode case.</param>
/// <returns>The specified text converted to base64 encode case.</returns>
public static string ToBase64EncodeCase(this string value)
{
return TextCase.Convert(value, Case.Base64EncodeCase);
}
}
}
9 changes: 8 additions & 1 deletion TextCase/TextCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public enum Case
ConstantCase,
CobolCase,
TrainCase,
InverseCase
InverseCase,
Base64EncodeCase,
Base64DecodeCase
}

public static class TextCase
Expand Down Expand Up @@ -50,5 +52,10 @@ public static int GetSentencesCount(string text)
{
return text.GetSentencesCount();
}

public static int GetParagraphsCount(string text)
{
return text.GetParagraphsCount();
}
}
}
2 changes: 2 additions & 0 deletions TextCase/TextCaseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ internal static ICaseConverter GetCaseConverter(Case textCase)
Case.CobolCase => new CobolCaseConverter(),
Case.TrainCase => new TrainCaseConverter(),
Case.InverseCase => new InverseCaseConverter(),
Case.Base64DecodeCase => new Base64DecodeCaseConverter(),
Case.Base64EncodeCase => new Base64EncodeCaseConverter(),
_ => throw new ArgumentException("No valid TextCase"),
};
}
Expand Down

0 comments on commit 1c96962

Please sign in to comment.