Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
741 additions
and
566 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,100 +1,95 @@ | ||
namespace Validot.Errors.Args | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
|
||
public sealed class NameArg : IArg | ||
{ | ||
private const string FormatParameter = "format"; | ||
|
||
private const string TitleCaseParameterValue = "titleCase"; | ||
|
||
private static readonly string[] KeyAsAllowedParameters = | ||
{ | ||
FormatParameter | ||
}; | ||
|
||
private static readonly IReadOnlyList<Regex> _titleCaseRegexes = new[] | ||
{ | ||
new Regex(@"([a-z])([A-Z][a-z])", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([A-Z][a-z])([A-Z])", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([a-z])([A-Z]+[a-z])", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([A-Z]+)([A-Z][a-z][a-z])", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([a-z]+)([A-Z0-9]+)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([A-Z]+)([A-Z][a-rt-z][a-z]*)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([0-9])([A-Z][a-z]+)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([A-Z]{2,})([0-9]{2,})", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([0-9]{2,})([A-Z]{2,})", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
}; | ||
|
||
private readonly string _name; | ||
|
||
public static string Name { get; } = "_name"; | ||
|
||
string IArg.Name => Name; | ||
|
||
public IReadOnlyCollection<string> AllowedParameters => KeyAsAllowedParameters; | ||
|
||
public string ToString(IReadOnlyDictionary<string, string> parameters) | ||
{ | ||
var formatParameter = parameters?.ContainsKey(FormatParameter) == true | ||
? parameters[FormatParameter] | ||
: null; | ||
|
||
if (formatParameter == null) | ||
{ | ||
return _name; | ||
} | ||
|
||
return Stringify(_name, formatParameter); | ||
} | ||
|
||
public NameArg(string name) | ||
{ | ||
ThrowHelper.NullArgument(name, nameof(name)); | ||
|
||
_name = name; | ||
} | ||
|
||
private static string Stringify(string value, string formatParameter) | ||
{ | ||
if (string.Equals(formatParameter, TitleCaseParameterValue, StringComparison.InvariantCulture)) | ||
{ | ||
return ConvertToTitleCase(value); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
// Title case method taken from https://stackoverflow.com/a/35953318/1633913 | ||
private static string ConvertToTitleCase(string input) | ||
{ | ||
if (string.IsNullOrWhiteSpace(input)) | ||
{ | ||
return input; | ||
} | ||
|
||
if (input.Contains("_")) | ||
{ | ||
input = input.Replace('_', ' '); | ||
input = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(input); | ||
} | ||
|
||
foreach (var regex in _titleCaseRegexes) | ||
{ | ||
input = regex.Replace(input, "$1 $2"); | ||
} | ||
|
||
input = input.Trim(); | ||
|
||
if (input.Length == 1) | ||
{ | ||
return char.ToUpperInvariant(input[0]).ToString(CultureInfo.InvariantCulture); | ||
} | ||
|
||
return char.ToUpperInvariant(input[0]).ToString(CultureInfo.InvariantCulture) + input.Substring(1); | ||
} | ||
} | ||
} | ||
namespace Validot.Errors.Args | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
|
||
public sealed class NameArg : IArg | ||
{ | ||
private const string FormatParameter = "format"; | ||
|
||
private const string TitleCaseParameterValue = "titleCase"; | ||
|
||
private static readonly string[] KeyAsAllowedParameters = | ||
{ | ||
FormatParameter | ||
}; | ||
|
||
private static readonly IReadOnlyList<Regex> _titleCaseRegexes = new[] | ||
{ | ||
new Regex(@"([a-z])([A-Z][a-z])", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([A-Z][a-z])([A-Z])", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([a-z])([A-Z]+[a-z])", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([A-Z]+)([A-Z][a-z][a-z])", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([a-z]+)([A-Z0-9]+)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([A-Z]+)([A-Z][a-rt-z][a-z]*)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([0-9])([A-Z][a-z]+)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([A-Z]{2,})([0-9]{2,})", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
new Regex(@"([0-9]{2,})([A-Z]{2,})", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200)), | ||
}; | ||
|
||
private readonly string _name; | ||
|
||
public static string Name { get; } = "_name"; | ||
|
||
string IArg.Name => Name; | ||
|
||
public IReadOnlyCollection<string> AllowedParameters => KeyAsAllowedParameters; | ||
|
||
public string ToString(IReadOnlyDictionary<string, string> parameters) | ||
{ | ||
var formatParameter = parameters?.ContainsKey(FormatParameter) == true | ||
? parameters[FormatParameter] | ||
: null; | ||
|
||
if (formatParameter == null) | ||
{ | ||
return _name; | ||
} | ||
|
||
return Stringify(_name, formatParameter); | ||
} | ||
|
||
public NameArg(string name) | ||
{ | ||
ThrowHelper.NullArgument(name, nameof(name)); | ||
|
||
_name = name; | ||
} | ||
|
||
private static string Stringify(string value, string formatParameter) | ||
{ | ||
if (string.Equals(formatParameter, TitleCaseParameterValue, StringComparison.InvariantCulture)) | ||
{ | ||
return ConvertToTitleCase(value); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
// Title case method taken from https://stackoverflow.com/a/35953318/1633913 | ||
private static string ConvertToTitleCase(string input) | ||
{ | ||
if (input.Contains("_")) | ||
{ | ||
input = input.Replace('_', ' '); | ||
input = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(input); | ||
} | ||
|
||
foreach (var regex in _titleCaseRegexes) | ||
{ | ||
input = regex.Replace(input, "$1 $2"); | ||
} | ||
|
||
input = input.Trim(); | ||
|
||
if (input.Length == 1) | ||
{ | ||
return char.ToUpperInvariant(input[0]).ToString(CultureInfo.InvariantCulture); | ||
} | ||
|
||
return char.ToUpperInvariant(input[0]).ToString(CultureInfo.InvariantCulture) + input.Substring(1); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.