Skip to content

Commit

Permalink
Add StartsWith and EndsWith to extended grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoraes committed Sep 5, 2024
1 parent f4f8174 commit 76ab78d
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion JexlNet.ExtendedGrammar/ExtendedGrammar.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -80,6 +80,14 @@ public ExtendedGrammar()
AddFunction("includes", Contains);
AddFunction("$includes", Contains);
AddTransform("includes", Contains);
// StartsWith
AddFunction("startsWith", StartsWith);
AddFunction("$startsWith", StartsWith);
AddTransform("startsWith", StartsWith);
// EndsWith
AddFunction("endsWith", EndsWith);
AddFunction("$endsWith", EndsWith);
AddTransform("endsWith", EndsWith);
// Split
AddFunction("split", Split);
AddFunction("$split", Split);
Expand Down Expand Up @@ -564,6 +572,38 @@ public static JsonNode Contains(JsonNode input, JsonNode pattern)
return false;
}

/// <summary>
/// Returns true if the string str starts with the character sequence chars.
/// </summary>
/// <example><code>startsWith(str, chars)</code><code>$startsWith(str, chars)</code><code>str|startsWith(chars)</code></example>
/// <returns>true if the string str starts with the character sequence chars</returns>
public static JsonNode StartsWith(JsonNode input, JsonNode chars)
{
if (input is JsonValue value && chars is JsonValue charsValue)
{
string str = value.ToString();
string charsStr = charsValue.ToString();
return str.StartsWith(charsStr);
}
return false;
}

/// <summary>
/// Returns true if the string str ends with the character sequence chars.
/// </summary>
/// <example><code>endsWith(str, chars)</code><code>$endsWith(str, chars)</code><code>str|endsWith(chars)</code></example>
/// <returns>true if the string str ends with the character sequence chars</returns>
public static JsonNode EndsWith(JsonNode input, JsonNode chars)
{
if (input is JsonValue value && chars is JsonValue charsValue)
{
string str = value.ToString();
string charsStr = charsValue.ToString();
return str.EndsWith(charsStr);
}
return false;
}

/// <summary>
/// Splits a string into an array of substrings based on a specified separator.
/// </summary>
Expand Down

0 comments on commit 76ab78d

Please sign in to comment.