Transaction

Transaction

new Transaction(debug) → {Transaction}

Description:
  • A class representing an EDI transaction
Source:
Version:
  • 1.0.0
Since:
  • 1.0.0
License:
  • MIT
See:
Example
const transaction = new Transaction();
transaction.generateSegments(file);
const itemLoop = new Loop();
itemLoop.setPosition(0);
itemLoop.addSegmentIdentifiers(["W07", "N9", "W20"]);
transaction.addLoop(itemLoop);
transaction.runLoops();
const mapLogic = {
 header: {
 transmissionDate: new FieldMap({
segmentIdentifier: "GS",
identifierValue: null,
identifierPosition: null,
valuePosition: 3,
}),
warehouseReceiptNumber: new FieldMap({
segmentIdentifier: "W17",
identifierValue: null,
identifierPosition: null,
valuePosition: 2,
}),
warehouse: {
name: new FieldMap({
segmentIdentifier: "N1",
identifierValue: "WH",
identifierPosition: 0,
valuePosition: 1,
}),
code: new FieldMap({
segmentIdentifier: "N1",
identifierValue: "WH",
identifierPosition: 0,
valuePosition: 3,
}),
},
},
detail: {
items: new LoopMap({
position: 0,
values: {
itemCode: new FieldMap({
segmentIdentifier: "W07",
identifierValue: null,
identifierPosition: null,
valuePosition: 4,
}),
lotCode: new FieldMap({
segmentIdentifier: "W07",
identifierValue: null,
identifierPosition: null,
valuePosition: 7,
}),
productionDate: new FieldMap({
segmentIdentifier: "N9",
identifierValue: null,
identifierPosition: null,
valuePosition: 1,
}),
netWeight: new FieldMap({
segmentIdentifier: "W20",
identifierValue: null,
identifierPosition: null,
valuePosition: 3,
}),
},
}),
},
};
const mapped = transaction.mapSegments(mapLogic);
console.log(mapped);
// {
//   header: {
//     transmissionDate: "20210101",
//     warehouseReceiptNumber: "1234567890",
//     warehouse: {
//       name: "WAREHOUSE NAME",
//       code: "WAREHOUSE CODE",
//     },
//   },
//   detail: {
//     items: [
//       {
//         itemCode: "ITEM CODE",
//         lotCode: "LOT CODE",
//         productionDate: "20210101",
//         netWeight: "1000",
//       },
//     ],
//   },
// }
Parameters:
Name Type Description
debug boolean Whether or not to enable debug mode
Returns:
Type
Transaction

Methods

(static) addLoop(loop) → {void}

Description:
  • Add a loop to the transaction instance
Source:
Example
const loop = new Loop();
loop.addSegmentIdentifiers(["W07", "N9", "W20"]);
transaction.addLoop(loop);
console.log(transaction.getLoops());
// [
//   {
//     position: 0,
//     segmentIdentifiers: [ 'W07', 'N9', 'W20' ],
//     contents: []
//   }
// ]
Parameters:
Name Type Description
loop Loop The loop to add to the transaction instance
Returns:
Type
void

(static) generateSegments(content) → {void}

Description:
  • Generate segments for instance from a string
Source:
Parameters:
Name Type Description
content string
Returns:
Type
void

(static) getLoops() → {Array.<Loop>}

Description:
  • Get all loops in the transaction instance
Source:
Example
const loops = transaction.getLoops();
console.log(loops);
// [
//   {
//     position: 0,
//     segmentIdentifiers: [ 'W07', 'N9', 'W20' ],
//     contents: [
//       [
//         {
//           name: 'W07',
//           fields: [
//             { content: '100', position: 0 },
//             { content: 'EA', position: 1 },
//             { content: 'ITEM CODE', position: 4 },
//             { content: '100', position: 5 },
//             { content: 'LB', position: 6 },
//             { content: 'LOT CODE', position: 7 },
//           ]
//         },
//         {
//           name: 'N9',
//           fields: [
//             { content: 'PD', position: 0 },
//             { content: '20210101', position: 1 },
//           ]
//         },
//         {
//           name: 'W20',
//           fields: [
//             { content: '1000', position: 0 },
//             { content: 'LB', position: 1 },
//             { content: '1000', position: 3 },
//             { content: 'LB', position: 4 },
//           ]
//         }
//       ]
//     ]
//   }
// ]
Returns:
Type
Array.<Loop>

(static) getSegments() → {Array.<Segment>}

Description:
  • Get all segments in the transaction instance
Source:
Example
const segments = transaction.getSegments();
console.log(segments);
// [
//   {
//     name: 'ST',
//     fields: [
//       { content: '945', position: 0 },
//       { content: '0001', position: 1 },
//     ]
//   },
//   {
//     name: 'B4',
//     fields: [
//       { content: 'N', position: 0 },
//       { content: '1234567890', position: 1 },
//       { content: '20210101', position: 2 },
//     ]
//   },
// ]
Returns:
Type
Array.<Segment>

