UNPKG

ifc-expressions

Version:

Parsing and evaluation of IFC expressions

45 lines (44 loc) 1.94 kB
import { ApplyRegex } from "./ApplyRegex.js"; import { ExprEvalSuccessObj } from "../../ExprEvalResult.js"; import { StringValue } from "../../../value/StringValue.js"; import { Type } from "../../../type/Types.js"; import { FuncArgString } from "../arg/FuncArgString.js"; import { FuncArgRegex } from "../arg/FuncArgRegex.js"; import { MatchesPattern } from "./MatchesPattern.js"; export class ReplacePattern extends ApplyRegex { constructor(name, simplePattern) { super(name, simplePattern, false); this.formalArguments.splice(2, 0, simplePattern ? new FuncArgString(true, ReplacePattern.KEY_REPLACE) : new FuncArgRegex(true, ReplacePattern.KEY_REPLACE)); } getReturnType(argumentTypes) { return Type.STRING; } calculateResult(callingExpr, evaluatedArguments) { const inputValue = evaluatedArguments.get(MatchesPattern.KEY_INPUT); const patternValue = evaluatedArguments.get(MatchesPattern.KEY_PATTERN); const replaceValue = evaluatedArguments.get(ReplacePattern.KEY_REPLACE); const pattern = patternValue.getValue(); if (pattern.length === 0) { return new ExprEvalSuccessObj(inputValue); } let flags = "mg"; if (this.simplePattern) { const caseSensitive = evaluatedArguments.get(MatchesPattern.KEY_CASE_INSENSITIVE); if (caseSensitive.getValue()) { flags = "img"; } } else { flags = evaluatedArguments.get(ReplacePattern.KEY_FLAGS).getValue(); } const regex = new RegExp(pattern, flags); const input = inputValue.getValue(); const replace = replaceValue.getValue(); const result = input.replace(regex, replace); return new ExprEvalSuccessObj(StringValue.of(result)); } } ReplacePattern.KEY_REPLACE = "replaceValue"; //# sourceMappingURL=ReplacePattern.js.map