Skip to content

Commit

Permalink
Update StringExtensions and add RangeExtensions. Update libraries (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva authored Aug 1, 2024
2 parents 96f8f31 + 6f4440c commit 6b4c1bb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/TinyHelpers.AspNetCore/TinyHelpers.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.6.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
33 changes: 33 additions & 0 deletions src/TinyHelpers/Extensions/RangeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace TinyHelpers.Extensions;

#if NET6_0_OR_GREATER
/// <summary>
/// Contains extension methods for the <see cref="Range"/> type.
/// </summary>
/// <seealso cref="Range"/>
public static class RangeExtensions
{
/// <summary>
/// Returns an enumerator that iterates through the range of integers defined by the specified <see cref="Range"/>.
/// </summary>
/// <param name="range">The range of integers to iterate over.</param>
/// <returns>An enumerator that can be used to iterate through the range of integers.</returns>
public static IEnumerator<int> GetEnumerator(this Range range)
{
if (range.Start.IsFromEnd || range.Start.Value >= range.End.Value)
{
for (var i = range.Start.Value; i >= range.End.Value; i--)
{
yield return i;
}
}
else
{
for (var i = range.Start.Value; i <= range.End.Value; i++)
{
yield return i;
}
}
}
}
#endif
6 changes: 4 additions & 2 deletions src/TinyHelpers/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public static string ReplaceIgnoreCase(this string input, string pattern, string
/// <param name="defaultValue">The value to return if the string is <see langword="null"/> or contains only whitespaces.</param>
/// <returns>The actual value of this string instance, or <paramref name="defaultValue"/> if the string is <see langword="null"/> or contains only whitespaces.</returns>
[return: NotNullIfNotNull(nameof(input))]
public static string? GetValueOrDefault([NotNullIfNotNull(nameof(input))] this string? input, string? defaultValue)
[return: NotNullIfNotNull(nameof(defaultValue))]
public static string? GetValueOrDefault(this string? input, string? defaultValue)
=> input.GetValueOrDefault(defaultValue, whiteSpaceAsEmpty: true);

/// <summary>
Expand All @@ -81,7 +82,8 @@ public static string ReplaceIgnoreCase(this string input, string pattern, string
/// <param name="whiteSpaceAsEmpty"><see langword="true"/> if whitespaces must be considered as empty characters; otherwise, <see langword="false"/>.</param>
/// <returns>The actual value of this string instance, or <paramref name="defaultValue"/> if the string is <see langword="null"/> or contains only whitespaces.</returns>
[return: NotNullIfNotNull(nameof(input))]
public static string? GetValueOrDefault([NotNullIfNotNull(nameof(input))] this string? input, string? defaultValue, bool whiteSpaceAsEmpty)
[return: NotNullIfNotNull(nameof(defaultValue))]
public static string? GetValueOrDefault(this string? input, string? defaultValue, bool whiteSpaceAsEmpty)
=> whiteSpaceAsEmpty ? (string.IsNullOrWhiteSpace(input) ? defaultValue : input) : (string.IsNullOrEmpty(input) ? defaultValue : input);

/// <summary>
Expand Down

0 comments on commit 6b4c1bb

Please sign in to comment.