Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions src/main/java/apoc/text/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,12 @@ public String format(@Name("text") String text, @Name("params") List<Object> par
if (params == null) return text;
return String.format(Locale.ENGLISH,text, params.toArray());
}

@UserFunction
@Description("apoc.text.slug(text, delim) - slug the text with the given delimiter")
public String slug(@Name("text") String text, @Name(value = "delim", defaultValue = "-") String delim) {
if (text == null) return null;
if (delim == null) return null;
return text.replace(" ", delim).replace(delim+delim, delim);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably use replaceAll("\s+",delim) for all whitespace
or \W+ for non-word-characters

}
}
9 changes: 9 additions & 0 deletions src/test/java/apoc/text/StringsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,14 @@ public void testRegexGroupsForNPE() {
testCall(db, "RETURN apoc.text.regexGroups(null,'<link (\\\\w+)>(\\\\w+)</link>') AS result", row -> { });
testCall(db, "RETURN apoc.text.regexGroups('abc',null) AS result", row -> { });
}

@Test
public void testSlug() {
testCall(db, "RETURN apoc.text.slug('a b','-') AS value", row -> assertEquals("a-b", row.get("value")));
testCall(db, "RETURN apoc.text.slug('a- b','-') AS value", row -> assertEquals("a-b", row.get("value")));
testCall(db, "RETURN apoc.text.slug('a b c') AS value", row -> assertEquals("a-b-c", row.get("value")));
testCall(db, "RETURN apoc.text.slug('a b c', '.') AS value", row -> assertEquals("a.b.c", row.get("value")));

}

}