@@ -12,34 +12,60 @@ See the full [language definition][lang-def] for a complete overview of CEL.
1212
1313[ lang-def ] : https://github.com/google/cel-spec/blob/master/doc/langdef.md
1414
15+ There are two ways to use CEL expressions in your code:
16+
17+ ### One-Step Evaluation
18+
19+ For simple use cases where you evaluate an expression once:
20+
1521``` typescript
1622import { CelProgram } from ' cel-typescript' ;
1723
1824// Basic string and numeric operations
19- const program1 = await CelProgram .compile (' size(message) > 5' );
20- await program1 .execute ({ message: ' Hello World' }); // true
25+ await CelProgram .evaluate (' size(message) > 5' , { message: ' Hello World' }); // true
2126
2227// Complex object traversal and comparison
23- const program2 = await CelProgram .compile (' user.age >= 18 && user.preferences.notifications' );
24- await program2 .execute ({
25- user: {
26- age: 25 ,
27- preferences: { notifications: true }
28+ await CelProgram .evaluate (
29+ ' user.age >= 18 && user.preferences.notifications' ,
30+ {
31+ user: {
32+ age: 25 ,
33+ preferences: { notifications: true }
34+ }
2835 }
29- }); // true
36+ ); // true
37+ ```
38+
39+ ### Compile Once, Execute Multiple Times
40+
41+ For better performance when evaluating the same expression multiple times with different contexts:
3042
31- // List operations and built-in functions
32- const program3 = await CelProgram .compile (' items.filter(i, i.price < 100).size() > 0' );
33- await program3 .execute ({
43+ ``` typescript
44+ import { CelProgram } from ' cel-typescript' ;
45+
46+ // Compile the expression once
47+ const program = await CelProgram .compile (' items.filter(i, i.price < max_price).size() > 0' );
48+
49+ // Execute multiple times with different contexts
50+ await program .execute ({
3451 items: [
3552 { name: ' Book' , price: 15 },
3653 { name: ' Laptop' , price: 1000 }
37- ]
54+ ],
55+ max_price: 100
56+ }); // true
57+
58+ await program .execute ({
59+ items: [
60+ { name: ' Phone' , price: 800 },
61+ { name: ' Tablet' , price: 400 }
62+ ],
63+ max_price: 500
3864}); // true
3965
4066// Date/time operations using timestamp() macro
41- const program4 = await CelProgram .compile (' timestamp(event_time) < timestamp("2025-01-01T00:00:00Z")' );
42- await program4 .execute ({
67+ const timeProgram = await CelProgram .compile (' timestamp(event_time) < timestamp("2025-01-01T00:00:00Z")' );
68+ await timeProgram .execute ({
4369 event_time: ' 2024-12-31T23:59:59Z'
4470}); // true
4571```
0 commit comments