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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mt940-ts

v1.0.3

Published

Parser for MTXXX financial messages defined by the SWIFT standard

Readme

mt940-ts

Build Status

A Typescript library for parsing MT940 messages. It takes as an input text representing a set of MT940 statements and produces an array of statements as output.

Installation

npm install mt940-ts

Usage

The module exports an mt940Parser method that takes a string as an argument and returns an array of statements representing each of the MT940 messages present in the input.

Example

import { readFileSync } from "fs";
import { mt940Parser } from "mt940-ts";

const input = readFileSync("./mt94x.txt").toString();
const parsed = mt940Parser(input);

console.log("Number of statements: ", parsed.length);
for (const statement of parsed) {
  console.log(
    `==== Showing records for account: ${statement.accountIdentification} ====`
  );
  statement.records.forEach((record) => {
    console.log(record);
  });
}

Creating a custom parser

mt940-ts aims to be a simple framework that facilitates the creation of parsers for any MTXXX Swift message. In order to write a custom parser for a given MTXXX message, the following steps must be followed:

  • define the tags that compose the MTXXX message
  • create an object that represents the structure of the MTXXX message, where each property belongs to a field in the MTXXX message and each value is the tag that corresponds to such property
  • create the parser by calling the parserFactory function from src/parser.ts passing the object created on the previous step as the parameter

As an example, lets create a parser for the MT973 message. I chose this one because it's rather small, having only 3 fields. Let's start by extending the src/tags.ts file with the definition of the tag 12 (tags 20 and 25 are already defined since they are used by the MT940 parser):

/*
 * Message type
 *
 * This field identifies the message type which is being requested.
 */
const messageTypeRegex = [RegExp(`^(971|972|998)$`)];

export const tag12 = configTag(
  "12",
  messageTypeRegex,
  (resultList: ParseLineResult[]) => {
    const [{ match, nonMatchReason }] = resultList;

    if (nonMatchReason) throw new Error(nonMatchReason);

    if (!match) throw new Error("Failed to extract message type");

    return { messageType: match[1] };
  }
);

Next, let's create the file src/mt973.ts which will contain the object that defines the structure of the message and it will create and export our parser:

import { tag12, tag20, tag25 } from "./tags";
import { parserFactory } from "./parser";
import { REPEAT } from "./combinators";

const mt973 = {
  transactionReferenceNumber: tag20("mandatory"),
  requestedMessages: REPEAT(tag12("mandatory"), tag25("mandatory")),
};

export const parser = parserFactory(mt973);

Note that the requestedMessages field uses the REPEAT parser combinator combines the given tags into a single tag and expects one or more occurrences of such combined tag in the input.

Now we can import our parser in our project and use it like so:

import { parser as mt973Parser } from "./mt973";

const mt973Msgs = mt973Parser("input");

for (const mt973Msg of mt973Msgs) {
  const { transactionReferenceNumber, requestedMessages } = mt973Msg;
  console.log(transactionReferenceNumber);
  for (const requestedMessage of requestedMessages) {
    console.log(`transaction reference number: ${requestedMessage}`);
    console.log(`message type: ${requestedMessage.messageType}`);
  }
}