-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utilities): add various extension methods
- Loading branch information
Showing
6 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2023 Maintainers of NUKE. | ||
// Distributed under the MIT License. | ||
// https://github.com/nuke-build/nuke/blob/master/LICENSE | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Nuke.Common.Utilities.Collections; | ||
|
||
partial class EnumerableExtensions | ||
{ | ||
private static readonly Random s_randomNumberGenerator = new Random(); | ||
|
||
public static T Random<T>(this IEnumerable<T> collection) | ||
{ | ||
var array = collection.ToArray(); | ||
return array[s_randomNumberGenerator.Next(array.Length)]; | ||
} | ||
|
||
public static ICollection<T> Randomize<T>(this ICollection<T> collection) | ||
{ | ||
var list = collection.ToList(); | ||
var count = list.Count; | ||
while (count > 1) { | ||
count--; | ||
var k = s_randomNumberGenerator.Next(count + 1); | ||
(list[k], list[count]) = (list[count], list[k]); | ||
} | ||
|
||
return list; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2023 Maintainers of NUKE. | ||
// Distributed under the MIT License. | ||
// https://github.com/nuke-build/nuke/blob/master/LICENSE | ||
|
||
using System; | ||
using System.Linq; | ||
|
||
namespace Nuke.Common.Utilities; | ||
|
||
partial class ObjectExtensions | ||
{ | ||
public static TOutput Apply<TInput, TOutput>(this TInput input, Func<TInput, TOutput> transform) | ||
{ | ||
return transform.Invoke(input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2023 Maintainers of NUKE. | ||
// Distributed under the MIT License. | ||
// https://github.com/nuke-build/nuke/blob/master/LICENSE | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
|
||
namespace Nuke.Common.Utilities; | ||
|
||
[PublicAPI] | ||
[DebuggerNonUserCode] | ||
[DebuggerStepThrough] | ||
public static partial class TaskExtensions | ||
{ | ||
public static void WaitAll(this IEnumerable<Task> tasks) | ||
{ | ||
var tasksArray = tasks.ToArray(); | ||
Task.WaitAll(tasksArray); | ||
} | ||
|
||
public static IReadOnlyCollection<T> WaitAll<T>(this IEnumerable<Task<T>> tasks) | ||
{ | ||
var tasksArray = tasks.ToArray(); | ||
Task.WaitAll(tasksArray); | ||
return tasksArray.Select(x => x.Result).ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2023 Maintainers of NUKE. | ||
// Distributed under the MIT License. | ||
// https://github.com/nuke-build/nuke/blob/master/LICENSE | ||
|
||
using System; | ||
using System.Linq; | ||
|
||
namespace Nuke.Common.Utilities; | ||
|
||
partial class StringExtensions | ||
{ | ||
public static string Truncate(this string str, int maxChars) | ||
{ | ||
return str.Length <= maxChars ? str : str.Substring(0, maxChars) + "…"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2023 Maintainers of NUKE. | ||
// Distributed under the MIT License. | ||
// https://github.com/nuke-build/nuke/blob/master/LICENSE | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using JetBrains.Annotations; | ||
|
||
namespace Nuke.Common.Utilities; | ||
|
||
[PublicAPI] | ||
[DebuggerNonUserCode] | ||
[DebuggerStepThrough] | ||
public static class UrlExtensions | ||
{ | ||
public static Uri WithUtmValues(this Uri uri, string medium, string source, string campaign = null, string content = null) | ||
{ | ||
var lastSegment = uri.Segments.Last().Trim('/').Apply(x => x.IsNullOrWhiteSpace() ? null : x); | ||
|
||
var dictionary = new Dictionary<string, string> | ||
{ | ||
["utm_medium"] = medium.NotNullOrWhiteSpace(), | ||
["utm_source"] = source.NotNullOrWhiteSpace(), | ||
["utm_campaign"] = campaign ?? lastSegment, | ||
["utm_content"] = content ?? (campaign != null ? lastSegment : null), | ||
}; | ||
|
||
var query = dictionary.Where(x => x.Value != null).Select(x => $"{x.Key}={x.Value}").Join("&"); | ||
return new Uri(uri.AbsoluteUri + "?" + query); | ||
} | ||
} |