Add endswith method to String class

This commit is contained in:
ryan 2024-12-30 20:04:02 +00:00
parent 848b87070c
commit ad326d9ea6

View File

@ -271,6 +271,25 @@ class StringVar(Var[STRING_TYPE], python_types=str):
raise_unsupported_operand_types("startswith", (type(self), type(prefix))) raise_unsupported_operand_types("startswith", (type(self), type(prefix)))
return string_starts_with_operation(self, 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 @overload
def __lt__(self, other: StringVar | str) -> BooleanVar: ... 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 @var_operation
def string_item_operation(string: StringVar[Any], index: NumberVar | int): def string_item_operation(string: StringVar[Any], index: NumberVar | int):
"""Get an item from a string. """Get an item from a string.