-
Notifications
You must be signed in to change notification settings - Fork 495
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
Random Strings and Text Functions #567
Conversation
adam-cowley
commented
Aug 25, 2017
- Added apoc.text.random(length, [valid_chars]) - Generate random strings and numbers #563
- Added apoc.text.capitalize, apoc.text.capitalizeAll, apoc.text.decapitalize, apoc.text.swapCase, apoc.text.camelCase, apoc.text.snakeCase, apoc.text.toUpperCase functions - apoc text functions for capitalization #530
…talize, apoc.text.swapCase, apoc.text.camelCase, apoc.text.snakeCase, apoc.text.toUpperCase
* you can now provide (multiple) aggregations per property, currently (min,max,avg,collect,sum and count are supported) * nodes and relationships have separate property-aggregations * implementation is parallelized now making it much faster
src/main/java/apoc/text/Strings.java
Outdated
public String random(final @Name("length") long length, @Name(value = "valid", defaultValue = "A-Za-z0-9") String valid) { | ||
valid = valid.replaceAll("A-Z", upper).replaceAll("a-z", lower).replaceAll("0-9", numeric); | ||
|
||
SecureRandom rnd = new SecureRandom(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you use thread-local-random instead it's not so expensive and not locking
src/main/java/apoc/text/Strings.java
Outdated
@UserFunction | ||
@Description("apoc.text.camelCase(text) YIELD value - Convert a string to camelCase") | ||
public String camelCase(@Name("text") String text) { | ||
String converted = WordUtils.capitalizeFully(text); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this works.
it would also be good to turn e.g. "FOO_BAR" or "foo-bar" or "FooBar" into "fooBar"
src/main/java/apoc/text/Strings.java
Outdated
public String snakeCase(@Name("text") String text) { | ||
text = text.toLowerCase(); | ||
|
||
return text.replaceAll(" ", "-"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would also be good to turn e.g. "FOO_BAR" , "FooBar" , "fooBar" into "foo-bar"
src/main/java/apoc/text/Strings.java
Outdated
public String toUpperCase(@Name("text") String text) { | ||
text = text.toUpperCase(); | ||
|
||
return text.replaceAll(" ", "_"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would also be good to turn e.g. "FooBar", "fooBar", "foo-bar" into "FOO_BAR" or
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is toUpperCase a little ambiguous a name for this function? Could it be confused with toUpper?
|
||
testCall( | ||
db, | ||
"RETURN apoc.text.capitalizeAll({text}) as value", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks like the wrong call
@adam-cowley ping :) |
src/main/java/apoc/text/Strings.java
Outdated
StringBuilder output = new StringBuilder( toIntExact(length) ); | ||
|
||
while ( output.length() < length ) { | ||
output.append( valid.charAt( ThreadLocalRandom.current().nextInt(valid.length()) ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pull the rng before the loop
src/main/java/apoc/text/Strings.java
Outdated
@UserFunction | ||
@Description("apoc.text.camelCase(text) YIELD value - Convert a string to camelCase") | ||
public String camelCase(@Name("text") String text) { | ||
text = text.replaceAll("([^A-Za-z0-9])", " "); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could use \W+ (non-word-characters)
src/main/java/apoc/text/Strings.java
Outdated
public String camelCase(@Name("text") String text) { | ||
text = text.replaceAll("([^A-Za-z0-9])", " "); | ||
|
||
String[] parts = text.split("(\\s)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\\s+
pulls together subsequen whitespace.
src/main/java/apoc/text/Strings.java
Outdated
StringBuilder output = new StringBuilder(); | ||
|
||
for (String part : parts) { | ||
part = part.toLowerCase().replaceAll("([^a-z0-9]+)", ""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is still then still needed with the changes above?
src/main/java/apoc/text/Strings.java
Outdated
@UserFunction | ||
@Description("apoc.text.toUpperCase(text) YIELD value - Convert a string to UPPER_CASE") | ||
public String toUpperCase(@Name("text") String text) { | ||
String[] parts = text.split("(?=[^a-z0-9])"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably use a + here too. How do you restore the chars that are replaced? like A
?
…talize, apoc.text.swapCase, apoc.text.camelCase, apoc.text.snakeCase, apoc.text.toUpperCase
@adam-cowley unfortunately your branch got messed up you have a lot of foreign commits in there now. |
Yeah, I ran:
|
Did you push that again? Probably easier to squash your commits locally and then cherry pick them over? |
@adam-cowley do you want to fix it or shall I help ? |