From 5c9fe968440f97c9cbcf66c386a719367a677bd5 Mon Sep 17 00:00:00 2001 From: Leonardo Soares <54752833+LeonardoSoaresDev@users.noreply.github.com> Date: Sun, 30 Jun 2024 11:40:09 -0300 Subject: [PATCH] adds new function STR_TRIM (#486) --- docs/references/functions.md | 1 + .../config/ExpressionConfiguration.java | 1 + .../functions/string/StringTrimFunction.java | 38 +++++++++++++++++++ .../functions/string/StringFunctionsTest.java | 14 +++++++ 4 files changed, 54 insertions(+) create mode 100644 src/main/java/com/ezylang/evalex/functions/string/StringTrimFunction.java diff --git a/docs/references/functions.md b/docs/references/functions.md index 8cee7e6..d79f8bc 100644 --- a/docs/references/functions.md +++ b/docs/references/functions.md @@ -40,6 +40,7 @@ Available through the _ExpressionConfiguration.StandardFunctionsDictionary_ cons | STR_FORMAT(format [,argument, ...]) | Returns a formatted string using the specified format string and arguments, using the configured locale | | STR_LOWER(value) | Converts the given value to lower case | | STR_STARTS_WITH(string, substring) | Returns true if the string starts with the substring (case-sensitive) | +| STR_TRIM(string) | Returns the given string with all leading and trailing space removed. | | STR_UPPER(value) | Converts the given value to upper case | ### Trigonometric Functions diff --git a/src/main/java/com/ezylang/evalex/config/ExpressionConfiguration.java b/src/main/java/com/ezylang/evalex/config/ExpressionConfiguration.java index 7063426..3abf2d2 100644 --- a/src/main/java/com/ezylang/evalex/config/ExpressionConfiguration.java +++ b/src/main/java/com/ezylang/evalex/config/ExpressionConfiguration.java @@ -185,6 +185,7 @@ public class ExpressionConfiguration { Map.entry("STR_FORMAT", new StringFormatFunction()), Map.entry("STR_LOWER", new StringLowerFunction()), Map.entry("STR_STARTS_WITH", new StringStartsWithFunction()), + Map.entry("STR_TRIM", new StringTrimFunction()), Map.entry("STR_UPPER", new StringUpperFunction()), // date time functions Map.entry("DT_DATE_NEW", new DateTimeNewFunction()), diff --git a/src/main/java/com/ezylang/evalex/functions/string/StringTrimFunction.java b/src/main/java/com/ezylang/evalex/functions/string/StringTrimFunction.java new file mode 100644 index 0000000..889df87 --- /dev/null +++ b/src/main/java/com/ezylang/evalex/functions/string/StringTrimFunction.java @@ -0,0 +1,38 @@ +/* + Copyright 2012-2024 Udo Klimaschewski + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package com.ezylang.evalex.functions.string; + +import com.ezylang.evalex.EvaluationException; +import com.ezylang.evalex.Expression; +import com.ezylang.evalex.data.EvaluationValue; +import com.ezylang.evalex.functions.AbstractFunction; +import com.ezylang.evalex.functions.FunctionParameter; +import com.ezylang.evalex.parser.Token; + +/** + * Returns the given string with all leading and trailing space removed. + * + * @author LeonardoSoaresDev + */ +@FunctionParameter(name = "string") +public class StringTrimFunction extends AbstractFunction { + @Override + public EvaluationValue evaluate( + Expression expression, Token functionToken, EvaluationValue... parameterValues) + throws EvaluationException { + return expression.convertValue(parameterValues[0].getStringValue().trim()); + } +} diff --git a/src/test/java/com/ezylang/evalex/functions/string/StringFunctionsTest.java b/src/test/java/com/ezylang/evalex/functions/string/StringFunctionsTest.java index d173190..7da46dd 100644 --- a/src/test/java/com/ezylang/evalex/functions/string/StringFunctionsTest.java +++ b/src/test/java/com/ezylang/evalex/functions/string/StringFunctionsTest.java @@ -151,4 +151,18 @@ class StringFunctionsTest extends BaseEvaluationTest { assertExpressionHasExpectedResult( expression, expectedResult, TestConfigurationProvider.ChicagoConfiguration); } + + @ParameterizedTest + @CsvSource( + delimiter = ':', + value = { + "STR_TRIM(\" Two whitespace \") : Two whitespace", + "STR_TRIM(\" Left whitespace\") : Left whitespace", + "STR_TRIM(\"Right whitespace \") : Right whitespace", + "STR_TRIM(\"No whitespace\") : No whitespace" + }) + void testTrimString(String expression, String expectedResult) + throws EvaluationException, ParseException { + assertExpressionHasExpectedResult(expression, expectedResult); + } } -- 2.51.2