Skip to content
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

Closed
wants to merge 21 commits into from
Closed

Random Strings and Text Functions #567

wants to merge 21 commits into from

Conversation

adam-cowley
Copy link
Contributor

adam-cowley and others added 4 commits August 25, 2017 12:10
…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
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();
Copy link
Member

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

@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);
Copy link
Member

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"

public String snakeCase(@Name("text") String text) {
text = text.toLowerCase();

return text.replaceAll(" ", "-");
Copy link
Member

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"

public String toUpperCase(@Name("text") String text) {
text = text.toUpperCase();

return text.replaceAll(" ", "_");
Copy link
Member

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

Copy link
Contributor Author

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",
Copy link
Member

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

@jexp
Copy link
Member

jexp commented Aug 30, 2017

@adam-cowley ping :)

StringBuilder output = new StringBuilder( toIntExact(length) );

while ( output.length() < length ) {
output.append( valid.charAt( ThreadLocalRandom.current().nextInt(valid.length()) ) );
Copy link
Member

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

@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])", " ");
Copy link
Member

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)

public String camelCase(@Name("text") String text) {
text = text.replaceAll("([^A-Za-z0-9])", " ");

String[] parts = text.split("(\\s)");
Copy link
Member

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.

StringBuilder output = new StringBuilder();

for (String part : parts) {
part = part.toLowerCase().replaceAll("([^a-z0-9]+)", "");
Copy link
Member

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?

@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])");
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 a + here too. How do you restore the chars that are replaced? like A ?

@jexp
Copy link
Member

jexp commented Sep 7, 2017

@adam-cowley unfortunately your branch got messed up you have a lot of foreign commits in there now.
did you rebase?

@adam-cowley
Copy link
Contributor Author

Yeah, I ran:

git remote add upstream [email protected]:neo4j-contrib/neo4j-apoc-procedures.git
git rebase upstream/3.2  

@jexp
Copy link
Member

jexp commented Sep 9, 2017

Did you push that again? Probably easier to squash your commits locally and then cherry pick them over?

@jexp
Copy link
Member

jexp commented Sep 9, 2017

@adam-cowley do you want to fix it or shall I help ?

@jexp jexp mentioned this pull request Jun 24, 2018
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants