added more whitespace behaviors (trims)

This commit is contained in:
Tim Chamberlain
2025-05-13 10:15:41 -05:00
parent 6d0f5d4fb3
commit 2491523a6b
2 changed files with 63 additions and 3 deletions

View File

@ -41,7 +41,10 @@ import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
public enum WhiteSpaceBehavior implements FieldBehavior<WhiteSpaceBehavior>, FieldBehaviorForFrontend, FieldFilterBehavior<WhiteSpaceBehavior>
{
NONE(null),
REMOVE_ALL_WHITESPACE((String s) -> s.chars().filter(c -> !Character.isWhitespace(c)).collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString());
REMOVE_ALL_WHITESPACE((String s) -> s.chars().filter(c -> !Character.isWhitespace(c)).collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString()),
TRIM((String s) -> s.trim()),
TRIM_LEFT((String s) -> s.stripLeading()),
TRIM_RIGHT((String s) -> s.stripTrailing());
private final Function<String, String> function;
@ -82,7 +85,7 @@ public enum WhiteSpaceBehavior implements FieldBehavior<WhiteSpaceBehavior>, Fie
switch(this)
{
case REMOVE_ALL_WHITESPACE -> applyFunction(recordList, table, field);
case REMOVE_ALL_WHITESPACE, TRIM, TRIM_LEFT, TRIM_RIGHT -> applyFunction(recordList, table, field);
default -> throw new IllegalStateException("Unexpected enum value: " + this);
}
}

View File

@ -94,6 +94,63 @@ class WhiteSpaceBehaviorTest extends BaseTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testTrimWhiteSpace()
{
assertNull(applyToRecord(WhiteSpaceBehavior.TRIM, new QRecord(), ValueBehaviorApplier.Action.INSERT).getValue(FIELD));
assertNull(applyToRecord(WhiteSpaceBehavior.TRIM, new QRecord().withValue(FIELD, null), ValueBehaviorApplier.Action.INSERT).getValue(FIELD));
assertEquals("doo bee doo\n bee doo", applyToRecord(WhiteSpaceBehavior.TRIM, new QRecord().withValue(FIELD, " doo bee doo\n bee doo\r \n\n"), ValueBehaviorApplier.Action.INSERT).getValue(FIELD));
assertEquals(ListBuilder.of("this is\rthe way", null, "that was the way"), applyToRecords(WhiteSpaceBehavior.TRIM, List.of(
new QRecord().withValue(FIELD, " this is\rthe way \t"),
new QRecord(),
new QRecord().withValue(FIELD, "that was the way\n")),
ValueBehaviorApplier.Action.INSERT).stream().map(r -> r.getValueString(FIELD)).toList());
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testTrimLeftWhiteSpace()
{
assertNull(applyToRecord(WhiteSpaceBehavior.TRIM_LEFT, new QRecord(), ValueBehaviorApplier.Action.INSERT).getValue(FIELD));
assertNull(applyToRecord(WhiteSpaceBehavior.TRIM_LEFT, new QRecord().withValue(FIELD, null), ValueBehaviorApplier.Action.INSERT).getValue(FIELD));
assertEquals("doo bee doo\n bee doo\r \n\n", applyToRecord(WhiteSpaceBehavior.TRIM_LEFT, new QRecord().withValue(FIELD, " doo bee doo\n bee doo\r \n\n"), ValueBehaviorApplier.Action.INSERT).getValue(FIELD));
assertEquals(ListBuilder.of("this is\rthe way \t", null, "that was the way\n"), applyToRecords(WhiteSpaceBehavior.TRIM_LEFT, List.of(
new QRecord().withValue(FIELD, " this is\rthe way \t"),
new QRecord(),
new QRecord().withValue(FIELD, " \n that was the way\n")),
ValueBehaviorApplier.Action.INSERT).stream().map(r -> r.getValueString(FIELD)).toList());
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testTrimRightWhiteSpace()
{
assertNull(applyToRecord(WhiteSpaceBehavior.TRIM_RIGHT, new QRecord(), ValueBehaviorApplier.Action.INSERT).getValue(FIELD));
assertNull(applyToRecord(WhiteSpaceBehavior.TRIM_RIGHT, new QRecord().withValue(FIELD, null), ValueBehaviorApplier.Action.INSERT).getValue(FIELD));
assertEquals(" doo bee doo\n bee doo", applyToRecord(WhiteSpaceBehavior.TRIM_RIGHT, new QRecord().withValue(FIELD, " doo bee doo\n bee doo\r \n\n"), ValueBehaviorApplier.Action.INSERT).getValue(FIELD));
assertEquals(ListBuilder.of(" this is\rthe way", null, " \n that was the way"), applyToRecords(WhiteSpaceBehavior.TRIM_RIGHT, List.of(
new QRecord().withValue(FIELD, " this is\rthe way \t"),
new QRecord(),
new QRecord().withValue(FIELD, " \n that was the way\n")),
ValueBehaviorApplier.Action.INSERT).stream().map(r -> r.getValueString(FIELD)).toList());
}
/*******************************************************************************
**
*******************************************************************************/
@ -167,7 +224,7 @@ class WhiteSpaceBehaviorTest extends BaseTest
new UpdateAction().execute(new UpdateInput(TestUtils.TABLE_NAME_SHAPE).withRecord(new QRecord().withValue("id", id).withValue("name", "Octagon")));
////////////////////////////////////////////////////////////////////////////////////
// turn off the to-lower-case behavior, so we'll see what was actually udpated to //
// turn off the to-lower-case behavior, so we'll see what was actually updated to //
////////////////////////////////////////////////////////////////////////////////////
field.setBehaviors(Collections.emptySet());
assertEquals("octagon", GetAction.execute(TestUtils.TABLE_NAME_SHAPE, id).getValueString("name"));