Skip to content

Commit

Permalink
Add options
Browse files Browse the repository at this point in the history
  • Loading branch information
frozzare committed Apr 12, 2023
1 parent 53a1df2 commit 9f4cd46
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
29 changes: 21 additions & 8 deletions src/main/scala/personnummer/Personnummer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import java.time.LocalDate
import java.time.Period
import java.time.format.DateTimeFormatter

class Options(
var allowCoordinationNumber: Boolean = true,
var allowInterimNumber: Boolean = false
) {}

class Personnummer {

/**
Expand Down Expand Up @@ -68,9 +73,9 @@ class Personnummer {
*
* @param pin String
*/
def this(pin: String) = {
def this(pin: String, options: Options = new Options()) = {
this()
parse(pin)
parse(pin, options)
}

/**
Expand Down Expand Up @@ -161,7 +166,7 @@ class Personnummer {
/**
* Parse Swedish personal identity number.
*/
private def parse(pin: String) = {
private def parse(pin: String, options: Options) = {
val reg: Regex =
"""^(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([+-]?)((?!000)\d{3}|[TRSUWXJKLMN]\d{2})(\d)$""".r

Expand Down Expand Up @@ -201,7 +206,15 @@ class Personnummer {

fullYear = century + year

if (this.valid() == false) {
if (!this.valid()) {
throw new Exception("Invalid swedish personal identity number")
}

if (this.isCoordinationNumber() && !options.allowCoordinationNumber) {
throw new Exception("Invalid swedish personal identity number")
}

if (this.isInterimNumber() && !options.allowInterimNumber) {
throw new Exception("Invalid swedish personal identity number")
}
}
Expand Down Expand Up @@ -282,18 +295,18 @@ object Personnummer {
*
* @return Personnummer
*/
def parse(pin: String): Personnummer = {
new Personnummer(pin)
def parse(pin: String, options: Options = new Options()): Personnummer = {
new Personnummer(pin, options)
}

/**
* Check if Swedish personal identity number is valid or not.
*
* @return Boolean
*/
def valid(pin: String): Boolean = {
def valid(pin: String, options: Options = new Options()): Boolean = {
try {
new Personnummer(pin)
new Personnummer(pin, options)
true
} catch {
case _: Throwable => false
Expand Down
11 changes: 7 additions & 4 deletions src/test/scala/personnummer/PersonnummerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,12 @@ class PersonnummerTests
if (item.valid) {
for (format <- availableListFormats) {
if (format != "integer") {
item.separated_format shouldEqual new Personnummer(item.get(format))
.format()
item.separated_format shouldEqual new Personnummer(
item.get(format),
new Options(allowInterimNumber = true)
).format()
item.long_format shouldEqual Personnummer
.parse(item.get(format))
.parse(item.get(format), new Options(allowInterimNumber = true))
.format(true)
}
}
Expand All @@ -197,7 +199,8 @@ class PersonnummerTests
for (format <- availableListFormats) {
if (format != "integer") {
an[Exception] should be thrownBy Personnummer.parse(
item.get(format)
item.get(format),
new Options(allowInterimNumber = true)
)
}
}
Expand Down

0 comments on commit 9f4cd46

Please sign in to comment.