Skip to content

Commit

Permalink
feat: improve java usage
Browse files Browse the repository at this point in the history
feat: lower jvmTarget to 17
docs: update README
chore: bump version to 1.0.1
  • Loading branch information
itsmefox committed Jan 21, 2024
1 parent ff086e1 commit 9f147ee
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Nano ID for Kotlin

<a href="https://github.com/viascom/nanoid-kotlin/releases"><img src="https://img.shields.io/maven-central/v/io.viascom.nanoid/nanoid" alt="Maven central"></a>
<img src="https://img.shields.io/badge/Kotlin-1.9.20-%238052ff?logo=kotlin" alt="Kotlin Version">
<img src="https://img.shields.io/badge/Kotlin-1.9.22-%238052ff?logo=kotlin" alt="Kotlin Version">
<img src="https://img.shields.io/badge/Java-17-%23437291?logo=openjdk" alt="Java Version">
<a href="http://www.apache.org/licenses/"><img src="https://img.shields.io/badge/license-Apache_2.0-blue.svg" alt="license Apache 2.0"></a>

_Inspired by the following parent project: [ai/nanoid](https://github.com/ai/nanoid)_
Expand All @@ -25,7 +26,7 @@ Add nanoid-kotlin as a dependency to your project.
Gradle:
```gradle
dependencies {
implementation 'io.viascom.nanoid:nanoid:1.0.0'
implementation 'io.viascom.nanoid:nanoid:1.0.1'
}
```

Expand All @@ -34,7 +35,7 @@ Maven:
<dependency>
<groupId>io.viascom.nanoid</groupId>
<artifactId>nanoid</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</dependency>
```

Expand Down
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plugins {
ext {
major = 1
minor = 0
patch = 0
patch = 1

isCiServer = System.getenv("GITHUB_ACTIONS") != null || System.getProperty("GITHUB_ACTIONS") != null
}
Expand All @@ -33,6 +33,9 @@ jar {
java {
withSourcesJar()
withJavadocJar()
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}

repositories {
Expand All @@ -45,7 +48,7 @@ dependencies {
}

tasks.withType(KotlinCompile).configureEach {
kotlinOptions.jvmTarget = JavaVersion.VERSION_19
kotlinOptions.jvmTarget = JavaVersion.VERSION_17
kotlinOptions.freeCompilerArgs = ["-Xjvm-default=all"]
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/io/viascom/nanoid/NanoId.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ object NanoId {
* @throws IllegalArgumentException if the alphabet is empty or larger than 255 characters, or if the size is not greater than zero.
*/
@JvmOverloads
@JvmStatic
fun generate(
@NotNull
size: Int = 21,
Expand Down Expand Up @@ -84,6 +85,7 @@ object NanoId {
* @return The generated optimized string.
*/
@JvmOverloads
@JvmStatic
fun generateOptimized(@NotNull size: Int, @NotNull alphabet: String, @NotNull mask: Int, @NotNull step: Int, @NotNull random: Random = SecureRandom()): String {
val idBuilder = StringBuilder(size)
val bytes = ByteArray(step)
Expand All @@ -107,6 +109,7 @@ object NanoId {
* @param alphabet The set of characters to use for generating the string.
* @return The additional bytes factor, rounded to two decimal places.
*/
@JvmStatic
fun calculateAdditionalBytesFactor(@NotNull alphabet: String): Double {
val mask = calculateMask(alphabet)
return (1 + abs((mask - alphabet.length.toDouble()) / alphabet.length)).round(2)
Expand All @@ -118,6 +121,7 @@ object NanoId {
* @param alphabet The set of characters to use for generating the string.
* @return The calculated mask value.
*/
@JvmStatic
fun calculateMask(@NotNull alphabet: String) = (2 shl (Integer.SIZE - 1 - Integer.numberOfLeadingZeros(alphabet.length - 1))) - 1

/**
Expand All @@ -128,6 +132,7 @@ object NanoId {
* @param additionalBytesFactor The additional bytes factor. Default value is calculated using `calculateAdditionalBytesFactor()`.
* @return The number of random bytes to generate in each iteration.
*/
@JvmStatic
@JvmOverloads
fun calculateStep(@NotNull size: Int, @NotNull alphabet: String, @NotNull additionalBytesFactor: Double = calculateAdditionalBytesFactor(alphabet)) =
ceil(additionalBytesFactor * calculateMask(alphabet) * size / alphabet.length).toInt()
Expand Down

0 comments on commit 9f147ee

Please sign in to comment.