UNPKG

@abaplint/runtime

Version:
63 lines 1.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parse = parse; const types_1 = require("../types"); const xstring_1 = require("../types/xstring"); function parse(val) { if (typeof val === "number") { return val; } else if (typeof val === "string") { const trimmed = val.trim(); if (trimmed === "") { return 0; } else if (trimmed.includes(".")) { return parseFloat(trimmed); } else { return parseInt(trimmed, 10); } } else if (val instanceof types_1.Integer) { // optimize, as this is the most common case return val.get(); } else if (val instanceof types_1.Float) { return val.getRaw(); } else if (val instanceof xstring_1.XString) { if (val.get() === "") { return 0; } return parseInt(val.get(), 16); } else if (val instanceof types_1.Hex || val instanceof types_1.HexUInt8) { let num = parseInt(val.get(), 16); // handle two complement, if (val.getLength() >= 4) { const maxVal = Math.pow(2, val.get().length / 2 * 8); if (num > maxVal / 2 - 1) { num = num - maxVal; } } return num; } else if (val instanceof types_1.Time || val instanceof types_1.Date) { return val.getNumeric(); } else if (val instanceof types_1.DecFloat34) { return val.getRaw(); } else if (val instanceof types_1.Integer8) { const bigint = val.get(); if (bigint > BigInt(Number.MAX_SAFE_INTEGER) || bigint < BigInt(Number.MIN_SAFE_INTEGER)) { throw new Error("int8 value too large for table expression index"); } return Number(bigint); } else { return parse(val.get()); } } //# sourceMappingURL=_parse.js.map