Skip to content

Commit 2f8a34c

Browse files
authored
Added missing endsWith macro for String (#51)
1 parent 5d38c5e commit 2f8a34c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

interpreter/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ impl<'a> Default for Context<'a> {
154154
ctx.add_function("all", functions::all);
155155
ctx.add_function("max", functions::max);
156156
ctx.add_function("startsWith", functions::starts_with);
157+
ctx.add_function("endsWith", functions::ends_with);
157158
ctx.add_function("matches", functions::matches);
158159
ctx.add_function("duration", functions::duration);
159160
ctx.add_function("timestamp", functions::timestamp);

interpreter/src/functions.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ pub fn starts_with(This(this): This<Arc<String>>, prefix: Arc<String>) -> bool {
224224
this.starts_with(prefix.as_str())
225225
}
226226

227+
/// Returns true if a string ends with another string.
228+
///
229+
/// # Example
230+
/// ```cel
231+
/// "abc".endsWith("c") == true
232+
/// ```
233+
pub fn ends_with(This(this): This<Arc<String>>, suffix: Arc<String>) -> bool {
234+
this.ends_with(suffix.as_str())
235+
}
236+
227237
/// Returns true if a string matches the regular expression.
228238
///
229239
/// # Example
@@ -671,6 +681,16 @@ mod tests {
671681
.for_each(assert_script);
672682
}
673683

684+
#[test]
685+
fn test_ends_with() {
686+
[
687+
("ends with true", "'foobar'.endsWith('bar') == true"),
688+
("ends with false", "'foobar'.endsWith('foo') == false"),
689+
]
690+
.iter()
691+
.for_each(assert_script);
692+
}
693+
674694
#[test]
675695
fn test_timestamp() {
676696
[(

0 commit comments

Comments
 (0)