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 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-edifact-parser

v1.0.0

Published

JavaScript parser for UN/EDIFACT documents.

Downloads

33

Readme

view on npm npm module downloads per month

node-simple-edifact-parser

Currently supported functionality:

  • An ES6 streaming parser reading UN/EDIFACT messages.
  • Provide your own event listeners to get the parser to do something useful.
  • Construct structured javascript objects from UN/EDIFACT messages.
  • Support for the UNA header and custom separators.

This library further intends to support:

  • Out of the box support for envelopes.

This library is based on the work of Tom De Caluwé for the node-edifact library.

Usage

This example parses a document and translates it to a javascript array result containing segments. Each segment is an object containing a name and an elements array. An element is an array of components.

var Parser = require('edifact/parser.js');

var doc = ...;

var parser = new Parser(validator);

// Parsed segments will be collected in the result array.
var result = [], elements, components;

parser.on('opensegment', function (segment) {
  // Started a new segment.
  elements = [];
  result.push({ name: segment, elements: elements });
});

parser.on('element', function () {
  // Parsed a new element.
  components = [];
  elements.push(components);
});

parser.on('component', function (data) {
  // Got a new component.
  components.push(value);
});

parser.on('closesegment', function () {
  // Closed a segment.
});

parser.write(doc)
parser.end();

Installation

The module can be installed through:

npm install simple-edifact-parser

Its only dependency is the node events library. Keep in mind that this is an ES6 library. It currently can be used with node 6.4 or higher. A suite of tests is included which can be run with the jasmine-es6 package.

Overview

This module is built around a central Parser class which provides the core UN/EDIFACT parsing functionality. It exposes two methods, the write() method to write some data to the parser and the end() method to close an EDI interchange. Data read by the parser can be read by using hooks which will be called on specific parsing events.

Configuring the parser

The Parser class currently supports any of the UNOA, UNOB, UNOC, UNOY UNOW and UCS2 encoding levels. Furthermore, the parser will read any custom delimiters from the UNA header. If such a header is not present in the document, the delimiters can also be configured manually. The delimiters should be specified using their character codes. An example:

let parser = new Parser();

parser.encoding('UNOC');
parser.configure({
  segmentTerminator: 39,
  dataElementSeparator: 43,
  componentDataSeparator: 58,
  decimalMark: 46,
  releaseCharacter: 63,
});

Parsing events

The parser can be used by listening to the following events:

| Event | Description | | ----: | :---------- | | opensegment | A new segment is started. The name of the segment is passed as the argument. | | element | A new element is added. This only marks the start of the element, the data will follow through the component events. | | component | A component is added to the element. The data is passed on as an argument. | | closesegment | The current segment is terminated. |

For all textual fields the data will be passed on literally to the component event handler.

Converting EDIFACT to JSON

While the Parser class offers a flexible option to write your own EDIFACT applications, you might only be looking for an easy way read EDIFACT documents. The Reader class offers an easy to use interface:

let reader = new Reader({ autoDetectEncoding: true });
let result = reader.parse(document);

The parse() method returns an array of segments. Each segment is an object containing a segment name and the list of elements as an array of arrays with the actual component data.

Performance

If performance is critical the event callbacks can also be directly defined as methods on the Parser instance. Defining an event callback on('opensegment', callback) then becomes:

let parser = new Parser();
let callback = function (segment) { ... };

parser.onopensegment = callback;

Keep in mind that this avoids any opensegment events to be produced and as such, also its associated overhead.

Classes

| Class | Description | | ----: | :---------- | | Parser | The Parser class encapsulates an online parsing algorithm. By itself it doesn't do anything useful, however the parser can be extended through several event callbacks. | | Reader | The Reader offers a fast an easy to use interface to convert EDIFACT messages to a JSON structure. |

Reference

Parser

A parser capable of accepting data formatted as an UN/EDIFACT interchange.:

new Parser()

| Function | Description | | -------: | :---------- | | configure(config) | Configure any custom delimiters | | encoding(level) | Set one of the predefined encoding levels | | on(event,callback) | Add a listener for a specific event. The event can be any of opensegment, element, component and closesegment | | write(chunk) | Write a chunk of data to the parser | | end() | Terminate the EDI interchange |

Reader

The Reader offers a fast and easy to use interface to convert EDIFACT messages to a JSON structure. Furthermore, the Reader class can also autodetect the message encoding (this feature can be turned off by passing autoDetectEncoding: false as an option to the constructor).

new Reader(options)

| Function | Description | | -------: | :---------- | | parse(document) | Parse a document and return the result as an array of segments. |