Packages
hl7v2
0.5.5
3.10.1
3.9.0
3.8.0
3.7.0
3.6.0
3.5.0
3.4.0
3.3.6
3.3.5
3.3.4
3.3.3
3.3.2
3.3.1
3.3.0
3.2.0
3.1.1
3.1.0
3.0.2
3.0.1
3.0.0
2.11.0
2.10.0
2.9.1
2.9.0
2.8.2
2.8.1
2.8.0
2.7.1
2.7.0
2.6.0
2.5.0
2.4.0
2.3.0
2.2.0
2.1.3
2.1.2
2.1.1
2.1.0
1.4.6
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.0
1.2.0
1.1.0
1.0.0
0.6.0
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.1.0
Pure Elixir HL7 v2.x toolkit — schema-driven parsing, typed segments, message builder, MLLP transport
Current section
Files
Jump to
Current section
Files
lib/hl7v2/type/cx.ex
defmodule HL7v2.Type.CX do
@moduledoc """
Extended Composite ID with Check Digit (CX) -- HL7v2 composite data type.
Used for MRN, patient IDs, visit numbers, and other identifiers.
10 components:
1. ID Number (ST)
2. Check Digit (ST)
3. Check Digit Scheme (ID) -- Table 0061
4. Assigning Authority (HD) -- sub-components delimited by `&`
5. Identifier Type Code (ID) -- Table 0203: MR, PI, VN, AN, SS, etc.
6. Assigning Facility (HD) -- sub-components delimited by `&`
7. Effective Date (DT)
8. Expiration Date (DT)
9. Assigning Jurisdiction (CWE) -- sub-components delimited by `&`
10. Assigning Agency or Department (CWE) -- sub-components delimited by `&`
"""
@behaviour HL7v2.Type
alias HL7v2.Type
alias HL7v2.Type.{HD, DT, CWE}
defstruct [
:id,
:check_digit,
:check_digit_scheme,
:assigning_authority,
:identifier_type_code,
:assigning_facility,
:effective_date,
:expiration_date,
:assigning_jurisdiction,
:assigning_agency
]
@type t :: %__MODULE__{
id: binary() | nil,
check_digit: binary() | nil,
check_digit_scheme: binary() | nil,
assigning_authority: HD.t() | nil,
identifier_type_code: binary() | nil,
assigning_facility: HD.t() | nil,
effective_date: Date.t() | DT.t() | nil,
expiration_date: Date.t() | DT.t() | nil,
assigning_jurisdiction: CWE.t() | nil,
assigning_agency: CWE.t() | nil
}
@doc """
Parses a CX from a list of components.
Components containing sub-components (HD, CWE) are split by `&` and
parsed into their respective structs.
## Examples
iex> HL7v2.Type.CX.parse(["12345", "", "", "MRN", "MR"])
%HL7v2.Type.CX{id: "12345", assigning_authority: %HL7v2.Type.HD{namespace_id: "MRN"}, identifier_type_code: "MR"}
iex> HL7v2.Type.CX.parse(["12345", "", "", "MRN&1.2.3&ISO", "MR"])
%HL7v2.Type.CX{
id: "12345",
assigning_authority: %HL7v2.Type.HD{namespace_id: "MRN", universal_id: "1.2.3", universal_id_type: "ISO"},
identifier_type_code: "MR"
}
iex> HL7v2.Type.CX.parse([])
%HL7v2.Type.CX{}
"""
@spec parse(list()) :: t()
def parse(components) when is_list(components) do
%__MODULE__{
id: Type.get_component(components, 0),
check_digit: Type.get_component(components, 1),
check_digit_scheme: Type.get_component(components, 2),
assigning_authority: parse_sub_hd(Type.get_component(components, 3)),
identifier_type_code: Type.get_component(components, 4),
assigning_facility: parse_sub_hd(Type.get_component(components, 5)),
effective_date: components |> Type.get_component(6) |> DT.parse(),
expiration_date: components |> Type.get_component(7) |> DT.parse(),
assigning_jurisdiction: parse_sub_cwe(Type.get_component(components, 8)),
assigning_agency: parse_sub_cwe(Type.get_component(components, 9))
}
end
@doc """
Encodes a CX to a list of component strings.
## Examples
iex> HL7v2.Type.CX.encode(%HL7v2.Type.CX{id: "12345", assigning_authority: %HL7v2.Type.HD{namespace_id: "MRN"}, identifier_type_code: "MR"})
["12345", "", "", "MRN", "MR"]
iex> HL7v2.Type.CX.encode(nil)
[]
"""
@spec encode(t() | nil) :: list()
def encode(nil), do: []
def encode(%__MODULE__{} = cx) do
[
cx.id || "",
cx.check_digit || "",
cx.check_digit_scheme || "",
encode_sub_hd(cx.assigning_authority),
cx.identifier_type_code || "",
encode_sub_hd(cx.assigning_facility),
DT.encode(cx.effective_date),
DT.encode(cx.expiration_date),
encode_sub_cwe(cx.assigning_jurisdiction),
encode_sub_cwe(cx.assigning_agency)
]
|> Type.trim_trailing()
end
defp parse_sub_hd(nil), do: nil
defp parse_sub_hd(value) when is_binary(value) do
subs = String.split(value, Type.sub_component_separator())
hd_val = HD.parse(subs)
if all_nil?(hd_val), do: nil, else: hd_val
end
defp encode_sub_hd(nil), do: ""
defp encode_sub_hd(%HD{} = hd_val) do
hd_val |> HD.encode() |> Enum.join(Type.sub_component_separator())
end
defp parse_sub_cwe(nil), do: nil
defp parse_sub_cwe(value) when is_binary(value) do
subs = String.split(value, Type.sub_component_separator())
cwe_val = CWE.parse(subs)
if all_nil?(cwe_val), do: nil, else: cwe_val
end
defp encode_sub_cwe(nil), do: ""
defp encode_sub_cwe(%CWE{} = cwe) do
cwe |> CWE.encode() |> Enum.join(Type.sub_component_separator())
end
defp all_nil?(struct) do
struct
|> Map.from_struct()
|> Map.values()
|> Enum.all?(&is_nil/1)
end
end