Skip to content

Commit ee3ce7d

Browse files
feat: non-static evaluate function
1 parent e6a7ac6 commit ee3ce7d

File tree

3 files changed

+6
-58
lines changed

3 files changed

+6
-58
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ There are two ways to use CEL expressions in your code:
2727
For simple use cases where you evaluate an expression once:
2828

2929
```typescript
30-
import { CelProgram } from "cel-typescript";
30+
import { evaluate } from "cel-typescript";
3131

3232
// Basic string and numeric operations
33-
await CelProgram.evaluate("size(message) > 5", { message: "Hello World" }); // true
33+
await evaluate("size(message) > 5", { message: "Hello World" }); // true
3434

3535
// Complex object traversal and comparison
36-
await CelProgram.evaluate("user.age >= 18 && user.preferences.notifications", {
36+
await evaluate("user.age >= 18 && user.preferences.notifications", {
3737
user: {
3838
age: 25,
3939
preferences: { notifications: true },

__tests__/cel.test.ts

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ describe("Performance measurements", () => {
138138
console.log(`Execution took ${executeTime.toFixed(0)} nanoseconds`);
139139

140140
// Measure one-step evaluation time
141-
const [, evaluateTime] = await measureTime(() =>
142-
CelProgram.evaluate(expr, context),
143-
);
141+
const [, evaluateTime] = await measureTime(() => evaluate(expr, context));
144142
console.log(
145143
`One-step evaluation took ${evaluateTime.toFixed(0)} nanoseconds`,
146144
);
@@ -159,44 +157,3 @@ describe("Performance measurements", () => {
159157
expect(evaluateTime).toBeLessThan(expectedEvaluateTime * (1 + tolerance));
160158
});
161159
});
162-
163-
describe("CelProgram.evaluate", () => {
164-
it("should evaluate a simple expression in one step", async () => {
165-
const result = await CelProgram.evaluate("size(message) > 5", {
166-
message: "Hello World",
167-
});
168-
expect(result).toBe(true);
169-
});
170-
171-
it("should handle errors gracefully", async () => {
172-
await expect(
173-
CelProgram.evaluate("invalid expression", {}),
174-
).rejects.toThrow();
175-
});
176-
177-
it("should handle complex expressions and data structures", async () => {
178-
const result = await CelProgram.evaluate(
179-
'{"name": "test", "items": [1, 2, 3].map(i, {"id": i})}',
180-
{},
181-
);
182-
expect(result).toEqual({
183-
name: "test",
184-
items: [{ id: 1 }, { id: 2 }, { id: 3 }],
185-
});
186-
});
187-
188-
it("should handle cart validation in one step", async () => {
189-
const expr =
190-
'has(cart.items) && cart.items.exists(item, item.productId == "prod_123" && item.quantity >= 1)';
191-
const result = await CelProgram.evaluate(expr, {
192-
cart: {
193-
items: [
194-
{ productId: "prod_456", quantity: 1 },
195-
{ productId: "prod_123", quantity: 1 },
196-
{ productId: "prod_789", quantity: 3 },
197-
],
198-
},
199-
});
200-
expect(result).toBe(true);
201-
});
202-
});

src/index.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@ class CelProgram {
4444
async execute(context: CelContext): Promise<any> {
4545
return this.native.execute(context);
4646
}
47-
48-
/**
49-
* Convenience method to compile and execute a CEL expression in one step.
50-
* Note: If you plan to evaluate the same expression multiple times with different contexts,
51-
* it's more efficient to use compile() once and then call execute() multiple times.
52-
*/
53-
static async evaluate(source: string, context: CelContext): Promise<any> {
54-
const program = await CelProgram.compile(source);
55-
return program.execute(context);
56-
}
5747
}
5848

5949
/**
@@ -66,7 +56,8 @@ class CelProgram {
6656
* @returns The result of evaluating the expression
6757
*/
6858
async function evaluate(source: string, context: CelContext): Promise<any> {
69-
return CelProgram.evaluate(source, context);
59+
const program = await CelProgram.compile(source);
60+
return program.execute(context);
7061
}
7162

7263
export { CelProgram, evaluate };

0 commit comments

Comments
 (0)