From e3d76587214a75f7af8b55f3c38336682334d35c Mon Sep 17 00:00:00 2001 From: petarpe Date: Wed, 26 Oct 2022 11:24:36 +0300 Subject: [PATCH 1/3] Add Pascal Case function --- .../java/com/bazaarvoice/jolt/Modifier.java | 1 + .../jolt/modifier/function/Strings.java | 29 ++++++++++++++++++- .../jolt/modifier/function/StringsTest.java | 5 ++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/jolt-core/src/main/java/com/bazaarvoice/jolt/Modifier.java b/jolt-core/src/main/java/com/bazaarvoice/jolt/Modifier.java index 7b70d25c..4e992c72 100644 --- a/jolt-core/src/main/java/com/bazaarvoice/jolt/Modifier.java +++ b/jolt-core/src/main/java/com/bazaarvoice/jolt/Modifier.java @@ -43,6 +43,7 @@ public abstract class Modifier implements SpecDriven, ContextualTransform { static { STOCK_FUNCTIONS.put( "toLower", new Strings.toLowerCase() ); STOCK_FUNCTIONS.put( "toUpper", new Strings.toUpperCase() ); + STOCK_FUNCTIONS.put( "toPascal", new Strings.toPascalCase() ); STOCK_FUNCTIONS.put( "concat", new Strings.concat() ); STOCK_FUNCTIONS.put( "join", new Strings.join() ); STOCK_FUNCTIONS.put( "split", new Strings.split() ); diff --git a/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Strings.java b/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Strings.java index 3af56224..e15a34cd 100644 --- a/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Strings.java +++ b/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Strings.java @@ -51,6 +51,33 @@ protected Optional applySingle( final Object arg ) { } } + public static final class toPascalCase extends Function.SingleFunction { + @Override + protected Optional applySingle( final Object arg ) { + + if ( ! (arg instanceof String) ) { + return Optional.empty(); + } + + String argString = (String) arg; + boolean capital = true; + StringBuilder result = new StringBuilder(); + for(int i=0; i< argString.length(); i++) { + char currentChar = argString.charAt(i); + if(Character.isLetter(currentChar) && capital) { + result.append(Character.toUpperCase(currentChar)); + capital = false; + } else if(Character.isLetter(currentChar)) { + result.append(Character.toLowerCase(currentChar)); + } else { + result.append(currentChar); + capital = true; + } + } + return Optional.of(result.toString()); + } + } + public static final class trim extends Function.SingleFunction { @Override protected Optional applySingle( final Object arg ) { @@ -233,4 +260,4 @@ private static Optional padString( boolean leftPad, String source, List< return Optional.empty(); } -} \ No newline at end of file +} diff --git a/jolt-core/src/test/java/com/bazaarvoice/jolt/modifier/function/StringsTest.java b/jolt-core/src/test/java/com/bazaarvoice/jolt/modifier/function/StringsTest.java index b6016458..a20a9656 100644 --- a/jolt-core/src/test/java/com/bazaarvoice/jolt/modifier/function/StringsTest.java +++ b/jolt-core/src/test/java/com/bazaarvoice/jolt/modifier/function/StringsTest.java @@ -32,6 +32,7 @@ public Iterator getTestCases() { List testCases = new LinkedList<>( ); Function SPLIT = new Strings.split(); + Function TO_PASCAL = new Strings.toPascalCase(); testCases.add( new Object[] {"split-invalid-null", SPLIT, null, Optional.empty() } ); testCases.add( new Object[] {"split-invalid-string", SPLIT, "", Optional.empty() } ); @@ -50,6 +51,10 @@ public Iterator getTestCases() { testCases.add( new Object[] {"split-regex-token-string", SPLIT, new Object[] {"[eE]", "test,TEST"}, Optional.of( Arrays.asList("t", "st,T", "ST") )} ); testCases.add( new Object[] {"split-regex2-token-string", SPLIT, new Object[] {"\\s+", "test TEST Test TeSt"}, Optional.of( Arrays.asList("test", "TEST", "Test", "TeSt") )} ); + testCases.add( new Object[] {"to-pascal-empty-string", TO_PASCAL, new Object[] {""}, Optional.of("")} ); + testCases.add( new Object[] {"to-pascal-two-strings", TO_PASCAL, new Object[] {"tESt tesT"}, Optional.of("Test Test")} ); + testCases.add( new Object[] {"to-pascal-separated-by-dot", TO_PASCAL, new Object[] {"tESt.tesT.tEsT"}, Optional.of("Test.Test.Test")} ); + return testCases.iterator(); } } From 443e98b46865165ba9059db0f724642d34521ddb Mon Sep 17 00:00:00 2001 From: petarpe Date: Wed, 26 Oct 2022 11:29:21 +0300 Subject: [PATCH 2/3] Update Strings.java --- .../java/com/bazaarvoice/jolt/modifier/function/Strings.java | 1 + 1 file changed, 1 insertion(+) diff --git a/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Strings.java b/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Strings.java index e15a34cd..f23e9ee1 100644 --- a/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Strings.java +++ b/jolt-core/src/main/java/com/bazaarvoice/jolt/modifier/function/Strings.java @@ -260,4 +260,5 @@ private static Optional padString( boolean leftPad, String source, List< return Optional.empty(); } + } From f661d516036e7b3cd92a13de09923ec887264b70 Mon Sep 17 00:00:00 2001 From: petarpe Date: Wed, 26 Oct 2022 14:28:38 +0300 Subject: [PATCH 3/3] Update stringsTests.json --- .../json/modifier/functions/stringsTests.json | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/jolt-core/src/test/resources/json/modifier/functions/stringsTests.json b/jolt-core/src/test/resources/json/modifier/functions/stringsTests.json index 47aa33e5..8dc83519 100644 --- a/jolt-core/src/test/resources/json/modifier/functions/stringsTests.json +++ b/jolt-core/src/test/resources/json/modifier/functions/stringsTests.json @@ -49,6 +49,13 @@ "trim" :{ "trimed" : "=trim(@(2,trimMe))" }, + "pascal" :{ + "leading": "=toPascal(@(2,string))", + "trailing": "=toPascal(^value)", + "custom1": "=toPascal(bazinga)", + "custom2": "=toPascal('yabadabadoo')", + "badArgs1" : "=toPascal(@2)" + }, "trimMe" : "=trim" }, "context": { @@ -90,6 +97,12 @@ "custom2": "fox", "advancedLookupRanges": "the" }, + "pascal": { + "leading": "The Quick Brown Fox", + "trailing": "Jumped Over The Lazy Dog", + "custom1": "Bazinga", + "custom2": "Yabadabadoo" + }, "trim" :{ "trimed" : "tuna" }