UNPKG

@abaplint/runtime

Version:
93 lines 3.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.templateFormatting = templateFormatting; const types_1 = require("./types"); function templateFormatting(source, options) { let text = ""; if (source instanceof types_1.FieldSymbol) { if (source.getPointer() === undefined) { throw new Error("GETWA_NOT_ASSIGNED"); } return templateFormatting(source.getPointer(), options); } else if (source instanceof types_1.Table || source instanceof types_1.HashedTable || source instanceof types_1.ABAPObject || source instanceof types_1.Structure) { throw new Error("STRG_ILLEGAL_DATA_TYPE"); } else if (source instanceof types_1.Character) { text = source.getTrimEnd(); } else if (source instanceof types_1.DecFloat34) { const raw = source.getRaw(); if (Number.isInteger(raw)) { text = raw.toFixed(0); } else { text = raw + ""; } } else if (source instanceof types_1.Float) { const raw = source.getRaw(); if (options?.style === "scientific") { text = raw.toExponential().toUpperCase(); text = text.replace(/([+-])(\d)$/, "$10$2"); } else if (Number.isInteger(raw)) { text = raw.toFixed(0); } else { text = raw.toFixed(16); } } else if (source instanceof types_1.Packed) { if (options?.decimals) { text = source.get().toFixed(options.decimals); } else { text = source.get().toFixed(source.getDecimals()); } } else { text = source.get() + ""; } if (options) { if (options.currency !== undefined) { throw "template formatting with currency not supported"; } if (options.date === "iso") { text = text.substr(0, 4) + "-" + text.substr(4, 2) + "-" + text.substr(6, 2); } if (options.time === "iso") { text = text.substr(0, 2) + ":" + text.substr(2, 2) + ":" + text.substr(4, 2); } if (options.timestamp === "iso") { // make sure to get decimals from packed number, text = templateFormatting(source).replace(".", ","); text = text.substr(0, 4) + "-" + text.substr(4, 2) + "-" + text.substr(6, 2) + "T" + text.substr(8, 2) + ":" + text.substr(10, 2) + ":" + text.substr(12, 2) + text.substr(14); if (text === "0--T::") { text = "0000-00-00T00:00:00"; } } else if (options.width && options.pad) { if (options.align === "right") { text = text.trimEnd().padStart(options.width, options.pad); } else { text = text.trimEnd().padEnd(options.width, options.pad); } } else if (options.width) { text = text.trimEnd().padEnd(options.width, " "); } else if (options.decimals && source instanceof types_1.Integer) { text = source.get().toFixed(options.decimals); } else if (options.decimals && source instanceof types_1.Float) { text = source.getRaw().toFixed(options.decimals); } } return text; } //# sourceMappingURL=template_formatting.js.map