ifc-expressions
Version:
Parsing and evaluation of IFC expressions
43 lines (42 loc) • 1.76 kB
JavaScript
import { ApplyRegex } from "./ApplyRegex.js";
import { ExprEvalSuccessObj } from "../../ExprEvalResult.js";
import { BooleanValue } from "../../../value/BooleanValue.js";
import { isNullish } from "../../../util/IfcExpressionUtils.js";
import { Type } from "../../../type/Types.js";
export class MatchesPattern extends ApplyRegex {
constructor(name, simplePattern, requireFullMatch) {
super(name, simplePattern, requireFullMatch);
}
getReturnType(argumentTypes) {
return Type.BOOLEAN;
}
calculateResult(callingExpr, evaluatedArguments) {
const inputValue = evaluatedArguments.get(MatchesPattern.KEY_INPUT);
const patternValue = evaluatedArguments.get(MatchesPattern.KEY_PATTERN);
const pattern = patternValue.getValue();
if (pattern.length === 0) {
return new ExprEvalSuccessObj(BooleanValue.of(false));
}
let flags = "m"; // default for simple pattern
if (this.simplePattern) {
const caseSensitive = evaluatedArguments.get(MatchesPattern.KEY_CASE_INSENSITIVE);
if (caseSensitive) {
flags = "im";
}
}
else {
flags = evaluatedArguments.get(MatchesPattern.KEY_FLAGS).getValue();
}
const regex = new RegExp(pattern, flags);
const input = inputValue.getValue();
const matches = input.match(regex);
if (isNullish(matches)) {
return new ExprEvalSuccessObj(BooleanValue.of(false));
}
if (this.requireFullMatch) {
return new ExprEvalSuccessObj(BooleanValue.of(matches[0] === input));
}
return new ExprEvalSuccessObj(BooleanValue.of(true));
}
}
//# sourceMappingURL=MatchesPattern.js.map