edadma / random_uuid   0.0.1

ISC License GitHub
Scala versions: 3.x
Scala.js versions: 1.x
Scala Native versions: 0.5

random_uuid

Maven Central Last Commit GitHub Scala Version ScalaJS Version Scala Native Version

A cross-platform Scala library for generating random UUIDs that works seamlessly across JVM, JavaScript (Node.js), and Scala Native platforms.

Overview

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.

Installation

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.

Basic Usage

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"

Platform-Specific Details

JVM Platform

// Uses java.util.UUID.randomUUID() under the hood
val uuid = randomUUID

JavaScript Platform (Node.js)

// Uses Node.js crypto.randomUUID() for optimal performance
val uuid = randomUUID

Scala Native Platform

// 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"

Examples

Basic UUID Generation

import io.github.edadma.random_uuid.*

@main def example(): Unit =
  // Generate multiple UUIDs
  val uuids = (1 to 5).map(_ => randomUUID)
  uuids.foreach(println)

Using in a Web Service

import io.github.edadma.random_uuid.*

case class User(id: String, name: String)

def createUser(name: String): User =
  User(randomUUID, name)

Batch Generation

import io.github.edadma.random_uuid.*

def generateBatch(count: Int): List[String] =
  List.fill(count)(randomUUID)

val batch = generateBatch(100)

Building the Project

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

Requirements

  • JVM: Java 8 or higher
  • JavaScript: Node.js 14+ (for crypto.randomUUID support)
  • Native: Scala Native 0.5.8+, libuuid system library

Tests

Run the test suite:

sbt test

The tests verify UUID generation and format validation across all supported platforms.

Contributing

This project welcomes contributions from the community. Contributions are accepted using GitHub pull requests.

For a good pull request, please provide:

  1. Clear description: Include the "what" and "why" for your changes
  2. Passing tests: Ensure all existing tests pass
  3. New tests: Include tests for new features or bug fixes
  4. Documentation: Update README.md for new features
  5. Code style: Run sbt scalafmtAll to format your code

Development Setup

# 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

Version History

  • 0.0.1 - Initial release with cross-platform UUID generation support

License

This project is licensed under the ISC License.

Links