(static) getType() → {string}

Description:
  • Get the transaction type
Source:
Example
const type = transaction.getType();
console.log(type);
// 945
Throws:
  • No ST segment found
    Type
    Error
  • No ST02 segment found
    Type
    Error
Returns:
Type
string

(static) inferLoops() → {void}

Description:
  • Infer loops from the transaction instance
Source:
Returns:
Type
void

(static) listSegmentIdentifiers() → {Array.<string>}

Description:
  • Get all segment identifiers in the transaction instance
Source:
Example
const segmentIdentifiers = transaction.listSegmentIdentifiers();
console.log(segmentIdentifiers);
// [
//   'ST', 'B4', 'N1', 'N3', 'N4', 'G61', 'N1', 'N3', 'N4', 'G61',
// ]
Returns:
Type
Array.<string>

(static) mapSegments(mapLogic, mapSegments) → {Object}

Description:
  • Map segments to a JSON object
Source:
Example
const mapLogic = {
 header: {
  sender: new FieldMap({
  segmentIdentifier: "N1",
 identifierValue: "SF",
identifierPosition: 0,
valuePosition: 1,
}),
receiver: new FieldMap({
segmentIdentifier: "N1",
identifierValue: "ST",
identifierPosition: 0,
valuePosition: 1,
}),
transmissionDate: new FieldMap({
segmentIdentifier: "GS",
identifierValue: null,
identifierPosition: null,
valuePosition: 3,
}),
warehouseReceiptNumber: new FieldMap({
segmentIdentifier: "W17",
identifierValue: null,
identifierPosition: null,
valuePosition: 2,
}),
warehouse: {
name: new FieldMap({
segmentIdentifier: "N1",
identifierValue: "WH",
identifierPosition: 0,
valuePosition: 1,
}),
code: new FieldMap({
segmentIdentifier: "N1",
identifierValue: "WH",
identifierPosition: 0,
valuePosition: 3,
}),
},
},
detail: {
items: new LoopMap({
position: 0,
values: {
itemCode: new FieldMap({
segmentIdentifier: "W07",
identifierValue: null,
identifierPosition: null,
valuePosition: 4,
}),
lotCode: new FieldMap({
segmentIdentifier: "W07",
identifierValue: null,
identifierPosition: null,
valuePosition: 7,
}),
productionDate: new FieldMap({
segmentIdentifier: "N9",
identifierValue: null,
identifierPosition: null,
valuePosition: 1,
}),
netWeight: new FieldMap({
segmentIdentifier: "W20",
identifierValue: null,
identifierPosition: null,
valuePosition: 3,
}),
quantity: new FieldMap({
segmentIdentifier: "W07",
identifierValue: null,
identifierPosition: null,
valuePosition: 0,
}),
},
}),
},
};
const mapSegments = transaction.getSegments();
const mapped = transaction.mapSegments(mapLogic, mapSegments);
console.log(mapped);
// {
//   header: {
//     sender: "SENDER",
//     receiver: "RECEIVER",
//     transmissionDate: "20210101",
//     warehouseReceiptNumber: "1234567890",
//     warehouse: {
//       name: "WAREHOUSE NAME",
//       code: "WAREHOUSE CODE",
//     },
//   },
//   detail: {
//     items: [
//       {
//         itemCode: "ITEM CODE",
//         lotCode: "LOT CODE",
//         productionDate: "20210101",
//         netWeight: "1000",
//         quantity: "100",
//       },
//     ],
//   },
// }
Parameters:
Name Type Description
mapLogic Object The map logic to use to map segments and fields to a JSON object
mapSegments Array.<Segment> The segments to map to a JSON object (defaults to the segments in the transaction instance)
Returns:
Type
Object

(static) removeSegment(segment) → {Transaction}

Description:
  • Remove a segment from the transaction instance
Source:
Parameters:
Name Type Description
segment Segment
Returns:
Type
Transaction

(static) runLoops() → {void}

Description:
  • Run all loops in the transaction instance
Source:
Returns:
Type
void

(static) toJSON() → {Object}

Description:
  • Convert the transaction instance to a JSON object
Source:
Example
const json = transaction.toJSON();
console.log(json);
// {
//   segments: [
//     {
//       name: 'ST',
//       fields: [
//         { content: '945', position: 0 },
//         { content: '0001', position: 1 },
//       ]
//     },
//     {
//       name: 'B4',
//       fields: [
//         { content: 'N', position: 0 },
//         { content: '1234567890', position: 1 },
//         { content: '20210101', position: 2 },
//       ]
//     },
//   ],
//   loops: [
//     {
//       position: 0,
//       segmentIdentifiers: [ 'W07', 'N9', 'W20' ],
//       contents: []
//     }
//   ]
// }
Returns:
Type
Object