Skip to content

Commit

Permalink
Replace snake_case and kebab-case.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejball committed Apr 29, 2019
1 parent 462d7b3 commit 48a8ae5
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/FindReplaceCode/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ private IEnumerable<KeyValuePair<string, string>> GetEnhancedFindReplacePairs(Ke
enhancedKeyValuePair = TryGetEnhancedFindReplacePairs(searchReplacePair, x => x.ToUpperInvariant());
if (enhancedKeyValuePair != null)
yield return enhancedKeyValuePair.Value;

enhancedKeyValuePair = TryGetEnhancedFindReplacePairs(searchReplacePair, x => ToSnakeCase(x, '_'));
if (enhancedKeyValuePair != null)
yield return enhancedKeyValuePair.Value;

enhancedKeyValuePair = TryGetEnhancedFindReplacePairs(searchReplacePair, x => ToSnakeCase(x, '-'));
if (enhancedKeyValuePair != null)
yield return enhancedKeyValuePair.Value;
}

private static KeyValuePair<string, string>? TryGetEnhancedFindReplacePairs(KeyValuePair<string, string> searchReplacePair, Func<string, string> transform)
Expand Down Expand Up @@ -286,6 +294,17 @@ private static string RenderMatchingGuid(Match match, Guid value)
return oldText != oldText.ToUpperInvariant() ? newText : newText.ToUpperInvariant();
}

private static string ToSnakeCase(string value, char separator)
{
if (value == null)
return null;

var words = GetWords(value);
return string.Join(separator.ToString(), words.Select(x => x.ToLowerInvariant()));
}

private static string[] GetWords(string value) => s_word.Matches(value).Select(x => x.ToString()).ToArray();

private const string c_guidPattern = @"\{[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}\}";

private static readonly string s_fullUsageMessage = string.Join(Environment.NewLine, new[]
Expand All @@ -294,6 +313,7 @@ private static string RenderMatchingGuid(Match match, Guid value)
});

private static readonly Regex s_hiddenDirectoryRegex = new Regex(@"[\\/]\..*[\\/]", RegexOptions.CultureInvariant);
private static readonly Regex s_word = new Regex("[A-Z]([A-Z]*(?![a-z])|[a-z]*)|[a-z]+|[0-9]+", RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture);

private readonly string m_folderPath;
private readonly ReadOnlyCollection<KeyValuePair<string, string>> m_searchReplaceArgs;
Expand Down

0 comments on commit 48a8ae5

Please sign in to comment.