Skip to content

Commit

Permalink
rough edges
Browse files Browse the repository at this point in the history
  • Loading branch information
kutyel committed Jul 18, 2024
1 parent 5ee92d7 commit aa2c3d1
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 54 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@ jobs:
- name: run make, format, tests (see package.json)
run: npm test

- name: elm-review
run: npx elm-review --template jfmengels/elm-review-unused/example

- name: build
run: npm run build
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright © 2024 Scrive AB
Copyright © 2024 Scrive AB, 2019 Niklas Holmgren

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
8 changes: 6 additions & 2 deletions src/PersonalNumber/Danish.elm
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ where DDMMYY is the day, month and year of birth.

import Json.Decode as Decode
import Json.Encode as Encode
import Maybe.Extra as Maybe
import Parser exposing ((|.), Parser, chompIf, getChompedString)
import Util exposing (digit, getSerialPart, stringLength, validateDate, whitespace)

Expand All @@ -58,9 +57,14 @@ personalNumberParser =
|. stringLength 6
|. Parser.oneOf
[ chompIf (\c -> c == '-')
-- FIXME: fix only numbers!!
|. digit
|. digit
|. digit
|. digit
, digit
|. digit
|. digit
|. digit
]
)
|> Parser.map PersonalNumber
Expand Down
16 changes: 12 additions & 4 deletions src/PersonalNumber/Finnish.elm
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ import Basics.Extra exposing (flip)
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import List.Extra as List
import Maybe.Extra as Maybe
import Parser exposing ((|.), Parser, chompIf, getChompedString)
import Util exposing (getSerialPart, stringLength, validateDate, whitespace)
import Util exposing (char, digit, getSerialPart, validateDate, whitespace)


{-| An opaque type representing a valid personal number.
Expand Down Expand Up @@ -70,9 +69,18 @@ personalNumberParser =
(getChompedString <|
Parser.succeed ()
|. whitespace
|. stringLength 6
|. digit
|. digit
|. digit
|. digit
|. digit
|. digit
|. chompIf (\c -> c == '-' || c == '+' || c == 'A')
|. stringLength 4
|. digit
|. digit
|. digit
-- checksum
|. char
)
|> Parser.map PersonalNumber

Expand Down
77 changes: 35 additions & 42 deletions src/PersonalNumber/Swedish.elm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ temporary visitors.
import Json.Decode
import Json.Encode
import Parser exposing ((|.), Parser, getChompedString)
import Result.Extra as Result
import String
import Util exposing (stringLength, whitespace)

Expand Down Expand Up @@ -91,8 +90,8 @@ personalNumberParser =
|> Parser.map PNR


checkFormat : String -> Result ValidationError String
checkFormat str =
validateFormat : String -> Result ValidationError String
validateFormat str =
case Parser.run personalNumberParser str of
Ok pnr ->
Ok (toString pnr)
Expand All @@ -103,45 +102,39 @@ checkFormat str =

verifyChecksum : String -> Result ValidationError String
verifyChecksum str =
case Parser.run personalNumberParser str of
Ok pnr ->
let
checksum =
pnr
|> toString
|> String.split ""
|> List.reverse
|> List.take 10
|> List.reverse
|> List.map String.toInt
|> List.map (Maybe.withDefault -1)
|> List.indexedMap (\a b -> ( a, b ))
|> List.map
(\( i, a ) ->
if modBy 2 i == 0 then
a * 2

else
a
)
|> List.map
(\a ->
if a > 9 then
1 + (a - 10)

else
a
)
|> List.foldl (+) 0
in
if modBy 10 checksum == 0 then
Ok str

else
Err InvalidChecksum
let
checksum =
str
|> String.split ""
|> List.reverse
|> List.take 10
|> List.reverse
|> List.map String.toInt
|> List.map (Maybe.withDefault -1)
|> List.indexedMap (\a b -> ( a, b ))
|> List.map
(\( i, a ) ->
if modBy 2 i == 0 then
a * 2

else
a
)
|> List.map
(\a ->
if a > 9 then
1 + (a - 10)

else
a
)
|> List.foldl (+) 0
in
if modBy 10 checksum == 0 then
Ok str

Err _ ->
Err InvalidChecksum
else
Err InvalidChecksum


numberType : String -> Result ValidationError PersonalNumber
Expand Down Expand Up @@ -197,7 +190,7 @@ fromString str =
digits =
String.right 4 pnr
in
checkFormat (date ++ digits)
validateFormat (date ++ digits)
|> Result.andThen verifyChecksum
|> Result.andThen numberType

Expand Down
9 changes: 7 additions & 2 deletions src/Util.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Util exposing (digit, getSerialPart, stringLength, validateDate, whitespace)
module Util exposing (char, digit, getSerialPart, stringLength, validateDate, whitespace)

import List.Extra as List
import Parser exposing ((|.), Parser, Step(..), chompIf, chompWhile, getChompedString)
Expand All @@ -11,7 +11,12 @@ whitespace =

digit : Parser ()
digit =
chompWhile Char.isDigit
chompIf Char.isDigit


char : Parser ()
char =
chompIf Char.isAlphaNum


stringLength : Int -> Parser String
Expand Down
5 changes: 2 additions & 3 deletions tests/DanishTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ suite =
|> Expect.equal (Ok "160688-4523")
, test "should not accept a PNR with less than 10 digits" <|
\_ -> Expect.err (PersonalNumber.fromString "160688")
, skip <|
test "should not accept an invalid PNR with other characters than digits" <|
\_ -> Expect.err (PersonalNumber.fromString "160688-45MM")
, test "should not accept an invalid PNR with other characters than digits" <|
\_ -> Expect.err (PersonalNumber.fromString "160688-45MM")
, test "should not accept a PNR that is not a valid date" <|
\_ -> Expect.err (PersonalNumber.fromString "3206884523")
, test "should not accept an empty value" <|
Expand Down
2 changes: 2 additions & 0 deletions tests/FinnishTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ suite =
|> Expect.equal (Ok "111111-111C")
, test "should not accept a PNR without the last four digits" <|
\_ -> Expect.err (PersonalNumber.fromString "200866001386")
, test "should not accept a PNR with alpha characters not in checknum position" <|
\_ -> Expect.err (PersonalNumber.fromString "200A66001386")
, test "should not accept an invalid PNR with the correct format" <|
\_ -> Expect.err (PersonalNumber.fromString " - - -- ")
, test "should not accept a PNR that is not a valid date" <|
Expand Down
2 changes: 2 additions & 0 deletions tests/NorwegianTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ suite =
|> Expect.equal (Ok "19118438739")
, test "should not accept a PNR without the last four digits" <|
\_ -> Expect.err (fromString "200866001386")
, test "should not accept a PNR with alpha characters not in checknum position" <|
\_ -> Expect.err (PersonalNumber.fromString "200A66001386")
, test "should not accept an invalid PNR with the correct format" <|
\_ -> Expect.err (fromString " - - -- ")
, test "should not accept a PNR that is not a valid date" <|
Expand Down
2 changes: 2 additions & 0 deletions tests/SwedishTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ suite =
\_ -> Expect.err (fromString "19921208")
, test "should not accept an invalid PNR with the correct format" <|
\_ -> Expect.err (fromString "00000000-0000")
, test "should not accept a PNR with alpha characters not in checknum position" <|
\_ -> Expect.err (PersonalNumber.fromString "1992A208-1286")
, test "should not accept a PNR that is not a valid date" <|
\_ -> Expect.err (fromString "19921308-1285")
, test "should not accept a PNR with an invalid checksum" <|
Expand Down

0 comments on commit aa2c3d1

Please sign in to comment.