Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle subcomponent 1 with no delimiter #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/hl7/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ segment.prototype.getComponent = function(fieldIndex, componentIndex, subCompone
if (components.length >= componentIndex) {
var component = components[componentIndex - 1]
if (subComponentIndex) {
if (component.value[0].length >= subComponentIndex) {
return component.value[0][subComponentIndex - 1].toString(delimiters)
var subComponent = Array.isArray(component.value[0]) ? component.value[0] : [component.value[0]];
if (subComponent.length >= subComponentIndex) {
return subComponent[subComponentIndex - 1].toString(delimiters)
}
return "";
}
Expand Down
11 changes: 11 additions & 0 deletions test/test-hl7.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ describe("Segment", function() {
assert.equal(simpleSegment.getComponent(1, 2, 2), "Sub2");
assert.equal(simpleSegment.getComponent(1, 2, 3), "");
});
it('should return the sub component at index 1', function() {
var rawMessage1 = "MSH|^~\\&|\rNME|^foo&";
var message1 = parser.parse(rawMessage1);
var segment1 = message1.getSegment("NME");
assert.equal(segment1.getComponent(1, 2, 1), "foo");

var rawMessage2 = "MSH|^~\\&|\rNME|^foo";
var message2 = parser.parse(rawMessage2);
var segment2 = message2.getSegment("NME");
assert.equal(segment2.getComponent(1, 2, 1), "foo");
});
});
describe('.setComponent(x, v)', function(){
it('should return the component at index', function() {
Expand Down
Loading