-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
90 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { ReturnValue, Tournament } from '.'; | ||
|
||
export interface ExpressionEvaluator { | ||
evaluate(code: string, data: unknown): ReturnValue; | ||
} | ||
|
||
export interface ExpressionEvaluatorClass { | ||
new (instance: Tournament): ExpressionEvaluator; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { ExpressionEvaluator, ReturnValue, Tournament } from '.'; | ||
|
||
export class FunctionEvaluator implements ExpressionEvaluator { | ||
private _codeCache: Record<string, Function> = {}; | ||
|
||
constructor(private instance: Tournament) {} | ||
|
||
private getFunction(expr: string): Function { | ||
if (expr in this._codeCache) { | ||
return this._codeCache[expr]; | ||
} | ||
const [code] = this.instance.getExpressionCode(expr); | ||
const func = new Function('E', code + ';'); | ||
this._codeCache[expr] = func; | ||
return func; | ||
} | ||
|
||
evaluate(expr: string, data: unknown): ReturnValue { | ||
const fn = this.getFunction(expr); | ||
return fn.call(data, this.instance.errorHandler); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { ExpressionEvaluatorClass } from '../src/index'; | ||
import { Tournament } from '../src/index'; | ||
import type { ExpressionTestEvaluation } from './ExpressionFixtures/base'; | ||
import { baseFixtures } from './ExpressionFixtures/base'; | ||
|
||
export const testExpressionsWithEvaluator = (Evaluator: ExpressionEvaluatorClass) => { | ||
const tourn = new Tournament( | ||
(e) => { | ||
console.error(e); | ||
}, | ||
undefined, | ||
Evaluator, | ||
); | ||
const builtins = { | ||
String, | ||
parseFloat, | ||
parseInt, | ||
}; | ||
for (const t of baseFixtures) { | ||
if (!t.tests.some((test) => test.type === 'evaluation')) { | ||
continue; | ||
} | ||
test(t.expression, () => { | ||
for (const test of t.tests.filter( | ||
(test_): test_ is ExpressionTestEvaluation => test_.type === 'evaluation', | ||
)) { | ||
expect( | ||
tourn.execute(t.expression.slice(1), { | ||
...builtins, | ||
$runIndex: 0, | ||
$json: test.input[0], | ||
$item: (i: number) => ({ $json: test.input[i] }), | ||
}), | ||
).toStrictEqual(test.output); | ||
} | ||
}); | ||
} | ||
}; |