Browse Source

refactor: move arg type

pull/7268/head
Pranav C 10 months ago
parent
commit
ad4ac116a0
  1. 95
      packages/nocodb-sdk/src/lib/formulaHelpers.ts

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

@ -256,12 +256,13 @@ export enum JSEPNode {
} }
interface FormulaMeta { interface FormulaMeta {
type?: string;
validation?: { validation?: {
args?: { args?: {
min?: number; min?: number;
max?: number; max?: number;
rqd?: number; rqd?: number;
type?: FormulaDataTypes;
}; };
custom?: (args: FormulaDataTypes[], parseTree: any) => void; custom?: (args: FormulaDataTypes[], parseTree: any) => void;
}; };
@ -276,6 +277,7 @@ const formulas: Record<string, FormulaMeta> = {
validation: { validation: {
args: { args: {
min: 1, min: 1,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: 'Average of input parameters', description: 'Average of input parameters',
@ -288,10 +290,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
ADD: { ADD: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
min: 1, min: 1,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: 'Sum of input parameters', description: 'Sum of input parameters',
@ -304,10 +306,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
DATEADD: { DATEADD: {
type: FormulaDataTypes.DATE,
validation: { validation: {
args: { args: {
rqd: 3, rqd: 3,
type: FormulaDataTypes.DATE,
}, },
custom: (_argTypes: FormulaDataTypes[], parsedTree: any) => { custom: (_argTypes: FormulaDataTypes[], parsedTree: any) => {
if (parsedTree.arguments[0].type === JSEPNode.LITERAL) { if (parsedTree.arguments[0].type === JSEPNode.LITERAL) {
@ -360,11 +362,11 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.DATE, returnType: FormulaDataTypes.DATE,
}, },
DATETIME_DIFF: { DATETIME_DIFF: {
type: FormulaDataTypes.DATE,
validation: { validation: {
args: { args: {
min: 2, min: 2,
max: 3, max: 3,
type: FormulaDataTypes.DATE,
}, },
custom: (_argTypes: FormulaDataTypes[], parsedTree: any) => { custom: (_argTypes: FormulaDataTypes[], parsedTree: any) => {
if (parsedTree.arguments[0].type === JSEPNode.LITERAL) { if (parsedTree.arguments[0].type === JSEPNode.LITERAL) {
@ -458,10 +460,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.COND_EXP, returnType: FormulaDataTypes.COND_EXP,
}, },
CONCAT: { CONCAT: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
min: 1, min: 1,
type: FormulaDataTypes.STRING,
}, },
}, },
description: 'Concatenated string of input parameters', description: 'Concatenated string of input parameters',
@ -473,10 +475,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
TRIM: { TRIM: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 1, rqd: 1,
type: FormulaDataTypes.STRING,
}, },
}, },
description: 'Remove trailing and leading whitespaces from input parameter', description: 'Remove trailing and leading whitespaces from input parameter',
@ -488,10 +490,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
UPPER: { UPPER: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 1, rqd: 1,
type: FormulaDataTypes.STRING,
}, },
}, },
description: 'Upper case converted string of input parameter', description: 'Upper case converted string of input parameter',
@ -500,10 +502,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
LOWER: { LOWER: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 1, rqd: 1,
type: FormulaDataTypes.STRING,
}, },
}, },
description: 'Lower case converted string of input parameter', description: 'Lower case converted string of input parameter',
@ -512,10 +514,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
LEN: { LEN: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 1, rqd: 1,
type: FormulaDataTypes.STRING,
}, },
}, },
description: 'Input parameter character length', description: 'Input parameter character length',
@ -524,10 +526,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
MIN: { MIN: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
min: 1, min: 1,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: 'Minimum value amongst input parameters', description: 'Minimum value amongst input parameters',
@ -536,10 +538,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
MAX: { MAX: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
min: 1, min: 1,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: 'Maximum value amongst input parameters', description: 'Maximum value amongst input parameters',
@ -548,10 +550,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
CEILING: { CEILING: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1, rqd: 1,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: 'Rounded next largest integer value of input parameter', description: 'Rounded next largest integer value of input parameter',
@ -560,10 +562,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
FLOOR: { FLOOR: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1, rqd: 1,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: description:
@ -573,11 +575,11 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
ROUND: { ROUND: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
min: 1, min: 1,
max: 2, max: 2,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: description:
@ -591,10 +593,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
MOD: { MOD: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 2, rqd: 2,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: 'Remainder after integer division of input parameters', description: 'Remainder after integer division of input parameters',
@ -603,10 +605,11 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
REPEAT: { REPEAT: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 2, rqd: 2,
type: FormulaDataTypes.STRING,
}, },
}, },
description: description:
@ -616,8 +619,11 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
LOG: { LOG: {
validation: {
args: {
type: FormulaDataTypes.NUMERIC, type: FormulaDataTypes.NUMERIC,
validation: {}, },
},
description: description:
'Logarithm of input parameter to the base (default = e) specified', 'Logarithm of input parameter to the base (default = e) specified',
syntax: 'LOG([base], value)', syntax: 'LOG([base], value)',
@ -625,18 +631,21 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
EXP: { EXP: {
validation: {
args: {
type: FormulaDataTypes.NUMERIC, type: FormulaDataTypes.NUMERIC,
validation: {}, },
},
description: 'Exponential value of input parameter (e ^ power)', description: 'Exponential value of input parameter (e ^ power)',
syntax: 'EXP(power)', syntax: 'EXP(power)',
examples: ['EXP(1) => 2.718281828459045', 'EXP({column1})'], examples: ['EXP(1) => 2.718281828459045', 'EXP({column1})'],
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
POWER: { POWER: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 2, rqd: 2,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: 'base to the exponent power, as in base ^ exponent', description: 'base to the exponent power, as in base ^ exponent',
@ -645,10 +654,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
SQRT: { SQRT: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1, rqd: 1,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: 'Square root of the input parameter', description: 'Square root of the input parameter',
@ -657,10 +666,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
ABS: { ABS: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1, rqd: 1,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: 'Absolute value of the input parameter', description: 'Absolute value of the input parameter',
@ -669,10 +678,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
NOW: { NOW: {
type: FormulaDataTypes.DATE,
validation: { validation: {
args: { args: {
rqd: 0, rqd: 0,
type: FormulaDataTypes.DATE,
}, },
}, },
description: 'Returns the current time and day', description: 'Returns the current time and day',
@ -681,10 +690,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.DATE, returnType: FormulaDataTypes.DATE,
}, },
REPLACE: { REPLACE: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 3, rqd: 3,
type: FormulaDataTypes.STRING,
}, },
}, },
description: description:
@ -697,10 +706,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
SEARCH: { SEARCH: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 2, rqd: 2,
type: FormulaDataTypes.STRING,
}, },
}, },
description: 'Index of srchStr specified if found, 0 otherwise', description: 'Index of srchStr specified if found, 0 otherwise',
@ -712,10 +721,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
INT: { INT: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1, rqd: 1,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: 'Integer value of input parameter', description: 'Integer value of input parameter',
@ -724,10 +733,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
RIGHT: { RIGHT: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 2, rqd: 2,
type: FormulaDataTypes.STRING,
}, },
}, },
description: 'n characters from the end of input parameter', description: 'n characters from the end of input parameter',
@ -736,10 +745,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
LEFT: { LEFT: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 2, rqd: 2,
type: FormulaDataTypes.STRING,
}, },
}, },
description: 'n characters from the beginning of input parameter', description: 'n characters from the beginning of input parameter',
@ -748,11 +757,11 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
SUBSTR: { SUBSTR: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
min: 2, min: 2,
max: 3, max: 3,
type: FormulaDataTypes.STRING,
}, },
}, },
description: description:
@ -766,10 +775,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
MID: { MID: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 3, rqd: 3,
type: FormulaDataTypes.STRING,
}, },
}, },
description: 'Alias for SUBSTR', description: 'Alias for SUBSTR',
@ -778,7 +787,6 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
IF: { IF: {
type: FormulaDataTypes.COND_EXP,
validation: { validation: {
args: { args: {
min: 2, min: 2,
@ -816,11 +824,13 @@ const formulas: Record<string, FormulaMeta> = {
}, },
}, },
SWITCH: { SWITCH: {
type: FormulaDataTypes.COND_EXP,
validation: { validation: {
args: { args: {
min: 3, min: 3,
}, },
custom: (_argTypes: any[], _parseTree) => {
// Todo: Add validation for switch
},
}, },
description: 'Switch case value based on expr output', description: 'Switch case value based on expr output',
syntax: 'SWITCH(expr, [pattern, value, ..., default])', syntax: 'SWITCH(expr, [pattern, value, ..., default])',
@ -856,10 +866,10 @@ const formulas: Record<string, FormulaMeta> = {
}, },
}, },
URL: { URL: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 1, rqd: 1,
type: FormulaDataTypes.STRING,
}, },
}, },
description: 'Convert to a hyperlink if it is a valid URL', description: 'Convert to a hyperlink if it is a valid URL',
@ -868,11 +878,11 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
WEEKDAY: { WEEKDAY: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
min: 1, min: 1,
max: 2, max: 2,
type: FormulaDataTypes.NUMERIC,
}, },
custom(_argTypes: FormulaDataTypes[], parsedTree: any) { custom(_argTypes: FormulaDataTypes[], parsedTree: any) {
if (parsedTree.arguments[0].type === JSEPNode.LITERAL) { if (parsedTree.arguments[0].type === JSEPNode.LITERAL) {
@ -916,7 +926,6 @@ const formulas: Record<string, FormulaMeta> = {
}, },
TRUE: { TRUE: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
max: 0, max: 0,
@ -929,7 +938,6 @@ const formulas: Record<string, FormulaMeta> = {
}, },
FALSE: { FALSE: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
max: 0, max: 0,
@ -942,10 +950,10 @@ const formulas: Record<string, FormulaMeta> = {
}, },
REGEX_MATCH: { REGEX_MATCH: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 2, rqd: 2,
type: FormulaDataTypes.STRING,
}, },
}, },
description: description:
@ -956,10 +964,10 @@ const formulas: Record<string, FormulaMeta> = {
}, },
REGEX_EXTRACT: { REGEX_EXTRACT: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 2, rqd: 2,
type: FormulaDataTypes.STRING,
}, },
}, },
description: 'Returns the first match of a regular expression in a string.', description: 'Returns the first match of a regular expression in a string.',
@ -968,10 +976,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
REGEX_REPLACE: { REGEX_REPLACE: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 3, rqd: 3,
type: FormulaDataTypes.STRING,
}, },
}, },
description: description:
@ -981,7 +989,6 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.STRING, returnType: FormulaDataTypes.STRING,
}, },
BLANK: { BLANK: {
type: FormulaDataTypes.STRING,
validation: { validation: {
args: { args: {
rqd: 0, rqd: 0,
@ -993,11 +1000,11 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NULL, returnType: FormulaDataTypes.NULL,
}, },
XOR: { XOR: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
min: 1, min: 1,
}, },
// todo: validation for boolean
}, },
description: description:
'Returns true if an odd number of arguments are true, and false otherwise.', 'Returns true if an odd number of arguments are true, and false otherwise.',
@ -1006,10 +1013,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.BOOLEAN, returnType: FormulaDataTypes.BOOLEAN,
}, },
EVEN: { EVEN: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1, rqd: 1,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: description:
@ -1019,10 +1026,10 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
ODD: { ODD: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
rqd: 1, rqd: 1,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: description:
@ -1080,11 +1087,11 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
ROUNDDOWN: { ROUNDDOWN: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
min: 1, min: 1,
max: 2, max: 2,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: description:
@ -1094,11 +1101,11 @@ const formulas: Record<string, FormulaMeta> = {
returnType: FormulaDataTypes.NUMERIC, returnType: FormulaDataTypes.NUMERIC,
}, },
ROUNDUP: { ROUNDUP: {
type: FormulaDataTypes.NUMERIC,
validation: { validation: {
args: { args: {
min: 1, min: 1,
max: 2, max: 2,
type: FormulaDataTypes.NUMERIC,
}, },
}, },
description: description:

Loading…
Cancel
Save