Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.0-beta.5
1.1.0-beta.6
20 changes: 12 additions & 8 deletions src/Common/Functions/F.Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,23 @@ public static string Format<T>(string formatString, T source, string? replaceIfN
var flags = BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance;
var value = source switch
{
// Source array - get specific item in array
// Source array - get specific item in array for numbered template
Array arr when numberedTemplates && templateNumber < arr.Length && arr.GetValue(templateNumber) is object val =>
val,

// Source array - get next item in array
Array arr when replaceIndex < arr.Length && arr.GetValue(replaceIndex++) is object val =>
// Source value - use string value but only for the first template, i.e. {0}
{ } obj when numberedTemplates && templateNumber == 0 && obj.ToString() is string val =>
val,

// Source object - get matching property value
{ } obj when typeof(T).GetProperty(template, flags)?.GetValue(obj) is object val =>
// Source array - get next item in array for named template
Array arr when !numberedTemplates && replaceIndex < arr.Length && arr.GetValue(replaceIndex++) is object val =>
val,

// Nothing matches to use
// Source object - get matching property value for named template
{ } obj when !numberedTemplates && typeof(T).GetProperty(template, flags)?.GetValue(obj) is object val =>
val,

// Nothing matches so put placeholder back
_ =>
$"{{{template}}}"
};
Expand All @@ -96,10 +100,10 @@ public static string Format<T>(string formatString, T source, string? replaceIfN
+ new string('}', endGroup.Captures.Count);
});

// Format string
// Format string with ordered values
var formatted = string.Format(DefaultCulture, rewrittenFormat, [.. values]);

// If the string still contains any templates, return original format string
// If the string still contains any placeholders, return original format string
return regex.IsMatch(formatted) ? formatString : formatted;
}

Expand Down
35 changes: 33 additions & 2 deletions tests/Tests.Common/Functions/F.Format_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ public void Returns_Replace(string? input)

public class With_Numbered_Placeholders
{
public class With_Single_Value_Source
{
[Fact]
public void Replaces_With_Value()
{
// Arrange
var format = "{0}/";
var value = Rnd.Guid;

// Act
var result = F.Format(format, value, string.Empty);

// Assert
Assert.Equal($"{value}/", result);
}
}

public class With_Object_Source
{
[Fact]
Expand Down Expand Up @@ -132,6 +149,22 @@ public void Replaces_With_Formatted_Values()

public class With_Named_Placeholders
{
public class With_Single_Value_Source
{
[Fact]
public void Returns_Format()
{
// Arrange
var format = "{bar}/";

// Act
var result = F.Format(format, Rnd.Lng, string.Empty);

// Assert
Assert.Equal(format, result);
}
}

public class With_Object_Source
{
[Fact]
Expand Down Expand Up @@ -263,6 +296,4 @@ public void Replaces_With_Values()
}
}
}


}