Skip to content

Commit 39b0bc5

Browse files
committed
feat(interpreter): adds possibility to customize interpreter name detection
1 parent 4468773 commit 39b0bc5

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

packages/core/src/interpreter.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,40 @@ function getInterpreter<T extends Record<string, AnyInterpreter>>(
3333

3434
export interface InterpreterOptions {
3535
numberOfArguments?: 1 | 2 | 3
36+
getInterpreterName?(condition: Condition, context: this): string
37+
}
38+
39+
function defaultInterpreterName(condition: Condition) {
40+
return condition.operator;
3641
}
3742

3843
export function createInterpreter<T extends AnyInterpreter, U extends {} = {}>(
3944
interpreters: Record<string, T>,
4045
rawOptions?: U
4146
) {
4247
const options = rawOptions as U & InterpreterOptions;
48+
const getInterpreterName = options && options.getInterpreterName || defaultInterpreterName;
4349
let interpret;
4450

4551
switch (options ? options.numberOfArguments : 0) {
4652
case 1:
4753
interpret = ((condition) => {
48-
const interpretOperator = getInterpreter(interpreters, condition.operator);
54+
const interpreterName = getInterpreterName(condition, options);
55+
const interpretOperator = getInterpreter(interpreters, interpreterName);
4956
return interpretOperator(condition, defaultContext); // eslint-disable-line @typescript-eslint/no-use-before-define
5057
}) as InterpretationContext<T>['interpret'];
5158
break;
5259
case 3:
5360
interpret = ((condition, value, params) => {
54-
const interpretOperator = getInterpreter(interpreters, condition.operator);
61+
const interpreterName = getInterpreterName(condition, options);
62+
const interpretOperator = getInterpreter(interpreters, interpreterName);
5563
return interpretOperator(condition, value, params, defaultContext); // eslint-disable-line @typescript-eslint/no-use-before-define
5664
}) as InterpretationContext<T>['interpret'];
5765
break;
5866
default:
5967
interpret = ((condition, value) => {
60-
const interpretOperator = getInterpreter(interpreters, condition.operator);
68+
const interpreterName = getInterpreterName(condition, options);
69+
const interpretOperator = getInterpreter(interpreters, interpreterName);
6170
return interpretOperator(condition, value, defaultContext); // eslint-disable-line @typescript-eslint/no-use-before-define
6271
}) as InterpretationContext<T>['interpret'];
6372
break;

0 commit comments

Comments
 (0)