-
Notifications
You must be signed in to change notification settings - Fork 0
/
nhi.pkl
104 lines (95 loc) · 3.3 KB
/
nhi.pkl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/// Checks strings against the New Zealand Ministry of Health NHI Validation Routine.
/// Supports the old and new NHI number formats specified in
/// [HISO 10046:2023](https://www.tewhatuora.govt.nz/publications/hiso-100462023-consumer-health-identity-standard/).
///
/// ## Usage
///
/// A simple [isValid] function can check whether a string is valid:
///
/// ```pkl
/// import "nhi.pkl"
///
/// typealias Nhi = String(nhi.isValid(this))
///
/// myOtherValidNhi: Nhi = "ZAC5361" // works
/// myOtherInvalidNhi: Nhi = "ZZZ0044" // fails
/// ```
///
/// Checks are case-insensitive.
///
/// ***Note:*** This does not check that the NHI number has been _assigned_ to
/// a person, it merely checks the NHI is consistent with the HISO 10046:2023
/// standard.
///
/// ### Excluding Testcases
///
/// NHI numbers that begin with `Z` are reserved for testing.
/// If you wish to exclude these values, you will need to manually check for a `Z`
/// prefix:
///
/// ```pkl
/// import "nhi.pkl"
///
/// typealias Nhi = String(nhi.isValid(this), !startsWith("Z"), !startsWith("z"))
///
/// myNhi1: Nhi = "ABC12AY" // works
/// myNhi2: Nhi = "ZAC5361" // fails
/// ```
///
/// ***Note:*** This check does not mean that the NHI number has been _assigned_ to
/// a person, it just means that the NHI value is not reserved for testing.
///
/// ## See Also
///
/// - <https://www.tewhatuora.govt.nz/publications/hiso-100462023-consumer-health-identity-standard/>
/// - <https://www.tewhatuora.govt.nz/our-health-system/digital-health/health-identity/national-health-index/information-for-health-it-vendors-and-developers>
@ModuleInfo { minPklVersion = "0.25.1" }
module nhi
local oldNhiFormatRegex = Regex(#"^[A-HJ-NP-Z]{3}\d{4}$"#)
local newNhiFormatRegex = Regex(#"^[A-HJ-NP-Z]{3}\d{2}[A-HJ-NP-Z]{2}$"#)
local i_code = "I".codePoints[0]
local o_code = "O".codePoints[0]
local at_code = "@".codePoints[0]
local function charCode(char: Char): Int =
let (code = char.codePoints[0])
char.toIntOrNull()
?? code - at_code - (if (i_code < code) 1 else 0) - (if (o_code < code) 1 else 0)
local function checksum(chars: List<Char>): Int =
chars
.dropLast(1)
.map((c) -> charCode(c))
.mapIndexed((i, c) -> c * (7 - i))
.reduce((e1, e2) -> e1 + e2)
/// Checks a string against the New Zealand Ministry of Health NHI
/// specification defined by the
/// [HISO 10046:2023](https://www.tewhatuora.govt.nz/publications/hiso-100462023-consumer-health-identity-standard/)
/// standard.
///
/// # See Also
/// <https://www.tewhatuora.govt.nz/publications/hiso-100462023-consumer-health-identity-standard/>
///
/// # Arguments
///
/// * `nhi`: A potential NHI string
///
/// returns: `true` if the given string satisfies the New Zealand NHI Validation Routine and `false` otherwise.
///
/// # Examples
///
/// ```pkl
/// import "nhi.pkl"
///
/// typealias Nhi = String(nhi.isValid(this))
///
/// myOtherValidNhi: Nhi = "ZAC5361" // works
/// myOtherInvalidNhi: Nhi = "ZZZ0044" // fails
/// ```
function isValid(nhi: String): Boolean =
let (nhi = nhi.toUpperCase())
if (nhi.matches(oldNhiFormatRegex))
let (checksum = checksum(nhi.chars) % 11)
checksum != 0 && (11 - checksum) % 10 == charCode(nhi.takeLast(1)[0])
else if (nhi.matches(newNhiFormatRegex))
23 - checksum(nhi.chars) % 23 == charCode(nhi.takeLast(1)[0])
else
false