SHARE
TWEET

Untitled

a guest Sep 11th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fs = require('fs'),
  2.     path = require('path'),    
  3.     filePath = path.join(__dirname, 'sie.si');
  4.  
  5. function parseInvoices(text){
  6.     let verRe = /^#VER .+?\n\{\n(#TRANS \d{4} \{.*?\} .+?\n)*\}/mg;
  7.     let accountRe = /^#KONTO .+$/mg;
  8.     let objectRe = /^#OBJEKT .+$/mg;
  9.     let transRe = /^#TRANS .+$/mg;
  10.     let accountSplitRe = /^#KONTO (\d{4}) "?(.+)"?$/;
  11.     let objectSplitRe = /^#OBJEKT (\d+) (\d+) "?(.+)"?$/;
  12.     let transSplitRe = /^#TRANS (\d{4}) \{("\d+" )?("\d+" )?\} (.+)$/;
  13.     let verSplitRe = /^#VER (.+) (.+) (.+) (.+)$/m;
  14.     let stripOrBlank = (t) => t != null ? t.replace(/\"|\s/g,"") : "";
  15.  
  16.     let accounts = text.match(accountRe).map(c => {
  17.         let accSplit = c.match(accountSplitRe);
  18.         return {accountNr: accSplit[1], description: accSplit[2]};
  19.     });
  20.  
  21.     let objects = text.match(objectRe).map(o => {
  22.         let objSplit = o.match(objectSplitRe);
  23.         return {dim: objSplit[1], id: objSplit[2], name: objSplit[3]};
  24.     });
  25.  
  26.     let verifications = text.match(verRe).map(v => {
  27.         let verSplit = v.match(verSplitRe);
  28.         let ver = {series: verSplit[1], nr: verSplit[2], date: verSplit[3], text: verSplit[4].replace("\"", ""), trans: []};
  29.         ver.trans = v.match(transRe).map(t => {
  30.             let transSplit = t.match(transSplitRe);
  31.             return {accountNr: transSplit[1], dim: stripOrBlank(transSplit[2]), obj: stripOrBlank(transSplit[3]), amount: transSplit[4] };
  32.         });
  33.         return ver;
  34.     });
  35.  
  36.     return {
  37.         objects: objects,
  38.         accounts: accounts,
  39.         verifications: verifications
  40.     };
  41. }
  42.  
  43. fs.readFile(filePath, {encoding: 'ascii'}, function(err,data){
  44.     if (!err) {
  45.         var invoices = parseInvoices(data);
  46.         var jsonDocument = JSON.stringify(invoices).replace(/\\"/g,"");
  47.         console.log(jsonDocument);
  48.     } else {
  49.         console.log(err);
  50.     }
  51. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top