Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pascal Case function #1173

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jolt-core/src/main/java/com/bazaarvoice/jolt/Modifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,33 @@ protected Optional<String> applySingle( final Object arg ) {
}
}

public static final class toPascalCase extends Function.SingleFunction<String> {
@Override
protected Optional<String> 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<String> {
@Override
protected Optional<String> applySingle( final Object arg ) {
Expand Down Expand Up @@ -233,4 +260,5 @@ private static Optional<Object> padString( boolean leftPad, String source, List<

return Optional.empty();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public Iterator<Object[]> getTestCases() {
List<Object[]> 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() } );
Expand All @@ -50,6 +51,10 @@ public Iterator<Object[]> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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"
}
Expand Down