@abaplint/runtime
Version:
Transpiler - Runtime
74 lines • 2.44 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 replace(input) {
let val = undefined;
if (typeof input.val === "string") {
val = input.val;
}
else if (input.val instanceof types_1.Character) {
val = input.val.getTrimEnd();
}
else {
val = input.val.get();
}
let wi = undefined;
if (typeof input.with === "string") {
wi = input.with;
}
else if (input.with instanceof types_1.Character) {
wi = input.with.getTrimEnd();
}
else if (input.with) {
wi = input.with.get();
}
let sub = undefined;
if (typeof input.sub === "string") {
sub = input.sub;
}
else if (input.sub instanceof types_1.Character) {
sub = input.sub.getTrimEnd();
}
else if (input.sub) {
sub = input.sub.get();
}
if (sub !== undefined) {
sub = abap_regex_1.ABAPRegExp.escapeRegExp(sub);
}
const regexInput = input.pcre || input.regex;
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 input.val === "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 input.val === "string")) {
const offset = input.off.get();
const length = input.len.get();
val = input.val.getOffset({ offset: 0, length: offset }).get() +
wi +
input.val.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