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

Add convenience method to generate randomNanoId with just a size #6

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -45,7 +45,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadoc</id>
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/aventrix/jnanoid/jnanoid/NanoIdUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ public static String randomNanoId() {
return randomNanoId(DEFAULT_NUMBER_GENERATOR, DEFAULT_ALPHABET, DEFAULT_SIZE);
}

/**
* Static factory to retrieve a url-friendly, pseudo randomly generated, NanoId String.
*
* The NanoId String is generated using a cryptographically strong pseudo random number
* generator.
*
* @param size The number of symbols in the NanoId String.
*
* @return A randomly generated NanoId String.
*/
public static String randomNanoId(int size) {
return randomNanoId(DEFAULT_NUMBER_GENERATOR, DEFAULT_ALPHABET, size);
}

/**
* Static factory to retrieve a NanoId String.
*
Expand Down Expand Up @@ -125,10 +139,7 @@ public static String randomNanoId(final Random random, final char[] alphabet, fi
return idBuilder.toString();
}
}

}

}

}
}
13 changes: 13 additions & 0 deletions src/test/java/com/aventrix/jnanoid/NanoIdUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ public void NanoIdUtils_VariousSizes_Success() {

}

@Test
public void NanoIdUtils_DefinedSizes_Success() {

//Test ID generation with all sizes between 1 and 1,000.
for (int size = 1; size <= 1000; size++) {

final String id = NanoIdUtils.randomNanoId(size);

assertEquals(size, id.length());
}

}

@Test
public void NanoIdUtils_WellDistributed_Success() {

Expand Down