A cross-platform Scala library for generating random UUIDs that works seamlessly across JVM, JavaScript (Node.js), and Scala Native platforms.
This library provides a unified API for generating random UUIDs across different Scala platforms. It leverages platform-specific UUID generation mechanisms while maintaining a consistent interface:
- JVM: Uses Java's built-in
java.util.UUID.randomUUID()
- JavaScript: Uses Node.js
crypto.randomUUID()
- Scala Native: Uses the
libuuid
library via native bindings
Whether you're building server applications, Node.js services, or native executables, this library ensures you can generate cryptographically secure random UUIDs with the same simple API.
Add the following to your build.sbt
:
libraryDependencies += "io.github.edadma" %%% "random_uuid" % "0.0.1"
The %%%
dependency operator automatically selects the appropriate platform-specific artifact.
The library provides a simple function to generate random UUIDs:
import io.github.edadma.random_uuid.*
// Generate a random UUID
val uuid: String = randomUUID
println(uuid) // e.g., "550e8400-e29b-41d4-a716-446655440000"
// Uses java.util.UUID.randomUUID() under the hood
val uuid = randomUUID
// Uses Node.js crypto.randomUUID() for optimal performance
val uuid = randomUUID
// Uses libuuid library for native UUID generation
val uuid = randomUUID
You can also check which platform you're running on:
import io.github.edadma.random_uuid.platform
println(s"Running on: $platform") // Prints "jvm", "js", or "native"
import io.github.edadma.random_uuid.*
@main def example(): Unit =
// Generate multiple UUIDs
val uuids = (1 to 5).map(_ => randomUUID)
uuids.foreach(println)
import io.github.edadma.random_uuid.*
case class User(id: String, name: String)
def createUser(name: String): User =
User(randomUUID, name)
import io.github.edadma.random_uuid.*
def generateBatch(count: Int): List[String] =
List.fill(count)(randomUUID)
val batch = generateBatch(100)
This project uses sbt with cross-compilation support:
# Compile for all platforms
sbt compile
# Test all platforms
sbt test
# Compile for specific platform
sbt randomUuidJVM/compile
sbt randomUuidJS/compile
sbt randomUuidNative/compile
# Run for specific platform
sbt randomUuidJVM/run
sbt randomUuidJS/run
sbt randomUuidNative/run
- JVM: Java 8 or higher
- JavaScript: Node.js 14+ (for crypto.randomUUID support)
- Native: Scala Native 0.5.8+, libuuid system library
Run the test suite:
sbt test
The tests verify UUID generation and format validation across all supported platforms.
This project welcomes contributions from the community. Contributions are accepted using GitHub pull requests.
For a good pull request, please provide:
- Clear description: Include the "what" and "why" for your changes
- Passing tests: Ensure all existing tests pass
- New tests: Include tests for new features or bug fixes
- Documentation: Update README.md for new features
- Code style: Run
sbt scalafmtAll
to format your code
# Clone the repository
git clone https://github.com/edadma/random_uuid.git
cd random_uuid
# Compile and test
sbt compile test
# Format code
sbt scalafmtAll
- 0.0.1 - Initial release with cross-platform UUID generation support
This project is licensed under the ISC License.