Skip to content

Commit

Permalink
Add initial constructor stub
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed May 23, 2024
1 parent 6f57bde commit ae5a956
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 120 deletions.
21 changes: 2 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
*.beam
*.iml
*.o
*.plt
*.swo
*.swp
*~
.erlang.cookie
.eunit
.idea
.rebar
.rebar3
_*
_build
docs
ebin
*.ez
/build
erl_crash.dump
gen
log
logs
rebar3.crashdump
36 changes: 16 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
# gleam_personnummer
# personnummer

A Gleam program


## Quick start
[![Package Version](https://img.shields.io/hexpm/v/personnummer)](https://hex.pm/packages/personnummer)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/personnummer/)

```sh
# Build the project
rebar3 compile

# Run the eunit tests
rebar3 eunit

# Run the Erlang REPL
rebar3 shell
gleam add personnummer
```
```gleam
import personnummer
pub fn main() {
// TODO: An example of the project in use
}
```

## Installation
Further documentation can be found at <https://hexdocs.pm/personnummer>.

If [available in Hex](https://www.rebar3.org/docs/dependencies#section-declaring-dependencies)
this package can be installed by adding `gleam_personnummer` to your `rebar.config` dependencies:
## Development

```erlang
{deps, [
gleam_personnummer
]}.
```sh
gleam run # Run the project
gleam test # Run the tests
gleam shell # Run an Erlang shell
```
21 changes: 20 additions & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
name = "gleam_personnummer"
name = "personnummer"
version = "1.0.0"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "username", repo = "project" }
# links = [{ title = "Website", href = "https://gleam.run" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
birl = ">= 1.6.1 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
14 changes: 14 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "birl", version = "1.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "976CFF85D34D50F7775896615A71745FBE0C325E50399787088F941B539A0497" },
{ name = "gleam_stdlib", version = "0.37.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5398BD6C2ABA17338F676F42F404B9B7BABE1C8DC7380031ACB05BBE1BCF3742" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
]

[requirements]
birl = { version = ">= 1.6.1 and < 2.0.0"}
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
13 changes: 0 additions & 13 deletions rebar.config

This file was deleted.

11 changes: 0 additions & 11 deletions rebar.lock

This file was deleted.

16 changes: 0 additions & 16 deletions src/gleam_personnummer.app.src

This file was deleted.

33 changes: 0 additions & 33 deletions src/gleam_personnummer.gleam

This file was deleted.

67 changes: 67 additions & 0 deletions src/personnummer.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import birl
import gleam/int
import gleam/option
import gleam/regex
import gleam/result

pub type PersonnummerError {
PersonnummerError(
/// The problem encountered that caused the compilation to fail
error: String,
)
}

pub type Personnummer {
Personnummer(
date: birl.Day,
serial: Int,
control: option.Option(Int),
separator: String,
coordination: Bool,
)
}

pub fn new(pnr_string) -> Result(Personnummer, PersonnummerError) {
let assert Ok(re) =
regex.from_string(
"^(\\d{2}){0,1}(\\d{2})(\\d{2})(\\d{2})([-+]{0,1})(\\d{3})(\\d{0,1})$",
)
case regex.scan(with: re, content: pnr_string) {
[] -> Error(PersonnummerError(error: "invalid format"))
[x, ..] -> new_from_matches(x)
}
}

fn new_from_matches(
matches: regex.Match,
) -> Result(Personnummer, PersonnummerError) {
let assert [century, year, month, day, divider, serial, control] =
matches.submatches

let century =
case century {
option.Some(_) -> maybe_str_to_int(century, 19)
option.None -> 19
}
* 100

let year = century + maybe_str_to_int(year, 0)
let month = maybe_str_to_int(month, 0)
let day = maybe_str_to_int(day, 0)
let control = case control {
option.Some(_) -> option.Some(maybe_str_to_int(control, 0))
option.None -> option.None
}

Ok(Personnummer(
date: birl.Day(year, month, day),
serial: maybe_str_to_int(serial, 0),
control: control,
separator: option.unwrap(divider, "-"),
coordination: False,
))
}

fn maybe_str_to_int(s: option.Option(String), default: Int) -> Int {
result.unwrap(int.base_parse(option.unwrap(s, ""), 10), default)
}
7 changes: 0 additions & 7 deletions test/gleam_personnummer_test.gleam

This file was deleted.

12 changes: 12 additions & 0 deletions test/personnummer_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import gleeunit

// import gleeunit/should
import personnummer

pub fn main() {
gleeunit.main()
}

pub fn new_test() {
personnummer.new("900101-0017")
}

0 comments on commit ae5a956

Please sign in to comment.