49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
const fs = require('fs');
|
|
|
|
function parseJournalEntries(filePath) {
|
|
const entries = [];
|
|
let currentEntry = null;
|
|
|
|
try {
|
|
const data = fs.readFileSync(filePath, 'utf8');
|
|
const lines = data.split('\n');
|
|
|
|
for (let line of lines) {
|
|
//line = line.trim(); // Trim leading/trailing whitespace
|
|
|
|
const regex = /^(\d{4}-\d{2}-\d{2})\s(.*)$/;
|
|
const match = line.match(regex);
|
|
|
|
if (match) {
|
|
const date = match[1];
|
|
const descr = match[2];
|
|
currentEntry = { entries: [] };
|
|
currentEntry.entries.push({ date, descr });
|
|
entries.push(currentEntry);
|
|
} else {
|
|
const regex = /[^\s+]([\w\d:.]+)/g;
|
|
const match = line.match(regex);
|
|
|
|
if (match) {
|
|
const acct = match[0];
|
|
const amount = match[1];
|
|
currentEntry.entries.push({ acct, amount });
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error reading the file:', error);
|
|
}
|
|
|
|
return entries;
|
|
}
|
|
|
|
function convertToJson(entries) {
|
|
return JSON.stringify(entries, null, 4);
|
|
}
|
|
|
|
const journalEntries = parseJournalEntries('main.gl');
|
|
const jsonData = convertToJson(journalEntries);
|
|
console.log(jsonData);
|
|
|