Skip to content

Commit

Permalink
Improve replace
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoraes committed Jul 2, 2024
1 parent c67350c commit 0ad0519
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions JexlNet.ExtendedGrammar/ExtendedGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,11 @@ public static JsonNode Join(JsonNode input, JsonNode separator)
/// <returns>A string with all occurrences of old replaced by new</returns>
public static JsonNode Replace(JsonNode input, JsonNode old, JsonNode newStr)
{
if (input is JsonValue value && old is JsonValue oldValue && newStr is JsonValue newValue)
if (input is JsonValue value && old is JsonValue oldValue)
{
string str = value.ToString();
string oldStr = oldValue.ToString();
string newStrStr = newValue.ToString();
string newStrStr = newStr is JsonValue newValue ? newValue.ToString() : "";
return str.Replace(oldStr, newStrStr);
}
return null;
Expand Down
11 changes: 11 additions & 0 deletions JexlNet.Test/ExtendedGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ public void Contains(string expression, bool expected)
Assert.Equal(expected, result);
}

[Theory]
[InlineData("replace('foo-bar', '-', '_')", "foo_bar")]
[InlineData("replace('foo-bar----', '-', '')", "foobar")]
[InlineData("'123ab123ab123ab'|replace('123')", "ababab")]
public void Replace(string expression, string expected)
{
var jexl = new Jexl(new ExtendedGrammar());
var result = jexl.Eval(expression);
Assert.Equal(expected, result?.ToString());
}

[Fact]
public void Split()
{
Expand Down

0 comments on commit 0ad0519

Please sign in to comment.