Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 30x 30x 30x 30x 30x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 6x 12x 6x 6x 6x 6x 12x 12x 12x 12x 12x | import {
FormulaBuilderBlockType,
type AmaliaFormula,
type FormulaBuilderLogicalOperatorBlock,
} from '@amalia/amalia-lang/formula/types';
// Copied from escape-string-regexp but we can't import ES modules on the backend for now.
export const regexEscape = (str: string) =>
str.replaceAll(/[|\\{}()[\]^$+*?.]/gu, String.raw`\$&`).replaceAll('-', String.raw`\x2d`);
export function replaceInExpression(expression: string, searchKey: string, replaceKey: string) {
// Example: find "filter.closedByRepInPeriod" or "salesforce.abr"
// in expression = SUM(filter.closedByRepInPeriod, "salesforce.abr")
// and NOT expression = SUM(filter.closedByRepInPeriod2, "salesforce.abr2")
// Test online: https://regex101.com/r/Jp81qV/1
const searchRegex = new RegExp(String.raw`(?<!\w)${regexEscape(searchKey)}(?!\w)`, 'gu');
return expression.replace(searchRegex, replaceKey) as AmaliaFormula;
}
export function replaceInFormulaBuilder<TBlock extends FormulaBuilderLogicalOperatorBlock>(
block: TBlock,
searchKey: string,
replaceKey: string,
): TBlock {
return {
...block,
operands: block.operands.map((operand) => {
switch (operand.type) {
case FormulaBuilderBlockType.LOGICAL_OPERATOR:
return replaceInFormulaBuilder(operand, searchKey, replaceKey);
case FormulaBuilderBlockType.FORMULA:
return {
...operand,
formula: replaceInExpression(operand.formula, searchKey, replaceKey),
};
default:
// Here we miss FunctionBlock, will be implemented during phase 2
return operand;
}
}),
};
}
|