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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 179x 179x 179x 1x 1x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 1x 1x 200x 1357x 14x 14x 14x 1343x 1343x 200x 200x 1x 1x 1x 1x 1x 1x 1x 1x 174x 174x 174x 1x 1x 173x 174x 1x 1x 172x 172x 172x 172x 172x 172x 172x 172x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 6x 1x 1x 1x 1x 6x 1x 1x 1x 1x | import { isNil } from 'lodash-es';
import { ConstantNode, FunctionNode, type MathNode } from 'mathjs';
import { commonMathJs } from '@amalia/amalia-lang/amalia-mathjs';
import { type AmaliaFormula } from '@amalia/amalia-lang/formula/types';
export class SanitizeFormula {
/**
* Lint formula string.
* @param formula
*/
public static sanitizeFormula(formula: string): string;
public static sanitizeFormula(formula: null): null;
public static sanitizeFormula(formula: string | null): string | null {
// Formula can be falsy (like "0"), but it has to follow the usual path.
// We only stop if the formula is null or undefined.
if (isNil(formula)) {
return formula;
}
// Force conversion to string because sometimes we have numbers.
return (
`${formula}`
.replaceAll(' AND ', ' and ')
.replaceAll(' OR ', ' or ')
.replaceAll(' NOT ', ' not ')
.replaceAll(' XOR ', ' xor ')
// Replace = with == (or else it's an assignment).
.replaceAll(/(?<left>[^<>=!])=(?<right>[^=])/gu, '$<left>==$<right>')
);
}
public static replaceNodes(node: MathNode) {
return node.transform((n: MathNode): MathNode => {
if (commonMathJs.isOperatorNode(n) && n.op === '==') {
const children = n.args.map((ni) => SanitizeFormula.replaceNodes(ni));
return new FunctionNode('equal', [new FunctionNode('compareNatural', children), new ConstantNode(0)]);
}
return n;
});
}
/**
* Replace equal by mathjs function node equal, because equal in Mathjs is an assignment.
*
* Transform mathjs to any as we are slowly migrating mathjs from javascript to typescript,
* but there are still lot of issues with our current usage.
*/
private static replaceEqualByCompareNatural(formula: string | null): string | null {
// Formula can be falsy (like "0"), but it has to follow the usual path.
// We only stop if the formula is null or undefined.
if (isNil(formula)) {
return formula;
}
if (formula === '') {
return '';
}
// documentation => https://mathjs.org/docs/expressions/parsing.html
const topLevelNode = commonMathJs.parse(formula);
const transformedReplaceEqual = SanitizeFormula.replaceNodes(topLevelNode);
return transformedReplaceEqual.toString();
}
/**
* For lisibility, we simplified writing formulas to our users, for instance by using `=` instead
* of `==`. This function converts Amalia formulas to mathjs compatible formula.
*
* @param formula
*/
public static readonly amaliaFormulaToMathJs = (formula: AmaliaFormula | string) =>
SanitizeFormula.replaceEqualByCompareNatural(SanitizeFormula.sanitizeFormula(formula));
/**
* Parse the formula then unparse it then reparse it to make it beautiful (i guess). See unit tests.
* @param formula
*/
public static readonly expressionPrettier = (formula: AmaliaFormula): AmaliaFormula => {
if (!formula) {
return formula;
}
try {
return commonMathJs
.parse(SanitizeFormula.sanitizeFormula(formula))
.toString({
/**
* @see https://mathjs.org/docs/reference/functions/format.html
* Beware! Colossal numbers will be truncated:
*
* ● SanitizeFormula › expressionPrettier › should not replace colossal numbers by their scientific notation
*
* expect(received).toBe(expected) // Object.is equality
*
* Expected: "IF(amount = 12345678901234567890, true, false)"
* Received: "IF(amount = 12345678901234567000, true, false)"
*/
// Make sure the numbers are not converted to scientific notation.
notation: 'fixed',
})
.replaceAll(' == ', ' = ');
} catch {
// The formula has error but it's only a prettier, return the formula as-is, we expect the user
// will correct it anyway since it won't pass form validation.
return formula;
}
};
public static readonly replaceDollarRowInFormula = (formula: AmaliaFormula, replacement: string) =>
formula.replaceAll('$row', replacement);
}
|