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

node-x12-edi

v1.0.4

Published

A Node.js library for parsing and generating X12 EDI files

Downloads

7

Readme

The intent of this module is to provide low-level access to X12 EDI documents in plain JS.

In this short guide I am going to walk through how to take an EDI file and convert it to a usable JSON Object.

Input File Content

ISA*00*          *00*          *12*1114315990     *16*007076664TEST  *230929*1419*X*00401*000000015*0*P*>
GS*RE*1114315990*022865398*20230929*1419*15*X*004010
ST*944*0001
W17*F*20230929*4280*0450366097
N1*WH*Distribution Center*91*PC1234
W08*M*0000***1234*123456789
W07*120*CA**VN*100000154*LT**22413962
N9*PC*20230904
W20****1800.00*N*L
W07*120*CA**VN*100000154*LT**22413963
N9*PC*20230904
W20****1800.00*N*L
W07*120*CA**VN*100000154*LT**22413964
N9*PC*20230904
W20****1800.00*N*L
W07*120*CA**VN*100000154*LT**22413965
N9*PC*20230904
W20****1800.00*N*L
W07*120*CA**VN*100000154*LT**22413966
N9*PC*20230904
W20****1800.00*N*L
W14*600
SE*21*0001
GE*1*15
IEA*1*000000015

Output: Fully usable JSON object

const mappedEDI = {
    header: {
        transmissionDate: "20230929",
        warehouseReceiptNumber: "4280",
        warehouse: {
            name: "Distribution Center",
            code: "PC1234"
        }
    },
    detail: {
        items: [
            {
                itemCode: "100000154",
                lotCode: "22413963",
                quantity: "120",
                netWeight: "1800.00"
            },
             {
                itemCode: "100000155",
                lotCode: "22413963",
                quantity: "120",
                netWeight: "1800.00"
            },
             {
                itemCode: "100000156",
                lotCode: "22413963",
                quantity: "120",
                netWeight: "1800.00"
            }
        ]
    }
}

The 4 main classes used to interface with an X12 EDI file are:

  • Transaction
  • FieldMap
  • LoopMap
  • Loop

Step 1: Load the EDI file contents:

const file = await readFile("test.edi", "utf8");

Step 2: create a new EDI Transaction:

const transaction = new Transaction();

Step 3: Initiate the segments of the Transaction by using the Transaction.generateSegments() method:

transaction.generateSegments(file);

Step 4: Create a loop for the Details section:

const itemLoop = new Loop();

itemLoop.setPosition(0);

itemLoop.addSegmentIdentifiers(["W07", "N9", "W20"]) // These are the segments in the EDI file that are grouped together in the details section to classify as 1 object / item.

Step 5: Add the loop to the transaction

transaction.addLoop(itemLoop);
transaction.runLoops(); // Executes the loops assigned to the Transaction instance

Step 6: Create the JSON Object you would like to map the EDI fields to:

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,
          }),
          quantity: new FieldMap({
            segmentIdentifier: "W07",
            identifierValue: null,
            identifierPosition: null,
            valuePosition: 0,
          }),
        },
      }),
    },
  };

Step 7: Map the segments

const mapped = transaction.mapSegments(mapLogic, transaction.getSegments());

Full Example

const file = await fs.readFile("test.edi", "utf8");

  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,
          }),
          quantity: new FieldMap({
            segmentIdentifier: "W07",
            identifierValue: null,
            identifierPosition: null,
            valuePosition: 0,
          }),
        },
      }),
    },
  };

  const mapped = transaction.mapSegments(mapLogic, transaction.getSegments());