@abaplint/runtime
Version:
Transpiler - Runtime
87 lines • 2.83 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.replace = replace;
const string_1 = require("../types/string");
const abap_regex_1 = require("../abap_regex");
const types_1 = require("../types");
function dereference(input) {
if (input instanceof types_1.FieldSymbol) {
const pointer = input.getPointer();
if (pointer === undefined) {
throw new Error("GETWA_NOT_ASSIGNED");
}
return pointer;
}
return input;
}
function replace(input) {
const source = dereference(input.val);
const withSource = dereference(input.with);
const subSource = dereference(input.sub);
const regexInput = dereference(input.pcre || input.regex);
let val = undefined;
if (typeof source === "string") {
val = source;
}
else if (source instanceof types_1.Character) {
val = source.getTrimEnd();
}
else {
val = source.get();
}
let wi = undefined;
if (typeof withSource === "string") {
wi = withSource;
}
else if (withSource instanceof types_1.Character) {
wi = withSource.getTrimEnd();
}
else if (withSource) {
wi = withSource.get();
}
let sub = undefined;
if (typeof subSource === "string") {
sub = subSource;
}
else if (subSource instanceof types_1.Character) {
sub = subSource.getTrimEnd();
}
else if (subSource) {
sub = subSource.get();
}
if (sub !== undefined) {
sub = abap_regex_1.ABAPRegExp.escapeRegExp(sub);
}
if (typeof regexInput === "string") {
sub = new RegExp(abap_regex_1.ABAPRegExp.convert(regexInput), "g");
}
else if (regexInput) {
sub = new RegExp(abap_regex_1.ABAPRegExp.convert(regexInput.get()), "g");
}
if (input.off && input.len && typeof source === "string") {
const offset = input.off.get();
const length = input.len.get();
val = val.substring(0, offset) + wi + val.substring(offset + length);
}
else if (input.off && input.len && !(typeof source === "string")) {
const offset = input.off.get();
const length = input.len.get();
val = source.getOffset({ offset: 0, length: offset }).get() +
wi +
source.getOffset({ offset: offset + length }).get();
}
else if (input.occ === undefined && sub && wi !== undefined) {
if (typeof sub === "string") {
sub = new RegExp(sub);
}
val = val.replace(sub, wi);
}
else if (input.occ && input.occ.get() === 0 && sub && wi !== undefined) {
if (typeof sub === "string") {
sub = new RegExp(sub, "g");
}
val = val.replace(sub, wi);
}
return new string_1.String().set(val);
}
//# sourceMappingURL=replace.js.map