Browse Source

Merge pull request #5270 from nocodb/refactor/formula

refactor: formula
pull/5291/head
աɨռɢӄաօռɢ 2 years ago committed by GitHub
parent
commit
d3b622337a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      packages/nocodb-sdk/src/lib/formulaHelpers.ts
  2. 67
      packages/nocodb/src/lib/utils/common/helpers/jsepTreeToFormula.ts
  3. 2
      packages/nocodb/src/lib/utils/common/helpers/updateColumnNameInFormula.ts

7
packages/nocodb-sdk/src/lib/formulaHelpers.ts

@ -179,9 +179,8 @@ export function jsepTreeToFormula(node) {
if (node.type === 'Literal') {
if (typeof node.value === 'string') {
return '"' + node.value + '"';
return String.raw`"${escapeDoubleQuotes(node.value)}"`;
}
return '' + node.value;
}
@ -214,3 +213,7 @@ export function jsepTreeToFormula(node) {
return '';
}
function escapeDoubleQuotes(v: string) {
return v.replace(/"/g, '\\"');
}

67
packages/nocodb/src/lib/utils/common/helpers/jsepTreeToFormula.ts

@ -1,67 +0,0 @@
export default function jsepTreeToFormula(node) {
if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') {
return (
'(' +
jsepTreeToFormula(node.left) +
' ' +
node.operator +
' ' +
jsepTreeToFormula(node.right) +
')'
);
}
if (node.type === 'UnaryExpression') {
return node.operator + jsepTreeToFormula(node.argument);
}
if (node.type === 'MemberExpression') {
return (
jsepTreeToFormula(node.object) +
'[' +
jsepTreeToFormula(node.property) +
']'
);
}
if (node.type === 'Identifier') {
return node.name;
}
if (node.type === 'Literal') {
if (typeof node.value === 'string') {
return '"' + node.value + '"';
}
return '' + node.value;
}
if (node.type === 'CallExpression') {
return (
jsepTreeToFormula(node.callee) +
'(' +
node.arguments.map(jsepTreeToFormula).join(', ') +
')'
);
}
if (node.type === 'ArrayExpression') {
return '[' + node.elements.map(jsepTreeToFormula).join(', ') + ']';
}
if (node.type === 'Compound') {
return node.body.map((e) => jsepTreeToFormula(e)).join(' ');
}
if (node.type === 'ConditionalExpression') {
return (
jsepTreeToFormula(node.test) +
' ? ' +
jsepTreeToFormula(node.consequent) +
' : ' +
jsepTreeToFormula(node.alternate)
);
}
return '';
}

2
packages/nocodb/src/lib/utils/common/helpers/updateColumnNameInFormula.ts

@ -1,4 +1,4 @@
import jsepTreeToFormula from './jsepTreeToFormula';
import { jsepTreeToFormula } from 'nocodb-sdk';
export default function (args: {
virtualColumns;

Loading…
Cancel
Save