51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
const fs = require('fs');
|
|
|
|
function parseTextFile(filePath) {
|
|
const content = fs.readFileSync(filePath, 'utf-8');
|
|
const lines = content.split('\n');
|
|
const entries = [];
|
|
let currentEntry = {};
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
const line = lines[i].trim();
|
|
|
|
if (line !== '') {
|
|
const parts = line.split(/\s+/);
|
|
|
|
if (parts.length === 1) {
|
|
// New entry detected, push the current entry to the entries array
|
|
if (Object.keys(currentEntry).length > 0) {
|
|
entries.push(currentEntry);
|
|
}
|
|
|
|
// Create a new entry object
|
|
currentEntry = {
|
|
header: line,
|
|
details: [],
|
|
};
|
|
} else {
|
|
const account = parts[0];
|
|
const amount = parseFloat(parts[1]);
|
|
|
|
currentEntry.details.push({
|
|
account,
|
|
amount,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Push the last entry to the entries array
|
|
if (Object.keys(currentEntry).length > 0) {
|
|
entries.push(currentEntry);
|
|
}
|
|
|
|
return entries;
|
|
}
|
|
|
|
// Usage: Provide the path to your text file here
|
|
const filePath = 'main.gl';
|
|
const entries = parseTextFile(filePath);
|
|
console.log(entries);
|
|
|