Skip to content

Commit

Permalink
Merge pull request #13 from martinsmith1968/feature/extra-extensions-…
Browse files Browse the repository at this point in the history
…from-apps

Feature/extra extensions from apps
  • Loading branch information
martinsmith1968 authored Apr 19, 2020
2 parents a623605 + 2a904fa commit 6a07b14
Show file tree
Hide file tree
Showing 6 changed files with 523 additions and 11 deletions.
9 changes: 5 additions & 4 deletions DNX.Helpers/DNX.Helpers.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Description>.NET Extensions and helpers for Core and Common .NET types</Description>
<Authors>Martin Smith</Authors>
Expand All @@ -12,9 +13,9 @@
<PackageIconUrl>https://raw.githubusercontent.com/martinsmith1968/DNX.Helpers/master/images/favicon-32x32.png</PackageIconUrl>
<PackageTags>DNX helpers extensions string array linq</PackageTags>
<PackageReleaseNotes>Interpolation to a working version and some preparation for moving to .NET Standard</PackageReleaseNotes>
<Version>2.0.8</Version>
<AssemblyVersion>2.0.8</AssemblyVersion>
<FileVersion>2.0.8</FileVersion>
<Version>2.1.0</Version>
<AssemblyVersion>2.1.0</AssemblyVersion>
<FileVersion>2.1.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>bin\DNX.Helpers.xml</DocumentationFile>
Expand Down Expand Up @@ -70,4 +71,4 @@
<DependentUpon>GuardBuiltInTypesTemplate.tt</DependentUpon>
</Compile>
</ItemGroup>
</Project>
</Project>
113 changes: 113 additions & 0 deletions DNX.Helpers/Internal/RunSafely.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.Threading.Tasks;

namespace DNX.Helpers.Internal
{
/// <summary>
/// RunSafely - execute code with defaulted exception handling
/// </summary>
public class RunSafely
{
/// <summary>
/// Executes the specified action.
/// </summary>
/// <param name="action">The action.</param>
/// <param name="exceptionHandler">The exception handler.</param>
public static void Execute(Action action, Action<Exception> exceptionHandler = null)
{
try
{
action.Invoke();
}
catch (Exception ex)
{
Execute(() => exceptionHandler?.Invoke(ex));
}
}

/// <summary>
/// Executes the specified function.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="func">The function.</param>
/// <param name="exceptionHandler">The exception handler.</param>
/// <returns>T.</returns>
public static T Execute<T>(Func<T> func, Action<Exception> exceptionHandler = null)
{
return Execute(func, default, exceptionHandler);
}

/// <summary>
/// Executes the specified function.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="func">The function.</param>
/// <param name="defaultValue">The default value.</param>
/// <param name="exceptionHandler">The exception handler.</param>
/// <returns>T.</returns>
public static T Execute<T>(Func<T> func, T defaultValue, Action<Exception> exceptionHandler = null)
{
try
{
return func.Invoke();
}
catch (Exception ex)
{
Execute(() => exceptionHandler?.Invoke(ex));

return defaultValue;
}
}

/// <summary>
/// execute as an asynchronous operation.
/// </summary>
/// <param name="task">The task.</param>
/// <param name="exceptionHandler">The exception handler.</param>
public static async Task ExecuteAsync(Task task, Action<Exception> exceptionHandler = null)
{
try
{
await task.ConfigureAwait(false);
}
catch (Exception ex)
{
Execute(() => exceptionHandler?.Invoke(ex));
}
}

/// <summary>
/// execute as an asynchronous operation.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="task">The task.</param>
/// <param name="exceptionHandler">The exception handler.</param>
/// <returns>Task&lt;T&gt;.</returns>
public static async Task<T> ExecuteAsync<T>(Task<T> task, Action<Exception> exceptionHandler = null)
{
return await ExecuteAsync(task, default, exceptionHandler);
}

/// <summary>
/// execute as an asynchronous operation.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="task">The task.</param>
/// <param name="defaultValue">The default value.</param>
/// <param name="exceptionHandler">The exception handler.</param>
/// <returns>Task&lt;T&gt;.</returns>
public static async Task<T> ExecuteAsync<T>(Task<T> task, T defaultValue, Action<Exception> exceptionHandler = null)
{
try
{
return await task.ConfigureAwait(false);
}
catch (Exception ex)
{
Execute(() => exceptionHandler?.Invoke(ex));

return defaultValue;
}
}
}
}
54 changes: 54 additions & 0 deletions DNX.Helpers/Strings/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Text.RegularExpressions;
using DNX.Helpers.Linq;

// ReSharper disable InconsistentNaming

// ReSharper disable InvertIf
// ReSharper disable LoopCanBeConvertedToQuery

Expand All @@ -32,6 +34,10 @@ public enum SplitDelimiterType
/// </summary>
public static class StringExtensions
{
private const string SI_WORDIFY_REGEX_TEXT = "(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])";

private static readonly Regex WordifyRegex = new Regex(SI_WORDIFY_REGEX_TEXT, RegexOptions.Compiled);

/// <summary>
/// Formats the specified arguments and text
/// </summary>
Expand Down Expand Up @@ -643,5 +649,53 @@ public static bool IsValidNumber(this string text, CultureInfo cultureInfo)

return Regex.IsMatch(text, pattern);
}

/// <summary>
/// Add spaces to separate the capitalized words in the string,
/// i.e. insert a space before each uppercase letter that is
/// either preceded by a lowercase letter or followed by a
/// lowercase letter (but not for the first char in string).
/// This keeps groups of uppercase letters - e.g. preservedWords - together.
/// </summary>
/// <param name="titleCaseString">A string in PascalCase</param>
/// <returns></returns>
public static string Wordify(this string titleCaseString)
{
return titleCaseString.Wordify(null);
}

/// <summary>
/// Add spaces to separate the capitalized words in the string,
/// i.e. insert a space before each uppercase letter that is
/// either preceded by a lowercase letter or followed by a
/// lowercase letter (but not for the first char in string).
/// This keeps groups of uppercase letters - e.g. acronyms - together.
/// </summary>
/// <param name="titleCaseString">A string in PascalCase</param>
/// <param name="preservedWords">The preservedWords - beyond acronyms</param>
/// <returns>System.String.</returns>
public static string Wordify(this string titleCaseString, IList<string> preservedWords)
{
if (string.IsNullOrWhiteSpace(titleCaseString))
return titleCaseString;

var text = WordifyRegex
.Replace(titleCaseString, " ${x}")
.Replace(" ", " ");

if (preservedWords.HasAny())
{
var acronymDict = preservedWords
.Distinct()
.ToDictionary(x => x.Wordify(), x => x)
;

text = acronymDict.Aggregate(text,
(current, dict) => current.Replace(dict.Key, dict.Value)
);
}

return text;
}
}
}
Loading

0 comments on commit 6a07b14

Please sign in to comment.