Skip to content

Instantly share code, notes, and snippets.

@mgaffigan
Last active December 1, 2025 16:52
Show Gist options
  • Select an option

  • Save mgaffigan/b3fbe4899893dd0b9c0725fb747c4d45 to your computer and use it in GitHub Desktop.

Select an option

Save mgaffigan/b3fbe4899893dd0b9c0725fb747c4d45 to your computer and use it in GitHub Desktop.
X12 Mirth Code templates
function f(seg, field) {
if (seg.length() == 0) {
return null;
}
var fieldName = seg.localName() + '.' + ('00' + field).slice(-2);
return seg[fieldName][fieldName + '.1'].toString();
}
function s(seg, field, value) {
var fieldName = seg.localName() + '.' + ('00' + field).slice(-2);
seg[fieldName][fieldName + '.1'] = value;
}
function xfirst(l, pred) {
for each (a in l) {
if (pred(a)) {
return a;
}
}
return l.(@notexists123421935 == 'asgdkladsg');
}
function xwhere(seg, pred) {
var res = <List />;
for each (iseg in seg) {
if (pred(iseg)) {
res.* += iseg.copy();
}
}
return res.*;
}
function collectWhile(seg, pred) {
var res = <List />;
res.* += seg.copy();
var parent = seg.parent();
for (var i = seg.childIndex() + 1; i < parent.*.length(); i++) {
var n = parent.*[i];
if (!pred(n)) {
break;
}
res.* += n.copy();
}
return res;
}
function collectUntilInElem(root, startSeg, excludeSegments) {
var res = <Result />;
for (var x = startSeg.childIndex() + 1; x < root.*.length(); x++) {
var c = root.*[x];
if (excludeSegments.indexOf(c.localName()) >= 0) {
break;
}
res.* += c.copy();
}
return res;
}
function FixEnvelope(req) {
// default includes newlines from time to time
req.@segmentDelimiter = '~';
var now = DateUtil.getCurrentDate('yyyyMMddHHmm');
var date8 = now.slice(0, 8);
var time4 = now.slice(-4);
s(req.ISA, 9, date8.slice(2));
s(req.ISA, 10, time4);
s(req.GS, 4, date8);
s(req.GS, 5, time4);
s(req.BHT, 4, date8);
s(req.BHT, 5, time4);
s(req.SE, 1, req.*.length() - 4 /* ISA, GS, GE, IEA exlcuded */);
// Must be exactly 9 characters. Must be unique for batch
// S NNNNNN AA
// S = Numbering system
// N = Message number mod 1e6
// A = Random number (millisecond epoch mod 100)
var messageSerial = ('000000' + connectorMessage.getMessageId()).slice(-6);
var timeSerial = String(Date.now()).slice(-2);
var sn = '1' + messageSerial + timeSerial;
s(req.ISA, 13, sn);
s(req.IEA, 2, sn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment