a-fas / mt940js

Swift MT940 bank statement javascript parser
Apache License 2.0
32 stars 17 forks source link

Tag 61 - New line not working #10

Closed nickidk4 closed 5 years ago

nickidk4 commented 5 years ago

Parser can't handle extra data field. In Parser.js Instead of splitting on '\n' you need to split on '\n:'. If you split on '\n' you will split on the newline inside tag 61. instead of newline at the end of tag 61.

parser.js:111

_splitAndNormalize(data) {
    return data
        .split('\n:')
        .map(line => {
            return line
                .replace('\r', '')
                .replace(/\s+$/, '');
        })
        .filter(line => !!line && line !== '-');
}

And then dont include ":" in retag.

parser.js:126

*_parseLines(lines) {
    const reTag = /^([0-9]{2}|NS)([A-Z])?:/;
    let tag = {};
    for (let i of lines) {
        if (i.startsWith('-}') || i.startsWith('{')) {
            continue; // Skip message headers
        }

        let match = i.match(reTag);
        if (match) { // Tag found
            if (tag.id) { yield tag } // Yield previous
            tag = { // Start new tag
                id: match[1],
                subId: match[2] || '',
                data: [i.substr(match[0].length)]
            };
        } else { // Add a line to previous tag
            tag.data = [];
            tag.data.push(i);
        }
    }

    if (tag.id) { yield tag } // Yield last
}

It took me quite some time to locate the bug. Hope you can update with my fix soon, or another alternative :)

a-fas commented 5 years ago

Hmmm, could you send an example which does not work ? The issue is not clear frankly. Also please in text not in a screenshot ;)

The lib does support multiline data https://github.com/a-fas/mt940js/blob/master/test/parser.test.js#L14 https://github.com/a-fas/mt940js/blob/master/test/parser.test.js#L112 So ... should work ...

nickidk4 commented 5 years ago

The tests you linked to shows that Tag 86 can handle multiple lines. But tag 61 can have multiple lines as well. image

a-fas commented 5 years ago

Ah, ok ... :) I'll have a look but an example would be very helpful

nickidk4 commented 5 years ago

Well swift messages contain quite private info. Will check with my boss how I can help you with examples :) And I will see if I can start making pull requests instead of pasting code in comments.

a-fas commented 5 years ago

Had a look, in fact the additional line in tag 61 was supported already, but had a bug :) Fixed and added a test. Try the latest code (not released as npm yet), if OK I'll release. The transaction attribute is called extraDetails.

nickidk4 commented 5 years ago

It still doesnt work. When I have tag 61 with newline it fails. Only works when I use my own fix. image

:61:180901D10,00NTRF6100103A82462003//03MT180933175035
/OCMT/EUR10,00/
a-fas commented 5 years ago

Hmmm, are you sure you're using the last code (right from the repo - I didn't publish on npm yet). Just pasted your line to example file (in test dir) and run it with cli.js - seems to work well. (had to change the amount and date also ...) image

nickidk4 commented 5 years ago

I see now how it works. And it works :)