52 lines
1.9 KiB
JavaScript
52 lines
1.9 KiB
JavaScript
const fs = require('fs');
|
|
try {
|
|
const { Reader, NullValidator, Parser } = require('ts-edifact');
|
|
const filePath = 'c:\\Users\\matthias.hoesch\\.gemini\\antigravity\\scratch\\vda-to-edifact-converter\\samples\\test_efz.edi';
|
|
const content = fs.readFileSync(filePath, 'utf8');
|
|
const normalized = content.replace(/\r\n/g, '').replace(/\r/g, '').trim();
|
|
|
|
console.log('Normalized content length:', normalized.length);
|
|
|
|
// We cannot easily inject NullValidator into Reader because it's hardcoded in constructor.
|
|
// Instead, let's try to use Parser + NullValidator directly if we want pure syntax check.
|
|
// However, Reader provides a nice way to get the result array.
|
|
|
|
const reader = new Reader();
|
|
|
|
// Re-initialize parser with NullValidator if possible
|
|
const validator = new NullValidator();
|
|
reader.validator = validator;
|
|
// We need to overwrite the parser as well because it's already created with the old validator
|
|
reader.parser = new Parser(validator);
|
|
|
|
// Force UNOC
|
|
reader.encoding('UNOC');
|
|
|
|
// Re-setup callbacks for the new parser
|
|
const result = reader.result;
|
|
let elements = reader.elements;
|
|
let components = reader.components;
|
|
|
|
reader.parser.onOpenSegment = function (segment) {
|
|
elements = [];
|
|
result.push({ name: segment, elements: elements });
|
|
};
|
|
reader.parser.onElement = function () {
|
|
components = [];
|
|
elements.push(components);
|
|
};
|
|
reader.parser.onComponent = function (value) {
|
|
components.push(value);
|
|
};
|
|
|
|
const segments = reader.parse(normalized);
|
|
console.log('Successfully parsed', segments.length, 'segments.');
|
|
console.log('First 3 segments:');
|
|
console.log(JSON.stringify(segments.slice(0, 3), null, 2));
|
|
|
|
} catch (e) {
|
|
console.error('Error during parsing:');
|
|
console.error(e.message);
|
|
if (e.stack) console.error(e.stack);
|
|
}
|