adif-parser-ts
Version:
An amateur radio log data ADIF parser in Typescript and Javascript
57 lines • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdifFormatter = void 0;
/**
* A class for formatting objects into ADIF.
*/
var AdifFormatter = /** @class */ (function () {
function AdifFormatter(obj) {
this.obj = obj;
}
/**
* Format the given object into an ADIF string.
*/
AdifFormatter.formatAdi = function (obj) {
return new AdifFormatter(obj).format();
};
AdifFormatter.prototype.format = function () {
// From just a moment of research, string concatenation should have OK
// performance. Maybe do testing and reconsider.
var buffer = '';
if (this.obj.header !== undefined) {
buffer += this.obj.header.text + '\n';
var restOfHeader = this.obj.header;
delete restOfHeader.text;
buffer += AdifFormatter.formatTags(restOfHeader);
buffer += '<eoh>\n\n';
}
if (!this.obj.records) {
return AdifFormatter.prepReturn(buffer);
}
for (var _i = 0, _a = this.obj.records; _i < _a.length; _i++) {
var rec = _a[_i];
buffer += AdifFormatter.formatTags(rec);
buffer += '<eor>\n\n';
}
return AdifFormatter.prepReturn(buffer);
};
AdifFormatter.formatTags = function (obj) {
var buffer = '';
for (var _i = 0, _a = Object.entries(obj); _i < _a.length; _i++) {
var _b = _a[_i], key = _b[0], value = _b[1];
var width = new TextEncoder().encode(value).byteLength;
buffer += "<".concat(key, ":").concat(width, ">").concat(value, "\n");
}
return buffer;
};
AdifFormatter.prepReturn = function (buffer) {
buffer = buffer.trim();
if (buffer.length === 0) {
return buffer;
}
return buffer + '\n';
};
return AdifFormatter;
}());
exports.AdifFormatter = AdifFormatter;
//# sourceMappingURL=adif-formatter.js.map