npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

hl7-tstd

v0.3.1

Published

A simple package to create, parse & transform HL7 message.

Readme

hl7-tstd

A simple package to create, parse & transform HL7 message.

import HL7, { Segment } from 'hl7-tstd';

const hl7 = new HL7(raw); // raw: raw HL7 message string

Segment is only a typescript type.

parseOptions

Additional configs for hl7 parsing and building.

| Parameter | Default Value | Expected | | :------------- | :-----------: | :-----------------------------------: | | fieldDelim | \| | string | | repeatingDelim | ~ | string | | componentDelim | ^ | string | | subCompDelim | \& | string | | eolDelim | \r?\n\|\r | \r?\n\|\r | \r\n | \n | \r | | buildEolChar | \r\n | string |

API References

Gets the value from a segment.

| Parameter | Type | Requirement | | :---------------- | :------: | :----------: | | field | string | Required | | repeatingIndex | number | Default: 0 | | subComponentIndex | number | Default: 0 |

Return: string | null

const zyxSegment = hl7.getSegment('ZYX');

zyxSegment?.get('ZYX.5.2', 1, 2);

Examples

ZYX|1|A|B|C|Repeat1~Component1^Component2~SubComp1&SubComp2^Component2~Repeat3
// Get entire segment
zyxSegment.get('ZYX'); // ZYX|1|A|B|C|Repeat1~Component1^Component2~SubComp1&SubComp2^Component2~Repeat3

// Get repeating fields
zyxSegment.get('ZYX.5'); // Repeat1
zyxSegment.get('ZYX.5', 2); // SubComp1&SubComp2^Component2
zyxSegment.get('ZYX.5', -1); // Repeat1~Component1^Component2~SubComp1&SubComp2^Component2~Repeat3

// Get component
zyxSegment.get('ZYX.5.1', 1); // Component1

// Get subcomponent
zyxSegment.get('ZYX.5.1', 2); // SubComp1
zyxSegment.get('ZYX.5.1', 2, 1); // SubComp2
zyxSegment.get('ZYX.5.1', 2, -1); // SubComp1&SubComp2

Sets the value of on a segment.

| Parameter | Type | Requirement | | :---------------- | :------: | :----------: | | field | string | Required | | value | string | Required | | repeatingIndex | number | Default: 0 | | subComponentIndex | number | Default: 0 |

const zyxSegment = hl7.getSegment('ZYX');

zyxSegment?.set('ZYX.5.2', 'ABCD', 1, 2); // ZYX|||||~^&&ABCD

Returns the first Segment matching the type param or else null.

| Parameter | Type | Requirement | | :-------- | :------: | :----------: | | type | string | Required |

Return: Segment | null

const pidSegment = hl7.getSegment('PID');

pidSegment?.set('PID.5', 'PAT_NAME');

Returns an array of Segments matching the type parameter, if provided; otherwise, returns an array of all Segments.

| Parameter | Type | Requirement | | :-------- | :------: | :---------: | | type | string | Optional |

Return: Segment[]

const obrSegments = hl7.getSegments('OBX');

for (const obrSegment of obrSegments) {
  obrSegment.get('OBR.4');
}

Returns an array of Segments matching the type, starting from the startSegment until encountering a segment listed in stopSegmentType.
Setting consecutive as true will return first set consecutive of matching Segments.

| Parameter | Type | Requirement | | :-------------- | :--------: | :------------: | | startSegment | Segment | Required | | type | string | Required | | stopSegmentType | string[] | Optional | | consecutive | boolean | Default: false |

Return: Segment[]

const obrSegment = hl7.getSegment('OBX');

const obxAfterObr = hl7.getSegmentsAfter(obrSegment!, 'OBX', ['OBR']);

Created a new segment of given type and appends it at the end of existing segments.

| Parameter | Type | Requirement | | :-------- | :------: | :----------: | | type | string | Required |

Return: Segment

const nteSegment = hl7.createSegment('NTE');

Created a new segment of given type and inserts it after targetSegment segment.

| Parameter | Type | Requirement | | :------------ | :-------: | :----------: | | type | string | Required | | targetSegment | Segment | Required |

Return: Segment

for (const obxSegment of hl7.getSegments('OBX')) {
  const nteSegment = hl7.createSegmentAfter('NTE', obxSegment);

  nteSegment.set('NTE.3', 'Notes');
}

Created a new segment of given type and inserts it before targetSegment segment.

| Parameter | Type | Requirement | | :------------ | :-------: | :----------: | | type | string | Required | | targetSegment | Segment | Required |

Return: Segment

for (const obxSegment of hl7.getSegments('OBX')) {
  const nteSegment = hl7.createSegmentBefore('NTE', obxSegment);

  nteSegment.set('NTE.3', 'Notes');
}

Deletes an existing segment if found.

| Parameter | Type | Requirement | | :-------- | :-------: | :----------: | | segment | Segment | Required |

const nteSegment = hl7.getSegment('NTE');

if (nteSegment) hl7.deleteSegment(nteSegment);

Deletes all existing segments if found.

| Parameter | Type | Requirement | | :-------- | :---------: | :----------: | | segments | Segment[] | Required |

const nteSegments = hl7.getSegments('NTE');

hl7.deleteSegments(nteSegments);

Moves segment after targetSegment.

| Parameter | Type | Requirement | | :------------ | :-----: | :----------: | | segment | Segment | Required | | targetSegment | Segment | Required |

Moves segment before targetSegment.

| Parameter | Type | Requirement | | :------------ | :-----: | :----------: | | segment | Segment | Required | | targetSegment | Segment | Required |

Reindexes segments based on resetRules.

| Parameter | Type | Requirement | | :--------- | :----: | :------------: | | resetRules | object | Required | | startIndex | number | Default: 1 | | field | string | Default: '1.1' |

resetRule

An object where each key is a segment type and its value is an array of segment types. The index of segment types specified in the keys will be set, and it will reset based on the segment types listed in its object value array.

field Segment field where index will be set. Defaults to '1.1'.

Example:

hl7.reindexSegments({ OBR: [], OBX: ['OBR'], NTE: ['OBR', 'OBX'] });

Here,

  • NTE segments will have index restarting from 1 after each OBR or OBX segment is encountered.
  • OBR segments will have index starting from one and incrementing since no reset segments were provided.

Note: reindexSegments sets the segment index value. This doesn't move the segments by itself.

⚠️ Deprecated: This method is triggered internally and doesn't need to be invoked manually.

Transforms the raw HL7 message suitable for manipulation and building.

hl7.transform(); // depricated

Attribution

This project includes code inspired from hl7-standard, licensed under the Apache License 2.0.