diff --git a/src/main/java/ru/holyway/botplatform/scripting/entity/AbstractText.java b/src/main/java/ru/holyway/botplatform/scripting/entity/AbstractText.java index fad87bf..1c3e933 100644 --- a/src/main/java/ru/holyway/botplatform/scripting/entity/AbstractText.java +++ b/src/main/java/ru/holyway/botplatform/scripting/entity/AbstractText.java @@ -113,6 +113,19 @@ public Predicate startWith(String text) { }; } + public Predicate 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 matches(String text) { return ctx -> { String value = value().apply(ctx); @@ -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); @@ -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); @@ -224,6 +257,16 @@ public AbstractText replace(Function from, Function from, Function 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 from, Function to) { return new Text(scriptContext -> { String value = value().apply(scriptContext); @@ -234,6 +277,16 @@ public AbstractText replaceAll(Function from, Function from, Function 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 to) { return new Text(scriptContext -> { String value = value().apply(scriptContext); @@ -244,6 +297,16 @@ public AbstractText replace(String from, Function to) { }); } + public AbstractText replace(String from, Function 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 to) { return new Text(scriptContext -> { String value = value().apply(scriptContext); @@ -254,6 +317,16 @@ public AbstractText replaceAll(String from, Function to) }); } + public AbstractText replaceAll(String from, Function 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 from, String to) { return new Text(scriptContext -> { String value = value().apply(scriptContext); @@ -264,6 +337,16 @@ public AbstractText replace(Function from, String to) { }); } + public AbstractText replace(Function 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 from, String to) { return new Text(scriptContext -> { String value = value().apply(scriptContext); @@ -273,4 +356,32 @@ public AbstractText replaceAll(Function from, String to) return value.replaceAll(from.apply(scriptContext), to); }); } -} \ No newline at end of file + + public AbstractText replaceAll(Function 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); + } +} diff --git a/src/test/java/ru/holyway/botplatform/scripting/AbstractTextAndNumberOperationsTest.java b/src/test/java/ru/holyway/botplatform/scripting/AbstractTextAndNumberOperationsTest.java index bd03c8d..7e3dc7d 100644 --- a/src/test/java/ru/holyway/botplatform/scripting/AbstractTextAndNumberOperationsTest.java +++ b/src/test/java/ru/holyway/botplatform/scripting/AbstractTextAndNumberOperationsTest.java @@ -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); @@ -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));