You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
"use strict";
|
|
|
|
const fs = require("fs");
|
|
const os = require("os");
|
|
const chalk = require("chalk");
|
|
const path = require("path");
|
|
|
|
if (process.argv[2] == null) {
|
|
console.error(chalk.red("You must specify one or more MT940 (.STA) files to fix, like so:\n abn-amro-mt940-fix YOUR-FILE.STA"));
|
|
} else {
|
|
process.argv.slice(2).forEach((file) => {
|
|
let absolutePath = path.resolve(file);
|
|
let contents = fs.readFileSync(absolutePath, { encoding: "utf8" });
|
|
|
|
let parsedPath = path.parse(absolutePath);
|
|
delete parsedPath.base;
|
|
parsedPath.name += "-fixed2";
|
|
|
|
let fixedContents = contents.replace(/(^|\r\n-\r\n)ABNANL2A\r\n940\r\nABNANL2A\r\n/g, "$1");
|
|
let fixedFilename = path.format(parsedPath);
|
|
|
|
let lines = fixedContents.split(/\r?\n/);
|
|
let outputLines = [];
|
|
for (let i=0; i<lines.length; i++) {
|
|
let line = lines[i];
|
|
if (line.startsWith(":86:")) { // contents start
|
|
let desc = line;
|
|
while (!lines[i+1].startsWith(":")) {
|
|
desc += lines[i+1];
|
|
i += 1;
|
|
}
|
|
let replacements = [
|
|
"SEPA OVERBOEKING",
|
|
"SEPA PERIODIEKE OVERB.",
|
|
"IBAN: ",
|
|
"NAAM:",
|
|
"/TRTP/IBAN/",
|
|
/\/TRTP\/.+\/IBAN\//,
|
|
/\/BIC\/\w+\//,
|
|
/NAME\//,
|
|
/\/REMI\//,
|
|
/KENMERK: \w+/,
|
|
/BIC: \w+ /,
|
|
/ +(?= )/g // remove multiple spaces
|
|
]
|
|
replacements.forEach((repl) => {
|
|
desc = desc.replace(repl,' ');
|
|
})
|
|
desc = desc.replace("OMSCHRIJVING:",",");
|
|
outputLines.push(desc);
|
|
} else {
|
|
outputLines.push(line);
|
|
}
|
|
}
|
|
fixedContents = outputLines.join(os.EOL);
|
|
|
|
fs.writeFileSync(fixedFilename, fixedContents, { encoding: "utf8" })
|
|
|
|
console.log(file, "->", fixedFilename);
|
|
});
|
|
}
|