tdecaluwe / node-edifact

Javascript stream parser for UN/EDIFACT documents.
https://www.npmjs.com/package/edifact
Apache License 2.0
50 stars 13 forks source link

Tracking segment groups #21

Open gnatowski opened 7 years ago

gnatowski commented 7 years ago

Could it be possible to track segment group on closing segment in parser? When we convert components to properties of our model, we need to know where we are in structure. For example in PRICAT/D02A we have DTM in groups SG1 = RFF-DTM, SG3 = RFF-DTM, SG5 = TAX-MOA-DTM, SG6 = CUX-DTM, and so on. In our old converter we had definitions for it, knowing which property in our model we have to set, e.g.: sg33.dtm.64=dateFrom, sg33.dtm.63=dateTo or for ORDRSP: root.dtm.64=default.DateFrom sg01.dtm.171=referenceDate sg26.dtm.64=item.dateFrom

Regards, Gabriel

tdecaluwe commented 7 years ago

Hello Gabriel, I've been thinking about this as well.

Currently there is one relatively easy way to do this. With the Tracker class you can validate a message against a certain segment table (for example to make sure you're receiving an INVOIC message). Since this class needs to track the current position to be able to do this you could also use this for your use case. However, currently there is no simple API for this.

The Tracker class only has one internal variable called stack which is actually a path to the current segment. Each element in the stack contains a reference to a segment table, a position and a repetition count. This means something like this should work:

var tracker = ... // get the tracker associated with your parser
var myDTMSegment = ... // to be retrieved in the segment table
var pointer = tracker.stack[tracker.stack.length - 1];
var segment = pointer.array[pointer.position];
if (segment === myDTMSegment) {
  ...
}