File tree Expand file tree Collapse file tree 4 files changed +35
-5
lines changed
main/java/dev/vepo/jsonata
test/java/dev/vepo/jsonata Expand file tree Collapse file tree 4 files changed +35
-5
lines changed Original file line number Diff line number Diff line change 1+ package dev .vepo .jsonata .functions ;
2+
3+ import java .util .List ;
4+
5+ import dev .vepo .jsonata .functions .data .Data ;
6+ import dev .vepo .jsonata .functions .json .JsonFactory ;
7+
8+ public record TrimJSONataFunction (List <JSONataFunction > providers ) implements JSONataFunction {
9+ public TrimJSONataFunction {
10+ if (providers .size () != 1 ) {
11+ throw new IllegalArgumentException ("StringCast function must have 1 argument" );
12+ }
13+ }
14+ @ Override
15+ public Data map (Data original , Data current ) {
16+ return JsonFactory .stringValue (providers .get (0 ).map (original , current ).toJson ().asText ().trim ());
17+ }
18+
19+ }
Original file line number Diff line number Diff line change @@ -12,7 +12,8 @@ public enum BuiltInFunction {
1212 SUBSTRING_BEFORE ("$substringBefore" ),
1313 SUBSTRING_AFTER ("$substringAfter" ),
1414 LOWERCASE ("$lowercase" ),
15- UPPERCASE ("$uppercase" );
15+ UPPERCASE ("$uppercase" ),
16+ TRIM ("$trim" );
1617
1718 public static Optional <BuiltInFunction > get (String name ) {
1819 return Stream .of (values ())
Original file line number Diff line number Diff line change 5050import dev .vepo .jsonata .functions .SubstringAfterJSONataFunction ;
5151import dev .vepo .jsonata .functions .SubstringBeforeJSONataFunction ;
5252import dev .vepo .jsonata .functions .SubstringJSONataFunction ;
53+ import dev .vepo .jsonata .functions .TrimJSONataFunction ;
5354import dev .vepo .jsonata .functions .UppercaseJSONataFunction ;
5455import dev .vepo .jsonata .functions .UserDefinedFunctionJSONataFunction ;
5556import dev .vepo .jsonata .functions .WildcardJSONataFunction ;
@@ -153,17 +154,20 @@ public void exitFunctionCall(FunctionCallContext ctx) {
153154 .parameterStatement ()
154155 .size ()));
155156 case SUBSTRING_BEFORE -> new SubstringBeforeJSONataFunction (previous (ctx .functionStatement ()
156- .parameterStatement ()
157- .size ()));
157+ .parameterStatement ()
158+ .size ()));
158159 case SUBSTRING_AFTER -> new SubstringAfterJSONataFunction (previous (ctx .functionStatement ()
159- .parameterStatement ()
160- .size ()));
160+ .parameterStatement ()
161+ .size ()));
161162 case LOWERCASE -> new LowecaseJSONataFunction (previous (ctx .functionStatement ()
162163 .parameterStatement ()
163164 .size ()));
164165 case UPPERCASE -> new UppercaseJSONataFunction (previous (ctx .functionStatement ()
165166 .parameterStatement ()
166167 .size ()));
168+ case TRIM -> new TrimJSONataFunction (previous (ctx .functionStatement ()
169+ .parameterStatement ()
170+ .size ()));
167171 })
168172 .orElseGet (() -> Optional .ofNullable (this .blocks .peek ())
169173 .flatMap (block -> block .function (fnName ))
Original file line number Diff line number Diff line change @@ -281,6 +281,12 @@ void stringCaseTest() {
281281 assertThat (jsonata ("$lowercase(\" ABCDEF\" )" ).evaluate ("{}" ).asText ()).isEqualTo ("abcdef" );
282282 }
283283
284+
285+ @ Test
286+ void stringTrimTest () {
287+ assertThat (jsonata ("$trim(\" abcdef\t \" )" ).evaluate ("{}" ).asText ()).isEqualTo ("abcdef" );
288+ }
289+
284290 @ Test
285291 void sortTest () {
286292 assertThat (jsonata ("""
You can’t perform that action at this time.
0 commit comments