Skip to content

Commit 0b446ec

Browse files
authored
Humanizer Truncate should always be used as "Humanizer.Truncate" (#132)
1 parent 3785ba7 commit 0b446ec

File tree

6 files changed

+76
-23
lines changed

6 files changed

+76
-23
lines changed

src/Handlebars.Net.Helpers.Core/Attributes/HandlebarsWriterAttribute.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using HandlebarsDotNet.Helpers.Enums;
1+
using HandlebarsDotNet.Helpers.Enums;
32

43
namespace HandlebarsDotNet.Helpers.Attributes;
54

src/Handlebars.Net.Helpers.Core/Options/HandlebarsHelpersOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace HandlebarsDotNet.Helpers.Options;
1010
/// </summary>
1111
public class HandlebarsHelpersOptions
1212
{
13-
private const string Dot = ".";
13+
public const string Dot = ".";
1414

1515
/// <summary>
1616
/// An array of the default allowed HandlebarsHelpers.

src/Handlebars.Net.Helpers.Humanizer/Helpers/HumanizerHelpers.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,12 @@ public string Titleize(string value)
133133
[HandlebarsWriter(WriterType.String)]
134134
public string ToMetric(object value)
135135
{
136-
switch (value)
136+
return value switch
137137
{
138-
case int intValue:
139-
return intValue.ToMetric();
140-
141-
case double doubleValue:
142-
return doubleValue.ToMetric();
143-
144-
default:
145-
throw new NotSupportedException($"The value '{value}' must be an int or double.");
146-
}
138+
int intValue => intValue.ToMetric(),
139+
double doubleValue => doubleValue.ToMetric(),
140+
_ => throw new NotSupportedException($"The value '{value}' must be an int or double.")
141+
};
147142
}
148143

149144
[HandlebarsWriter(WriterType.String)]
@@ -218,7 +213,7 @@ public string Transform(string value, string? transformer)
218213
return value.Transform(MapToStringTransformer(transformer));
219214
}
220215

