Skip to content

Commit c8d27f8

Browse files
committed
Peer edits
1 parent b647f62 commit c8d27f8

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

src/Elastic.Markdown/Myst/InlineParsers/Substitution/SubstitutionMutationHelper.cs

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ public static (string Key, string[] Mutations) ParseKeyWithMutations(string rawK
2929
return (key, mutations);
3030
}
3131

32+
/// <summary>
33+
/// Applies mutations to a value using the existing SubstitutionMutation system
34+
/// </summary>
35+
/// <param name="value">The original value to transform</param>
36+
/// <param name="mutations">Collection of SubstitutionMutation enums to apply</param>
37+
/// <returns>The transformed value</returns>
38+
public static string ApplyMutations(string value, IReadOnlyCollection<SubstitutionMutation> mutations)
39+
{
40+
var result = value;
41+
foreach (var mutation in mutations)
42+
{
43+
result = ApplySingleMutation(result, mutation);
44+
}
45+
return result;
46+
}
47+
3248
/// <summary>
3349
/// Applies mutations to a value using the existing SubstitutionMutation system
3450
/// </summary>
@@ -43,34 +59,41 @@ public static string ApplyMutations(string value, string[] mutations)
4359
var trimmedMutation = mutationStr.Trim();
4460
if (SubstitutionMutationExtensions.TryParse(trimmedMutation, out var mutation, true, true))
4561
{
46-
// Use the same logic as SubstitutionRenderer.Write
47-
var (success, update) = mutation switch
48-
{
49-
SubstitutionMutation.MajorComponent => TryGetVersion(result, v => $"{v.Major}"),
50-
SubstitutionMutation.MajorX => TryGetVersion(result, v => $"{v.Major}.x"),
51-
SubstitutionMutation.MajorMinor => TryGetVersion(result, v => $"{v.Major}.{v.Minor}"),
52-
SubstitutionMutation.IncreaseMajor => TryGetVersion(result, v => $"{v.Major + 1}.0.0"),
53-
SubstitutionMutation.IncreaseMinor => TryGetVersion(result, v => $"{v.Major}.{v.Minor + 1}.0"),
54-
SubstitutionMutation.LowerCase => (true, result.ToLowerInvariant()),
55-
SubstitutionMutation.UpperCase => (true, result.ToUpperInvariant()),
56-
SubstitutionMutation.Capitalize => (true, Capitalize(result)),
57-
SubstitutionMutation.KebabCase => (true, ToKebabCase(result)),
58-
SubstitutionMutation.CamelCase => (true, ToCamelCase(result)),
59-
SubstitutionMutation.PascalCase => (true, ToPascalCase(result)),
60-
SubstitutionMutation.SnakeCase => (true, ToSnakeCase(result)),
61-
SubstitutionMutation.TitleCase => (true, TitleCase(result)),
62-
SubstitutionMutation.Trim => (true, Trim(result)),
63-
_ => (false, result)
64-
};
65-
if (success)
66-
{
67-
result = update;
68-
}
62+
result = ApplySingleMutation(result, mutation);
6963
}
7064
}
7165
return result;
7266
}
7367

68+
/// <summary>
69+
/// Applies a single mutation to a value
70+
/// </summary>
71+
/// <param name="value">The value to transform</param>
72+
/// <param name="mutation">The mutation to apply</param>
73+
/// <returns>The transformed value</returns>
74+
private static string ApplySingleMutation(string value, SubstitutionMutation mutation)
75+
{
76+
var (success, result) = mutation switch
77+
{
78+
SubstitutionMutation.MajorComponent => TryGetVersion(value, v => $"{v.Major}"),
79+
SubstitutionMutation.MajorX => TryGetVersion(value, v => $"{v.Major}.x"),
80+
SubstitutionMutation.MajorMinor => TryGetVersion(value, v => $"{v.Major}.{v.Minor}"),
81+
SubstitutionMutation.IncreaseMajor => TryGetVersion(value, v => $"{v.Major + 1}.0.0"),
82+
SubstitutionMutation.IncreaseMinor => TryGetVersion(value, v => $"{v.Major}.{v.Minor + 1}.0"),
83+
SubstitutionMutation.LowerCase => (true, value.ToLowerInvariant()),
84+
SubstitutionMutation.UpperCase => (true, value.ToUpperInvariant()),
85+
SubstitutionMutation.Capitalize => (true, Capitalize(value)),
86+
SubstitutionMutation.KebabCase => (true, ToKebabCase(value)),
87+
SubstitutionMutation.CamelCase => (true, ToCamelCase(value)),
88+
SubstitutionMutation.PascalCase => (true, ToPascalCase(value)),
89+
SubstitutionMutation.SnakeCase => (true, ToSnakeCase(value)),
90+
SubstitutionMutation.TitleCase => (true, TitleCase(value)),
91+
SubstitutionMutation.Trim => (true, Trim(value)),
92+
_ => (false, value)
93+
};
94+
return success ? result : value;
95+
}
96+
7497
private static (bool Success, string Result) TryGetVersion(string version, Func<SemVersion, string> transform)
7598
{
7699
if (!SemVersion.TryParse(version, out var v) && !SemVersion.TryParse(version + ".0", out v))

src/Elastic.Markdown/Myst/InlineParsers/Substitution/SubstitutionParser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ protected override void Write(HtmlRenderer renderer, SubstitutionLeaf leaf)
6767
}
6868

6969
// Apply mutations using shared utility
70-
var mutationStrings = leaf.Mutations.Select(m => m.ToStringFast(true)).ToArray();
71-
replacement = SubstitutionMutationHelper.ApplyMutations(replacement, mutationStrings);
70+
replacement = SubstitutionMutationHelper.ApplyMutations(replacement, leaf.Mutations);
7271
_ = renderer.Write(replacement);
7372
}
7473
}

0 commit comments

Comments
 (0)