Skip to content

Commit 4330a79

Browse files
committed
preprocess a bit more of parsed functions
1 parent 23bba4c commit 4330a79

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

web/src/utils/column-parser.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,20 @@ const funcs: { [name: string]: (columns: Column[], record: Record, args: string[
6969
}
7070
};
7171

72-
const parse = (columns: Column[], record: Record, calculatedValue: string) => {
73-
// OR
72+
const parseORs = (columns: Column[], calculatedValue: string): ((record: Record) => ColValue)[] => {
73+
// parseORs returns a closure [(Record) => ColValue] to pre-process as much as possible
7474
const ors = calculatedValue.split(' or ');
75-
for (const or of ors) {
75+
return ors.map(or => {
7676
for (const name in funcs) {
7777
if (or.startsWith(name + '(')) {
7878
const regex = new RegExp(name + '|\\(|\\)', 'g');
7979
const repl = or.replaceAll(regex, '');
8080
const args = repl.split(',');
81-
const result = funcs[name](columns, record, args);
82-
if (result) {
83-
return result;
84-
}
81+
return (record: Record) => funcs[name](columns, record, args);
8582
}
8683
}
87-
}
88-
return undefined;
84+
return (_: Record) => undefined;
85+
});
8986
};
9087

9188
const forceType = (id: ColumnsId, value: ColValue, type?: FieldType): ColValue => {
@@ -123,7 +120,16 @@ export const computeValueFunc = (
123120
return result.flatMap(r => r) as ColValue;
124121
};
125122
}
126-
return (r: Record) => parse(columns, r, def.calculated!);
123+
const orFuncs = parseORs(columns, def.calculated!);
124+
return (r: Record) => {
125+
for (const orFunc of orFuncs) {
126+
const result = orFunc(r);
127+
if (result) {
128+
return result;
129+
}
130+
}
131+
return undefined;
132+
};
127133
} else if (fields) {
128134
return (r: Record) => {
129135
const result: ColValue[] = fields.map(fc => {

0 commit comments

Comments
 (0)