221-
[HandlebarsWriter(WriterType.String)]
216+
[HandlebarsWriter(WriterType.String, $"{nameof(Humanizer)}.{nameof(Truncate)}")]
222217
public string Truncate(string value, int length, string? separator = null, string? truncator = null, string? truncateFrom = null)
223218
{
224219
if (string.IsNullOrWhiteSpace(separator))

src/Handlebars.Net.Helpers/HandlebarsHelpers.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private static void RegisterEvaluateHelper(IHandlebars handlebarsContext)
184184

185185
private static void RegisterCustomHelper(IHandlebars handlebarsContext, HandlebarsHelpersOptions options, string categoryPrefix, IHelpers helper)
186186
{
187-
Type helperClassType = helper.GetType();
187+
var helperClassType = helper.GetType();
188188

189189
var methods = helperClassType.GetMethods()
190190
.Select(methodInfo => new
@@ -220,15 +220,16 @@ private static void RegisterCustomHelper(IHandlebars handlebarsContext, Handleba
220220
private static string GetName(MethodInfo methodInfo, HandlebarsWriterAttribute attribute, HandlebarsHelpersOptions options, string categoryPrefix)
221221
{
222222
var names = new List<string>();
223-
if (attribute.Name is { } && !string.IsNullOrWhiteSpace(attribute.Name))
223+
if (!string.IsNullOrWhiteSpace(attribute.Name))
224224
{
225-
names.Add(attribute.Name);
225+
var modifiedName = attribute.Name!.Replace(HandlebarsHelpersOptions.Dot, options.PrefixSeparator);
226+
names.Add(modifiedName);
226227
}
227228
else
228229
{
229-
if (options.Prefix is { } && !string.IsNullOrWhiteSpace(options.Prefix))
230+
if (!string.IsNullOrWhiteSpace(options.Prefix))
230231
{
231-
names.Add(options.Prefix);
232+
names.Add(options.Prefix!);
232233
}
233234

234235
if (options.UseCategoryPrefix)
@@ -333,7 +334,7 @@ IHelperOptions options
333334
parameterCountRequired--;
334335
}
335336

336-
if (model is { })
337+
if (model != null)
337338
{
338339
numberOfArguments += 1;
339340
}
@@ -374,7 +375,7 @@ IHelperOptions options
374375
var parsedArguments = ArgumentsParser.Parse(context, methodInfo.GetParameters(), arguments);
375376

376377
// Add null for optional arguments
377-
for (int i = 0; i < parameterCountAllowed.Max() - numberOfArguments; i++)
378+
for (var i = 0; i < parameterCountAllowed.Max() - numberOfArguments; i++)
378379
{
379380
parsedArguments.Add(null);
380381
}
@@ -384,7 +385,7 @@ IHelperOptions options
384385
parsedArguments.Insert(0, true);
385386
}
386387

387-
if (model is { })
388+
if (model != null)
388389
{
389390
parsedArguments.Insert(0, model);
390391
}
@@ -416,6 +417,6 @@ IHelperOptions options
416417
/// </summary>
417418
private static string[] CreateHelperNames(string helperName)
418419
{
419-
return new[] { helperName, $"[{helperName}]" };
420+
return [helperName, $"[{helperName}]"];
420421
}
421422
}

test/Handlebars.Net.Helpers.Tests/Templates/HumanizerHelpersTemplateTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public void HumanizeDateTime()
4545
result.Should().Be("yesterday");
4646
}
4747

48+
49+
4850
[Theory]
4951
[InlineData("{{[Humanizer.Humanize] \"HTML\"}}", "HTML")]
5052
[InlineData("{{[Humanizer.Humanize] \"PascalCaseInputStringIsTurnedIntoSentence\"}}", "Pascal case input string is turned into sentence")]
@@ -59,4 +61,41 @@ public void HumanizeString(string template, string expected)
5961
// Assert
6062
result.Should().Be(expected);
6163
}
64+
65+
[Theory]
66+
[InlineData("{{[Humanizer.Truncate] \"This is a long sentence that needs truncating.\" 10}}", "This is a…")]
67+
[InlineData("{{[Humanizer.Truncate] \"Short sentence.\" 20}}", "Short sentence.")]
68+
[InlineData("{{[Humanizer.Truncate] \"Exact length.\" 13}}", "Exact length.")]
69+
[InlineData("{{[Humanizer.Truncate] \"This sentence will be truncated to zero.\" 0}}", "")]
70+
[InlineData("{{[Humanizer.Truncate] \"This is a test for truncating with custom ellipsis.\" 15 \"[...]\"}}", "This is a [...]")]
71+
public void HumanizeTruncate(string template, string expected)
72+
{
73+
// Arrange
74+
var action = _handlebarsContext.Compile(template);
75+
76+
// Act
77+
var result = action("");
78+
79+
// Assert
80+
result.Should().Be(expected);
81+
}
82+
83+
[Fact]
84+
public void HumanizeTruncateWithCategoryPrefix()
85+
{
86+
// Arrange
87+
var handlebarsContext = Handlebars.Create();
88+
HandlebarsHelpers.Register(handlebarsContext, o =>
89+
{
90+
o.PrefixSeparator = "_";
91+
});
92+
var template = "{{[Humanizer_Truncate] \"This is a long sentence that needs truncating.\" 10}}";
93+
var action = handlebarsContext.Compile(template);
94+
95+
// Act
96+
var result = action("");
97+
98+
// Assert
99+
result.Should().Be("This is a…");
100+
}
62101
}

test/Handlebars.Net.Helpers.Tests/Templates/StringHelpersTemplateTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Globalization;
44
using FluentAssertions;
55
using HandlebarsDotNet.Compiler;
6+
using HandlebarsDotNet.Helpers.Enums;
67
using HandlebarsDotNet.Helpers.Models;
78
using HandlebarsDotNet.Helpers.Utils;
89
using Moq;
@@ -538,6 +539,24 @@ public void ToWrappedString_Null()
538539
decoded.Should().Be("test abc");
539540
}
540541

542+
[Fact]
543+
public void Truncate()
544+
{
545+
// Arrange
546+
var handlebarsContext = Handlebars.Create();
547+
HandlebarsHelpers.Register(handlebarsContext, o =>
548+
{
549+
o.UseCategoryPrefix = false;
550+
o.Categories = [Category.String, Category.Humanizer];
551+
});
552+
var action = handlebarsContext.Compile("{{Truncate \"This is a long sentence that needs truncating.\" 10 }}");
553+
554+
// Act
555+
var result = action("");
556+
557+
result.Should().Be("This is a ");
558+
}
559+
541560
[Fact]
542561
public void Format_Template_Now()
543562
{

0 commit comments

Comments
 (0)