jspurefix
Version:
pure node js fix engine
85 lines • 3.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleFieldDefinition = void 0;
const field_enum_1 = require("../field-enum");
const tags_1 = require("../../buffer/tag/tags");
const ascii_chars_1 = require("../../buffer/ascii/ascii-chars");
const _ = require("lodash");
class SimpleFieldDefinition {
constructor(num, name, abbreviation, baseCategory, baseCategoryAbbreviation, type, description) {
this.num = num;
this.name = name;
this.abbreviation = abbreviation;
this.baseCategory = baseCategory;
this.baseCategoryAbbreviation = baseCategoryAbbreviation;
this.type = type;
this.description = description;
this.tag = parseInt(num, 10);
this.tagType = tags_1.Tags.toType(type);
}
isEnum() {
return this.enums != null;
}
containsEnum(key) {
const enums = this.enums;
if (!enums) {
return false;
}
return enums.has(key);
}
resolveEnum(key) {
const enums = this.enums;
if (!enums) {
return key;
}
const e = enums.get(key);
if (e) {
return e.val;
}
return key;
}
patchEnumValue(v) {
let converted = _.upperFirst(_.camelCase(v));
const charAtPos = converted.charCodeAt(0);
const zero = ascii_chars_1.AsciiChars.Zero;
const nine = ascii_chars_1.AsciiChars.Nine;
const atDigit = charAtPos >= zero && charAtPos <= nine;
if (atDigit) {
converted = `E${converted}`;
}
if (this.enumVals.has(converted)) {
converted = `${converted}2`;
}
return converted;
}
addEnum(key, val, description) {
let enums = this.enums;
let enumVals = this.enumVals;
if (enums == null) {
this.enums = enums = new Map();
this.enumVals = enumVals = new Map();
}
if (!description)
description = val;
val = this.patchEnumValue(val);
enums.set(key, new field_enum_1.FieldEnum(key, val, description !== null && description !== void 0 ? description : ''));
enumVals.set(val, true);
}
toString() {
var _a;
let abbreviation = '';
if (this.abbreviation && this.name !== this.abbreviation) {
abbreviation = `(${this.abbreviation})`;
}
let baseCategoryAbbreviation = '';
if (this.baseCategoryAbbreviation && this.baseCategoryAbbreviation !== this.name) {
baseCategoryAbbreviation = this.baseCategoryAbbreviation;
}
if (baseCategoryAbbreviation.length > 0 && this.baseCategory) {
baseCategoryAbbreviation = `${this.baseCategory} ${baseCategoryAbbreviation}`;
}
return `${this.num} ${this.name} ${abbreviation} ${baseCategoryAbbreviation} ${this.type} ${(_a = this.description) !== null && _a !== void 0 ? _a : ''} ${this.enums ? `enumerated = [ ${this.enums.toString()} ]` : ''}`;
}
}
exports.SimpleFieldDefinition = SimpleFieldDefinition;
//# sourceMappingURL=simple-field-definition.js.map