Skip to content

Commit a7bb964

Browse files
committed
added Text.Zip
1 parent fcbd03f commit a7bb964

10 files changed

Lines changed: 104 additions & 23 deletions

File tree

Blade/Blade/Tags/LineBreaks.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ public static Regex Replacer(string names, bool open = true, bool close = true)
1414
return new Regex("<" + closer + names + "[^>]*>", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
1515
}
1616

17-
private static readonly Regex NewLine = new Regex(@"[\r\n]");
1817
/// <summary>
1918
/// Convert \n into line-breaks
2019
/// </summary>
2120
/// <param name="value"></param>
2221
/// <returns></returns>
2322
public static string Nl2Br(string value)
2423
{
25-
return NewLine.Replace(value, "<br>");
24+
return Text.Nl2X(value, "<br>");
2625
}
2726

2827

2928
private static readonly Regex Br = Replacer("br");
29+
3030
/// <summary>
3131
/// Convert <br> and <br/> into line-breaks
3232
/// </summary>

Blade/Blade/Tags/Remove.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ public static string Remove(string original)
1515
sanitizedText = sanitizedText.Replace("<", " ").Replace(">", " ");
1616

1717
// combine resulting multi-spaces
18-
sanitizedText = Regex.Replace(sanitizedText, "\\s{2,}", " ");
18+
sanitizedText = Text.ShrinkSpaces(sanitizedText);// Regex.Replace(sanitizedText, "\\s{2,}", " ");
1919

2020
return sanitizedText.Trim();
2121
}
22-
22+
23+
2324
}
2425
}

Blade/Blade/Text/Crop.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Connect.Razor.Internals;
2+
3+
namespace Connect.Razor.Blade
4+
{
5+
public static partial class Text
6+
{
7+
/// <summary>
8+
/// Cut off a text at the best possible place with a max-length.
9+
/// This will count html-entities like &amp; &nbsp; or umlauts as 1 character,
10+
/// and will try to cut off between words if possible.
11+
/// </summary>
12+
/// <param name="value">String to cut off. Can contain umlauts and html-entities, but should not contain html-tags as there are not treated properly.</param>
13+
/// <param name="length">length to cut off at</param>
14+
/// <returns></returns>
15+
public static string Crop(string value, int length)
16+
{
17+
return Truncator.SafeTruncate(value, length);
18+
}
19+
20+
}
21+
}

