From 97fb157b2580d3f2d0ab028a97ff7edd3f3f908c Mon Sep 17 00:00:00 2001 From: Ryan <15974789+5quinque@users.noreply.github.com> Date: Fri, 3 Jan 2025 20:50:04 +0000 Subject: [PATCH] Add `endswith` method to String class (#4577) Co-authored-by: ryan <> --- reflex/vars/sequence.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/reflex/vars/sequence.py b/reflex/vars/sequence.py index 476c1e32c..5864e70b9 100644 --- a/reflex/vars/sequence.py +++ b/reflex/vars/sequence.py @@ -271,6 +271,25 @@ class StringVar(Var[STRING_TYPE], python_types=str): raise_unsupported_operand_types("startswith", (type(self), type(prefix))) return string_starts_with_operation(self, prefix) + @overload + def endswith(self, suffix: StringVar | str) -> BooleanVar: ... + + @overload + def endswith(self, suffix: NoReturn) -> NoReturn: ... + + def endswith(self, suffix: Any) -> BooleanVar: + """Check if the string ends with a suffix. + + Args: + suffix: The suffix. + + Returns: + The string ends with operation. + """ + if not isinstance(suffix, (StringVar, str)): + raise_unsupported_operand_types("endswith", (type(self), type(suffix))) + return string_ends_with_operation(self, suffix) + @overload def __lt__(self, other: StringVar | str) -> BooleanVar: ... @@ -501,6 +520,24 @@ def string_starts_with_operation( ) +@var_operation +def string_ends_with_operation( + full_string: StringVar[Any], suffix: StringVar[Any] | str +): + """Check if a string ends with a suffix. + + Args: + full_string: The full string. + suffix: The suffix. + + Returns: + Whether the string ends with the suffix. + """ + return var_operation_return( + js_expression=f"{full_string}.endsWith({suffix})", var_type=bool + ) + + @var_operation def string_item_operation(string: StringVar[Any], index: NumberVar | int): """Get an item from a string.