Skip to content

Commit

Permalink
Update minLength limits logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperoman committed Sep 16, 2023
1 parent 0f0a83b commit e709be8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 82 deletions.
16 changes: 9 additions & 7 deletions src/main/scala/sqid/options/SqidsOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,25 @@ sealed abstract case class SqidsOptions(
blocklist
)

def withMinLength(minLength: Int): Either[InvalidSqidsOptions, SqidsOptions] = SqidsOptions.apply(
alphabet,
minLength,
blocklist
)
def withMinLength(minLength: Int): Either[InvalidSqidsOptions, SqidsOptions] =
SqidsOptions.apply(
alphabet,
minLength,
blocklist
)
}

object SqidsOptions {
val MinLengthLimit = 255
def apply(
alphabet: Alphabet,
minLength: Int,
blocklist: Blocklist
): Either[InvalidSqidsOptions, SqidsOptions] =
if (minLength < 0)
Left(InvalidSqidsOptions("minLength need to be > 0"))
else if (minLength > alphabet.length)
Left(InvalidSqidsOptions("minLength cant be larger than alphabet length"))
else if (minLength > MinLengthLimit)
Left(InvalidSqidsOptions(s"minLength cant be larger than $MinLengthLimit"))
else
Right(
new SqidsOptions(
Expand Down
74 changes: 0 additions & 74 deletions src/test/scala/sqids/AlphabetSuite_shuffle.scala

This file was deleted.

28 changes: 27 additions & 1 deletion src/test/scala/sqids/SqidsSuite_minLength.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package sqids

import munit.ScalaCheckSuite
import sqids.options.SqidsOptions
import sqids.options.Alphabet

final class SqidsSuite_minLength extends ScalaCheckSuite {

Expand All @@ -19,7 +20,32 @@ final class SqidsSuite_minLength extends ScalaCheckSuite {
assertEquals(sqids.encodeUnsafeString(numbers: _*), id)
assertEquals(sqids.decode(id), numbers)
}
test("incremental") {
val numbers: List[Long] = List(1, 2, 3);
val alphabetLength = Alphabet.default.length
val map: Map[Int, String] = Map(
6 -> "86Rf07",
7 -> "86Rf07x",
8 -> "86Rf07xd",
9 -> "86Rf07xd4",
10 -> "86Rf07xd4z",
11 -> "86Rf07xd4zB",
12 -> "86Rf07xd4zBm",
13 -> "86Rf07xd4zBmi",
alphabetLength + 0 -> "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTM",
alphabetLength + 1 -> "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMy",
alphabetLength + 2 -> "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMyf",
alphabetLength + 3 -> "86Rf07xd4zBmiJXQG6otHEbew02c3PWsUOLZxADhCpKj7aVFv9I8RquYrNlSTMyf1"
)

map.foreach { case (minLength, id) =>
val sqids = Sqids.withMinLength(minLength).toOption.get
assertEquals(sqids.encodeUnsafeString(numbers: _*), id)
assertEquals(sqids.encodeUnsafeString(numbers: _*).length, minLength)
assertEquals(sqids.decode(id), numbers)
}

}
test("incremental numbers") {
val ids = Map(
"SvIzsqYMyQwI3GWgJAe17URxX8V924Co0DaTZLtFjHriEn5bPhcSkfmvOslpBu" -> List(0, 0),
Expand Down Expand Up @@ -64,7 +90,7 @@ final class SqidsSuite_minLength extends ScalaCheckSuite {
test("out-of-range invalid min length") {
assert(SqidsOptions.default.withMinLength(minLength = -1).isLeft)
assert(
SqidsOptions.default.withMinLength(minLength = SqidsOptions.default.alphabet.value.length + 1).isLeft
SqidsOptions.default.withMinLength(minLength = SqidsOptions.MinLengthLimit + 1).isLeft
)
}
}
Expand Down

0 comments on commit e709be8

Please sign in to comment.