Skip to content

Commit

Permalink
Change JoinStr to filter empty/whitespace only text
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-strecker-sonarsource committed May 3, 2022
1 parent 278d630 commit 93ed614
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions analyzers/src/SonarAnalyzer.Common/Helpers/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,35 +118,32 @@ public static int IndexOf<T>(this IEnumerable<T> enumerable, Func<T, bool> predi
?.index ?? -1;

/// <summary>
/// This is string.Join() as extension. Concatenates members of collection using specified separator between each member. Selector is used to extract string value from T for concatenation.
/// This is <see cref="string.Join"/> as extension. Concatenates members of collection using specified <paramref name="separator"/> between each member.
/// <paramref name="selector"/> is used to convert <typeparamref name="T"/> to <see cref="string"/> for concatenation. Any whitespace or <see langword="null"/>
/// <see cref="string"/> will be ignored.
/// </summary>
public static string JoinStr<T>(this IEnumerable<T> enumerable, string separator, Func<T, string> selector) =>
string.Join(separator, enumerable.Select(x => selector(x)));

/// <summary>
/// This is string.Join() as extension. Concatenates members of collection using specified separator between each member. Selector is used to extract integer value from T for concatenation.
/// </summary>
public static string JoinStr<T>(this IEnumerable<T> enumerable, string separator, Func<T, int> selector) =>
string.Join(separator, enumerable.Select(x => selector(x)));
string.Join(separator, enumerable.Select(x => selector(x)).Where(x => !string.IsNullOrWhiteSpace(x)));

/// <summary>
/// This is string.Join() as extension. Concatenates members of string collection using specified separator between each member.
/// This is <see cref="string.Join"/> as extension. Concatenates members of collection using specified <paramref name="separator"/> between each member.
/// Any whitespace or <see langword="null"/> <see cref="string"/> will be ignored.
/// </summary>
public static string JoinStr(this IEnumerable<string> enumerable, string separator) =>
JoinStr(enumerable, separator, x => x);
string.Join(separator, enumerable.Where(x => !string.IsNullOrWhiteSpace(x)));

/// <summary>
/// Concatenates the members of a <see cref="string"/> collection using the specified <paramref name="separator"/> between each member.
/// Any whitespace or null member of the collection will be ignored.
/// This is <see cref="string.Join"/> as extension. Concatenates members of collection using specified <paramref name="separator"/> between each member.
/// <paramref name="selector"/> is used to convert <typeparamref name="T"/> to <see cref="int"/> for concatenation.
/// </summary>
public static string JoinIfNotWhitespace(this IEnumerable<string> enumerable, string separator) =>
string.Join(separator, enumerable.Where(x => !string.IsNullOrWhiteSpace(x)));
public static string JoinStr<T>(this IEnumerable<T> enumerable, string separator, Func<T, int> selector) =>
string.Join(separator, enumerable.Select(x => selector(x)));

/// <summary>
/// This is string.Join() as extension. Concatenates members of int collection using specified separator between each member.
/// This is <see cref="string.Join"/> as extension. Concatenates members of collection using specified <paramref name="separator"/> between each member.
/// </summary>
public static string JoinStr(this IEnumerable<int> enumerable, string separator) =>
JoinStr(enumerable, separator, x => x);
string.Join(separator, enumerable);


}
Expand Down

0 comments on commit 93ed614

Please sign in to comment.