47 lines
1.5 KiB
JavaScript
47 lines
1.5 KiB
JavaScript
/**
|
|
* EDIFACT Logic Utilities
|
|
*/
|
|
window.EDIBridge = window.EDIBridge || {};
|
|
|
|
class EDIFACTLogic {
|
|
constructor(options = {}) {
|
|
this.segmentTerminator = options.segmentTerminator || "'";
|
|
this.elementSeparator = options.elementSeparator || "+";
|
|
this.componentSeparator = options.componentSeparator || ":";
|
|
this.decimalSeparator = options.decimalSeparator || ".";
|
|
this.escapeCharacter = options.escapeCharacter || "?";
|
|
}
|
|
|
|
segment(tag, elements = []) {
|
|
const formattedElements = elements.map(el => {
|
|
if (Array.isArray(el)) {
|
|
return el.map(comp => this.escape(comp)).join(this.componentSeparator);
|
|
}
|
|
return this.escape(el);
|
|
});
|
|
|
|
return tag + this.elementSeparator + formattedElements.join(this.elementSeparator) + this.segmentTerminator;
|
|
}
|
|
|
|
escape(val) {
|
|
if (val === undefined || val === null) return '';
|
|
const s = String(val);
|
|
return s.replace(/[+':?]/g, match => this.escapeCharacter + match);
|
|
}
|
|
|
|
static parseSegments(content) {
|
|
const segmentTerminator = "'";
|
|
return content.split(segmentTerminator)
|
|
.map(s => s.trim())
|
|
.filter(s => s.length > 0)
|
|
.map(s => {
|
|
const parts = s.split('+');
|
|
const tag = parts[0];
|
|
const elements = parts.slice(1).map(el => el.split(':'));
|
|
return { tag, elements };
|
|
});
|
|
}
|
|
}
|
|
|
|
window.EDIBridge.EDIFACTLogic = EDIFACTLogic;
|