Przeglądaj źródła

add length var operation for objects (#5318)

Khaleel Al-Adhami 22 godzin temu
rodzic
commit
e6208bee48
2 zmienionych plików z 20 dodań i 0 usunięć
  1. 8 0
      reflex/vars/object.py
  2. 12 0
      tests/integration/test_var_operations.py

+ 8 - 0
reflex/vars/object.py

@@ -144,6 +144,14 @@ class ObjectVar(Var[OBJECT_TYPE], python_types=Mapping):
 
     items = entries
 
+    def length(self) -> NumberVar[int]:
+        """Get the length of the object.
+
+        Returns:
+            The length of the object.
+        """
+        return self.keys().length()
+
     def merge(self, other: ObjectVar):
         """Merge two objects.
 

+ 12 - 0
tests/integration/test_var_operations.py

@@ -608,6 +608,15 @@ def VarOperations():
                 int_var2=VarOperationState.int_var2,
                 id="memo_comp_nested",
             ),
+            # length
+            rx.box(
+                rx.text(VarOperationState.list3.length()),
+                id="list_length",
+            ),
+            rx.box(
+                rx.text(VarOperationState.obj.length()),
+                id="obj_length",
+            ),
             # foreach in a match
             rx.box(
                 rx.match(
@@ -944,6 +953,9 @@ def test_var_operations(driver, var_operations: AppHarness):
         # rx.memo component with state
         ("memo_comp", "1210"),
         ("memo_comp_nested", "345"),
+        # length
+        ("list_length", "3"),
+        ("obj_length", "3"),
         # foreach in a match
         ("foreach_in_match", "first\nsecond\nthird"),
         # literal range in a foreach