Skip to content

Commit

Permalink
adds new function STR_TRIM (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoSoaresDev authored Jun 30, 2024
1 parent a630947 commit 5c9fe96
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/references/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,18 @@ void testFormatChicago(String expression, String expectedResult)
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);
}
}

0 comments on commit 5c9fe96

Please sign in to comment.