Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ public Predicate<ScriptContext> startWith(String text) {
};
}

public Predicate<ScriptContext> startWith(String text, boolean ignoreCase) {
return ctx -> {
String value = value().apply(ctx);
if (value == null) {
return false;
}
if (ignoreCase) {
return StringUtils.startsWithIgnoreCase(value, text);
}
return value.startsWith(text);
};
}

public Predicate<ScriptContext> matches(String text) {
return ctx -> {
String value = value().apply(ctx);
Expand Down Expand Up @@ -183,6 +196,16 @@ public AbstractText replace(String from, String to) {
});
}

public AbstractText replace(String from, String to, boolean ignoreCase) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
if (value == null) {
return null;
}
return replaceLiteral(value, from, to, ignoreCase);
});
}

public AbstractText replaceAll(String from, String to) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
Expand All @@ -193,6 +216,16 @@ public AbstractText replaceAll(String from, String to) {
});
}

public AbstractText replaceAll(String from, String to, boolean ignoreCase) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
if (value == null) {
return null;
}
return replaceAllRegex(value, from, to, ignoreCase);
});
}

public AbstractText path(final String path) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
Expand Down Expand Up @@ -224,6 +257,16 @@ public AbstractText replace(Function<ScriptContext, String> from, Function<Scrip
});
}

public AbstractText replace(Function<ScriptContext, String> from, Function<ScriptContext, String> to, boolean ignoreCase) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
if (value == null) {
return null;
}
return replaceLiteral(value, from.apply(scriptContext), to.apply(scriptContext), ignoreCase);
});
}

public AbstractText replaceAll(Function<ScriptContext, String> from, Function<ScriptContext, String> to) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
Expand All @@ -234,6 +277,16 @@ public AbstractText replaceAll(Function<ScriptContext, String> from, Function<Sc
});
}

public AbstractText replaceAll(Function<ScriptContext, String> from, Function<ScriptContext, String> to, boolean ignoreCase) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
if (value == null) {
return null;
}
return replaceAllRegex(value, from.apply(scriptContext), to.apply(scriptContext), ignoreCase);
});
}

public AbstractText replace(String from, Function<ScriptContext, String> to) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
Expand All @@ -244,6 +297,16 @@ public AbstractText replace(String from, Function<ScriptContext, String> to) {
});
}

public AbstractText replace(String from, Function<ScriptContext, String> to, boolean ignoreCase) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
if (value == null) {
return null;
}
return replaceLiteral(value, from, to.apply(scriptContext), ignoreCase);
});
}

public AbstractText replaceAll(String from, Function<ScriptContext, String> to) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
Expand All @@ -254,6 +317,16 @@ public AbstractText replaceAll(String from, Function<ScriptContext, String> to)
});
}

public AbstractText replaceAll(String from, Function<ScriptContext, String> to, boolean ignoreCase) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
if (value == null) {
return null;
}
return replaceAllRegex(value, from, to.apply(scriptContext), ignoreCase);
});
}

public AbstractText replace(Function<ScriptContext, String> from, String to) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
Expand All @@ -264,6 +337,16 @@ public AbstractText replace(Function<ScriptContext, String> from, String to) {
});
}

public AbstractText replace(Function<ScriptContext, String> from, String to, boolean ignoreCase) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
if (value == null) {
return null;
}
return replaceLiteral(value, from.apply(scriptContext), to, ignoreCase);
});
}

public AbstractText replaceAll(Function<ScriptContext, String> from, String to) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
Expand All @@ -273,4 +356,32 @@ public AbstractText replaceAll(Function<ScriptContext, String> from, String to)
return value.replaceAll(from.apply(scriptContext), to);
});
}
}

public AbstractText replaceAll(Function<ScriptContext, String> from, String to, boolean ignoreCase) {
return new Text(scriptContext -> {
String value = value().apply(scriptContext);
if (value == null) {
return null;
}
return replaceAllRegex(value, from.apply(scriptContext), to, ignoreCase);
});
}

private static String replaceLiteral(String value, String from, String to, boolean ignoreCase) {
if (!ignoreCase) {
return value.replace(from, to);
}
Pattern pattern = Pattern.compile(Pattern.quote(from), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher matcher = pattern.matcher(value);
return matcher.replaceAll(Matcher.quoteReplacement(to));
}

private static String replaceAllRegex(String value, String from, String to, boolean ignoreCase) {
if (!ignoreCase) {
return value.replaceAll(from, to);
}
Pattern pattern = Pattern.compile(from, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher matcher = pattern.matcher(value);
return matcher.replaceAll(to);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public void shouldEvaluateTextPredicatesAndTransforms() {
assertTrue(text.contains("world").test(context));
assertTrue(text.cic("HELLO").test(context));
assertTrue(text.startWith(" ").test(context));
assertTrue(text.startWith(" h", true).test(context));
assertFalse(text.startWith(" h", false).test(context));
assertTrue(text.trim().eq("Hello world").test(context));

AbstractText regexGroup = text.regexp("(Hello) (world)", 2);
Expand All @@ -39,6 +41,11 @@ public void shouldEvaluateTextPredicatesAndTransforms() {
assertEquals("b", text.split("-", 1).apply(context));
assertNull(text.split("-", 5).apply(context));

context.setContextValue("text", "Hello WORLD world");
assertEquals("Hello there there", text.replace("world", "there", true).apply(context));
assertEquals("Hello WORLD world", text.replace("world", "there", false).apply(context));
assertEquals("Hello done done", text.replaceAll("w.rld", "done", true).apply(context));

context.setContextValue("text", "json: {\"value\": 15}");
AbstractText path = new AbstractText.Text(ctx -> ctx.getContextValue("text").substring(6)).path("$.value");
assertEquals("15", path.apply(context));
Expand Down
Loading