@abaplint/runtime
Version:
Transpiler - Runtime
101 lines • 3.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Numc = void 0;
const _parse_1 = require("../operators/_parse");
const throw_error_1 = require("../throw_error");
const float_1 = require("./float");
const initialValues = {};
const regexCharacters = /[a-zA-Z]/g;
class Numc {
value;
length;
qualifiedName;
constructor(input) {
this.length = input?.length ? input?.length : 1;
this.qualifiedName = input?.qualifiedName;
this.clear();
}
clone() {
const n = new Numc({ length: this.length, qualifiedName: this.qualifiedName });
// set without trigger checks and padding
n.value = this.value;
return n;
}
getQualifiedName() {
return this.qualifiedName;
}
set(value, raw = false) {
if (value instanceof Numc && value.getLength() === this.length) {
this.value = value.get();
return this;
}
else if (typeof value === "number") {
this.value = Math.trunc(value) + "";
}
else if (typeof value === "string") {
value = value.trim().replace(regexCharacters, "");
if (value === "") {
this.clear();
}
else {
this.value = parseInt(value, 10) + "";
}
}
else if (value instanceof float_1.Float) {
this.value = Math.trunc(value.getRaw()) + "";
}
else {
this.set(value.get());
return this;
}
if (this.value.length > this.length) {
this.value = this.value.substr(this.value.length - this.length, this.length);
}
else {
const pad = this.length - this.value.length;
if (pad > 0 && raw === false) {
this.value = "0".repeat(pad) + this.value;
}
}
return this;
}
getLength() {
return this.length;
}
clear() {
if (initialValues[this.length] === undefined) {
initialValues[this.length] = "0".repeat(this.length);
}
this.value = initialValues[this.length];
}
get() {
return this.value;
}
getOffset(input) {
let offset = input?.offset;
if (offset) {
offset = (0, _parse_1.parse)(offset);
}
let length = input?.length;
if (length) {
length = (0, _parse_1.parse)(length);
}
if ((offset && offset >= this.length)
|| (offset && offset < 0)
|| (length && length < 0)) {
(0, throw_error_1.throwError)("CX_SY_RANGE_OUT_OF_BOUNDS");
}
let ret = this.value;
if (offset) {
ret = ret.substr(offset);
}
if (length !== undefined) {
ret = ret.substr(0, length);
}
const r = new Numc({ length: ret.length });
r.set(ret);
return r;
}
}
exports.Numc = Numc;
//# sourceMappingURL=numc.js.map