Blade/Blade/Text/Ellipsis.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ namespace Connect.Razor.Blade
44
{
55
public static partial class Text
66
{
7-
/// <summary>
8-
/// Cut off a text at the best possible place with a max-length.
9-
/// This will count html-entities like &amp; &nbsp; or umlauts as 1 character,
10-
/// and will try to cut off between words if possible.
11-
/// </summary>
12-
/// <param name="value">String to cut off. Can contain umlauts and html-entities, but should not contain html-tags as there are not treated properly.</param>
13-
/// <param name="length">length to cut off at</param>
14-
/// <returns></returns>
15-
public static string Crop(string value, int length)
16-
{
17-
return Truncator.SafeTruncate(value, length);
18-
}
197

208
/// <summary>
219
/// Crop a text if too long, add in that case, also add an ellipsis or a custom suffix

Blade/Blade/Text/HasText.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Connect.Razor.Internals;
1+
using System;
2+
using System.Text.RegularExpressions;
3+
using Connect.Razor.Internals;
24

35
namespace Connect.Razor.Blade
46
{
@@ -25,7 +27,7 @@ public static bool Has(object value, bool handleHtmlWhitespaces = true)
2527
public static bool Has(string value, bool handleHtmlWhitespaces = true)
2628
{
2729
// do quick-check, as this will usually be all it needs
28-
if(string.IsNullOrWhiteSpace(value))
30+
if(String.IsNullOrWhiteSpace(value))
2931
return false;
3032

3133
// if it got here and we don't want to re-check for html-whitespace, then we do have text
@@ -36,8 +38,7 @@ public static bool Has(string value, bool handleHtmlWhitespaces = true)
3638
foreach (var whitespace in Defaults.HtmlNonBreakingSpaces)
3739
value = value.Replace(whitespace, " ");
3840

39-
return !string.IsNullOrWhiteSpace(value);
41+
return !String.IsNullOrWhiteSpace(value);
4042
}
41-
4243
}
4344
}

Blade/Blade/Text/Zip.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Text.RegularExpressions;
2+
using Connect.Razor.Internals;
3+
4+
namespace Connect.Razor.Blade
5+
{
6+
public static partial class Text
7+
{
8+
/// <summary>
9+
/// Will remove all new-lines from a string and merge multiple spaces together
10+
/// </summary>
11+
/// <param name="value"></param>
12+
/// <returns></returns>
13+
public static string Zip(string value)
14+
{
15+
return string.IsNullOrEmpty(value)
16+
? value
17+
: ShrinkSpaces(Nl2X(value, " "));
18+
}
19+
20+
internal static string ShrinkSpaces(string value)
21+
{
22+
return Regex.Replace(value, @"\s{2,}", " ");
23+
}
24+
25+
internal static readonly Regex NewLine = new Regex(@"[\r\n]");
26+
27+
internal static string Nl2X(string value, string replacement)
28+
{
29+
return NewLine.Replace(value, replacement);
30+
}
31+
}
32+
}

Blade/RazorBlade.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
</ItemGroup>
3838
<ItemGroup>
3939
<Compile Include="Blade\Tags\LineBreaks.cs" />
40+
<Compile Include="Blade\Text\Zip.cs" />
41+
<Compile Include="Blade\Text\Crop.cs" />
4042
<Compile Include="Internals\Defaults.cs" />
4143
<Compile Include="Blade\Dic\SafeGet.cs" />
4244
<Compile Include="Blade\Dic\Dynamic.cs" />

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ The goal is to provide helpers for very common code snippets or functions, which
1010
_Sometimes you need the first 100 characters followed by an ellipsis (if truncated), but umlauts like `&uuml;` will mess up your count or might even be cut off. This is automatically handled by:_
1111

1212
```razor
13+
@* just cut it off at the visible character count, not splitting words *@
1314
@Text.Crop(someText, 100)
15+
16+
@* truncate a text and if necessary, add ellipsis character *@
17+
@Text.Ellipsis(longText, 100)
1418
```
1519

1620
_Or sometimes you need a value, but if it's empty, you need another one. So instead of writing:_
@@ -37,9 +41,6 @@ _Note that HTML whitespace like `&nbsp;` will also be treated as empty, unless y
3741
@* remove html from a wysiwyg-string *@
3842
@Tags.Remove(formattedText)
3943
40-
@* truncate a text and if necessary, add ellipsis character *@
41-
@Text.Ellipsis(longText, 100)
42-
4344
@* the same with a custom ending *@
4445
@Text.Ellipsis(longText, 100, "...more")
4546
@@ -72,6 +73,7 @@ This is a short summary of the most used variations of the helpers. Further deta
7273
1. `Text.Ellipsis(value, length)`
7374
1. `Text.Has(value)`
7475
1. `Text.First(value, value[, moreValues, ...])`
76+
1. `Text.Zip(value)`
7577

7678
## Commands in v0.1 to Shorten Texts Correctly
7779

@@ -95,6 +97,10 @@ This is a short summary of the most used variations of the helpers. Further deta
9597

9698
1. `Text.First(intendedValue, next-value, next-value, [up to 5 values], false)` - same behavior as above, values will be checked in the order given. By ending with `false` html-whitespace will not be cleaned but treated as text.
9799

100+
### Commands to Clean up Text
101+
102+
1. `Text.Zip(value)` will remove line-breaks and shrink all multiple-spaces into one single space.
103+
98104
## Commands in v0.1 to Convert Html to Text or Back
99105

100106
1. `Tags.Remove(htmlText)` - strips the html from an string, ensuring that all tags will cause 1 spaces between words, but only one (multiple spaces are shortened to 1 again)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Connect.Razor.Internals;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Connect.Razor.Blade;
4+
5+
6+
namespace Razor_Blades_Tests
7+
{
8+
[TestClass]
9+
public class Test_Blades_Zip
10+
{
11+
12+
[TestMethod]
13+
public void Test_Zip_Basic()
14+
{
15+
var message = "This is a teaser for something";
16+
var expected = "This is a teaser for something";
17+
Assert.AreEqual(expected, Text.Zip(message), "multiple spaces must go");
18+
}
19+
20+
[TestMethod]
21+
public void Test_Zip_NewLine()
22+
{
23+
var message = "This is a \n teaser\n for something";
24+
var expected = "This is a teaser for something";
25+
Assert.AreEqual(expected, Text.Zip(message), "multiple spaces must go");
26+
}
27+
28+
}
29+
}

Razor Blades Tests/Tests for RazorBlade.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Reference Include="System.Core" />
5050
</ItemGroup>
5151
<ItemGroup>
52+
<Compile Include="Test_Blades_Zip.cs" />
5253
<Compile Include="Test_Blades_Html.cs" />
5354
<Compile Include="Test_Blades_HasText.cs" />
5455
<Compile Include="Test_Blades_FirstText.cs" />

0 commit comments

Comments
 (0)