Functional Scala implementation of Cuid2.
sa9vplf9bpyqj6hui8rsvi27
nfehj5xe9g7aruem87e1dcb6
p50d8uxvzs0irgrl9sidxqos
- secure
- collision-resistant
- optimized for horizontal scaling and performance
- friendlier to humans (easier to distinguish visually, double-click-selectable)
A full comparison to other IDs can be found in the readme of the original implementation.
libraryDependencies += "me.wojnowski" %% "scuid" % "<version>"
libraryDependencies += "me.wojnowski" %% "scuid-circe" % "<version>"
import cats.effect.IO
import me.wojnowski.scuid.Cuid2
import me.wojnowski.scuid.Cuid2Gen
...
for {
cuid2Generator: Cuid2Gen[IO] <- Cuid2Gen.default[IO]
id: Cuid2 <- cuid2Generator.generate // q055n575se3xpxu2xxmmd3ex
} yield ()
import cats.Id
import me.wojnowski.scuid.Cuid2
import me.wojnowski.scuid.Cuid2Gen
...
val generator: Cuid2Gen[Id] = Cuid2Gen.unsafeDefault
val id: Cuid2 = generator.generate // r0o5ncoizclu1b9iraz620cn
There are three options for length of the generated ID:
Cuid2
- 24 characters (default)Cuid2Long
- 32 charactersCuid2Custom[n]
- custom length (parametrised using literal types, e.g.Cuid2Custom[10]
orCuid2Custom[27]
)
This library uses SHA3, which is available from JDK 9 (b119) onwards. Alternative implementation can be provided via
Cuid2Gen.custom
.
val idDefault: Either[ValidationError, Cuid2] = Cuid2.validate("r0o5ncoizclu1b9iraz620cn")
val idLong: Either[ValidationError, Cuid2Long] = Cuid2Long.validate("tzwxyg5tav24zm8ycsrtpfi0njhegmes")
val idCustomLength: Either[ValidationError, Cuid2Custom[10]] = Cuid2Custom.validate[10]("axcf7v6n1w")
ValidationError
is an ADT, which offers prettyMessage
method for easy conversion to String
.
Use the following import for Cuid2 codecs:
import me.wojnowski.scuid.circe._
- Circe integration
- Tapir integration
- ZIO integration